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

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.

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