3 ms·
I find ternary operators perfectly readable if you split the parts across lines — they map exactly to the conditions and statements in an if-else group. Co
by bobbylarrybobby 6y ago
I find ternary operators perfectly readable if you split the parts across lines — they map exactly to the conditions and statements in an if-else group.
Cond1 // if this
? Result1 // return this
: cond2 // else if this
? Result2 // return this
: cond3 // else if this
? Result3 // return this
: ResultElse // else return this
- pests 6y agoWhich is not true in PHP up until the most recent version (iirc) due to it being left associative instead of right associative you are assuming.
- amenod 6y agoTrue, or even: Cond1 // if this ? Result1 // return this : cond2 // else if this ? Result2 // return this : cond3 // else if this ? Result3 // return this : ResultElse // else return this
- giantDinosaur 6y agoGoodness me that has potential to become something truly hideous.
- Kkoala 6y agoIt's funny how people have so many different preferences for ternary formatting. I can't think of any other operator that would have tens of different format prefs.
- amenod 6y agoIt's actually not that bad in practice... But I guess the pragmatic formatting is "whatever `prettier` does by default" anyway. :)
- mekkkkkk 6y agoI've come to hesistently accept nested ternaries, but I try to have all lines lead with a colon. This kinda mimics a switch statement with returns. firstPossible ? thenThis : secondPossible ? thenThisOtherThing : thirdPossible ? thenThisThirdThing : defaultThing Always fun to see how people treat ternary nesting. They still feel a bit naughty to me. The terseness is sort of unbeatable though.
- Kkoala 6y agoIt's funny how people have so many different preferences for ternary formatting. I can't think of any other operator that would have tens of different format prefs.