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.
  • 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.

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.

  • 1 user / day
  • 26 users / week
  • 34 users / month
  • 157 users / 6 months
  • 22 local subscribers
  • 2.83K subscribers
  • 314 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