SDF Chatter
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
philnash@programming.dev to JavaScript@programming.devEnglish · 1 year ago

Stop nesting ternaries in JavaScript

www.sonarsource.com

external-link
message-square
34
fedilink
32
external-link

Stop nesting ternaries in JavaScript

www.sonarsource.com

philnash@programming.dev to JavaScript@programming.devEnglish · 1 year ago
message-square
34
fedilink
Nesting ternary operators makes code more complex and less clear. Let's investigate other ways to write conditional expressions.
  • JakenVeina@lemm.ee
    link
    fedilink
    arrow-up
    10
    arrow-down
    2
    ·
    1 year 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@programming.dev
      link
      fedilink
      arrow-up
      5
      ·
      1 year 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@programming.dev
        link
        fedilink
        arrow-up
        10
        ·
        1 year 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@programming.dev
          link
          fedilink
          arrow-up
          3
          ·
          1 year ago

          Your probably right, that looks quite desirable.

        • snowe@programming.dev
          link
          fedilink
          arrow-up
          2
          arrow-down
          1
          ·
          1 year 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@programming.dev
            link
            fedilink
            arrow-up
            2
            ·
            1 year ago

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

        • JakenVeina@lemm.ee
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          1 year 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@programming.dev
            link
            fedilink
            arrow-up
            5
            ·
            edit-2
            1 year 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@lemm.ee
              link
              fedilink
              arrow-up
              1
              ·
              1 year ago

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

      • JakenVeina@lemm.ee
        link
        fedilink
        arrow-up
        2
        ·
        edit-2
        1 year 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)@beehaw.org
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      1 year ago

      deleted by creator

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.

  • 7 users / day
  • 7 users / week
  • 68 users / month
  • 283 users / 6 months
  • 11 local subscribers
  • 2.37K subscribers
  • 169 Posts
  • 354 Comments
  • Modlog
  • mods:
  • Erlingur@programming.dev
  • Ategon@programming.dev
  • nick@programming.dev
  • BE: 0.19.8
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org