Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
philnash ( philnash@programming.dev )  to JavaScript@programming.devEnglish · 3 years ago

Stop nesting ternaries in JavaScript

www.sonarsource.com

external-link
message-square
25
link
fedilink
26
external-link

Stop nesting ternaries in JavaScript

www.sonarsource.com

philnash ( philnash@programming.dev )  to JavaScript@programming.devEnglish · 3 years ago
message-square
25
link
fedilink
Nesting ternary operators makes code more complex and less clear. Let's investigate other ways to write conditional expressions.
alert-triangle
You must log in or # to comment.
  • kassuro ( kassuro@feddit.de ) 
    link
    fedilink
    arrow-up
    19
    ·
    3 years ago

    I often have the pleasure of refactoring this mess. And sometimes it’s not just 2 ternaries but like 4 or 5 thrown together. It took like half an hour to even understand what was going on. If you do this, you are just an evil person…

    So I totally support this. Stop this shit…

    • folkrav ( folkrav@lemmy.ca ) 
      link
      fedilink
      arrow-up
      9
      ·
      3 years ago

      This shit always leaves me wondering who even writes this crap. The answer is more often than not a junior that just discovered code golf thinking he’s oh so clever. You learn to appreciate boring code, with experience…

      • kassuro ( kassuro@feddit.de ) 
        link
        fedilink
        arrow-up
        4
        ·
        3 years ago

        Totally agree with you. I’m always an advocate for boring and easy to understand code.

        I really don’t need an extra layer of complexity just so someone can save a line or two.

    • coloredgrayscale ( coloredgrayscale@programming.dev ) 
      link
      fedilink
      arrow-up
      9
      ·
      3 years ago

      pleasure of refactoring this mess took half an hour to understand

      If you can use any of the jetBrains IDE it can rewrite it to if/else with a few clicks (or [alt] +[return])

      • kassuro ( kassuro@feddit.de ) 
        link
        fedilink
        arrow-up
        2
        ·
        3 years ago

        Ah that would be helpful. Gotta try that the next time I encounter such a monstrosity.

    • Lmaydev ( Lmaydev@programming.dev ) 
      link
      fedilink
      arrow-up
      1
      ·
      3 years ago

      Perfect job for ChatGPT.

  • JakenVeina ( JakenVeina@lemm.ee ) 
    link
    fedilink
    arrow-up
    7
    ·
    3 years ago

    Until JS supports switch expressions, nested ternaries will continue to be the most effective way to write multi-state conditionals.

    Also, stop using linting tools that prioritize consistency over human readability, and then complaining that the code they generate is not easily-readable by humans.

    • CameronDev ( CameronDev@programming.dev ) 
      link
      fedilink
      arrow-up
      4
      ·
      3 years ago

      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch - hasnt it had this forever? Or are you refering to something else?

      • spartanatreyu ( spartanatreyu@programming.dev ) 
        link
        fedilink
        arrow-up
        10
        ·
        3 years ago

        Pretty sure they meant match as in pattern matching, not switch as in switch/case/break.

        You can see the proposal here: https://github.com/tc39/proposal-pattern-matching

        • CameronDev ( CameronDev@programming.dev ) 
          link
          fedilink
          arrow-up
          3
          ·
          3 years ago

          Your probably right, that looks quite desirable.

        • snowe ( snowe@programming.dev ) 
          link
          fedilink
          arrow-up
          2
          ·
          3 years ago

          they also said switch expressions, which indicates they want the switch statement to be settable directly to a variable with whatever the return type of the switch is.

          • spartanatreyu ( spartanatreyu@programming.dev ) 
            link
            fedilink
            arrow-up
            2
            ·
            3 years ago

            Match already returns the value which can be thrown into a variable.

        • JakenVeina ( JakenVeina@lemm.ee ) 
          link
          fedilink
          arrow-up
          1
          ·
          3 years ago

          Nah, I meant switch, as that’s what it’s called in C#-land. See above.

          That proposal for matching looks interesting, but not quite the same, no.

          • spartanatreyu ( spartanatreyu@programming.dev ) 
            link
            fedilink
            arrow-up
            4
            ·
            3 years ago

            Are you sure?

            Your C# example:

            var output = input switch
            {
                null    => "Null",
                0       => "Zero",
                > 0     => "Positive",
                _       => "Negative"
            };
            

            JS proposal for match:

            const output = match input {
                when null:    "Null";
                when 0:       "Zero";
                if input > 0: "Positive";
                default:      "Negative";
            }
            
            • JakenVeina ( JakenVeina@lemm.ee ) 
              link
              fedilink
              arrow-up
              1
              ·
              3 years ago

              Aha, yeah, I see it now. Looking forward to it.

      • JakenVeina ( JakenVeina@lemm.ee ) 
        link
        fedilink
        arrow-up
        2
        ·
        3 years ago

        Yeah, a switch expression is different than a switch statement. I’m not actually sure how many languages actually have them, but in C# its…

        var output = input switch
        {
            null    => "Null",
            0       => "Zero",
            > 0     => "Positive",
            _       => "Negative"
        };
        
    • T (they/she) ( Templa@beehaw.org ) 
      link
      fedilink
      arrow-up
      2
      ·
      3 years ago

      deleted by creator

  • CameronDev ( CameronDev@programming.dev ) 
    link
    fedilink
    arrow-up
    6
    ·
    3 years ago

    Does this article need to exist? I assumed anyone writing a nested ternary was intentionally writing shit code

    • T (they/she) ( Templa@beehaw.org ) 
      link
      fedilink
      arrow-up
      6
      ·
      3 years ago

      Some people think they are being “cleaver” writing a lot of stuff in one line. So yes, it does.

  • SokathHisEyesOpen ( Anticorp@lemmy.ml ) 
    link
    fedilink
    English
    arrow-up
    5
    ·
    3 years ago

    No.

  • hoot ( hoot@lemmy.ca ) 
    link
    fedilink
    arrow-up
    4
    ·
    3 years ago

    I’ll be sending this article to my dev teams. It’s right up there with “stop writing Helper classes”.

    • coloredgrayscale ( coloredgrayscale@programming.dev ) 
      link
      fedilink
      arrow-up
      5
      ·
      3 years ago

      What’s wrong with helper/util classes?

  • jadero ( jadero@programming.dev ) 
    link
    fedilink
    arrow-up
    3
    ·
    3 years ago

    Or anywhere for that matter. Have you got nested IF()s in Excel? For crying out loud, pop into VBA and write it up as a custom function like a human being.

    • spartanatreyu ( spartanatreyu@programming.dev ) 
      link
      fedilink
      arrow-up
      4
      ·
      3 years ago

      Good news Everyone

      Excel has a JS API now, so we can get rid of VBA entirely and fill Excel with terrible ternary operators!

      • jadero ( jadero@programming.dev ) 
        link
        fedilink
        arrow-up
        1
        ·
        3 years ago

        Oh, I didn’t know that Excel could do JS. I haven’t touched it since I got out of the field a decade or so ago.

        One of the things I did was quite a bit of Excel consulting and training. I remember the joy of trying to decode massive formula cells, especially when there were nested IF()s. My rule of thumb was that anything with more than three functions in one formula got converted to a custom function, even if it didn’t have general utility.

        I found that anyone who could construct and understand those massive formulas were generally capable of dealing with the equivalent VBA with a bit of training. Also, it was generally true that if they couldn’t handle the relevant VBA, they were in no position to deal with massive formulas anyway.

  • jeremyparker ( jeremyparker@programming.dev ) 
    link
    fedilink
    arrow-up
    1
    ·
    3 years ago

    I’m pretty sure wolves don’t really bark. They’re all growls and howls. You said you wanted more bike shedding, right?

JavaScript@programming.dev

javascript@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !javascript@programming.dev
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 14 users / day
  • 27 users / week
  • 33 users / month
  • 156 users / 6 months
  • 22 local subscribers
  • 2.83K subscribers
  • 313 Posts
  • 250 Comments
  • Modlog
  • mods:
  • Erlingur ( erlingur@programming.dev ) 
  • Ategon ( Ategon@programming.dev ) 
  • nick ( nick@programming.dev ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code