My (unhealthy) obsession with the (if?then:else) shorthand syntax.

  • 8
    I love it as long as you don't chain multiple ones together, that becomes unreadable really quickly.
  • 3
    Unfortunately our coding standard forbids this :(
  • 3
    @LucaScorpion i sometimes chain for simple things, but never more than one extra if. Mostly for array.sort in Javascript. I'm lazy, and doing that lets me get away without closing the lambda expression in {} 🙃
  • 0
    @yusijs also you can avoid using filter, not that its bad, but because one line sexyness
    arr.forEach(e => condition ? something : '')
  • 0
    @nicholai same with filter? arr.filter(v => v.abc ? true : false)
    Unless I missed something?
  • 0
    Only if the whole phrase fits in about 40 characters, and it is part of some parameterized list. Otherwise​ it should be a regular if else statement.
  • 0
    @yusijs in my case it would be arr.filter(e => condition).forEach(e => something)
  • 0
    @nicholai aah, gotcha!
  • 0
    @querellaMMXII That's a strange coding standard! Many places say the shorthand is so clean and easy it's actually preferred!
  • 1
    @chasb96 we're doing safety critical coding and the short form can look quite bad if misused.

    It's far less ambiguous to write out the long form.

    We can use the ternary operator but we have to justify it with a comment which kinda defeats the point. Even then it probably wouldn't make it through review.
Add Comment