3 ms·
If I'm reading it right, those false cases shouldn't exist; it should be: x = (condA) ? (trueA) : (condB) ? (trueB) : (condC) ? (trueC) :
by setr 7y ago
If I'm reading it right, those false cases shouldn't exist; it should be:
x = (condA) ? (trueA) :
(condB) ? (trueB) :
(condC) ? (trueC) :
(condD) ? (trueD) :
(condE) ? (trueE) : (final case);
and the equivalent if/else chain should be:
if (condA) {x = trueA }
elif (condB) {x = trueB }
elif (condC) {x = trueC }
elif (condD) {x = trueD }
elif (condE) {x = trueE }
else {x = (final case) }
In which case it doesn't really get you much readability-wise, since the formatting consistency appears in either case; the main benefit really is that ternary is an expression rather than a statement
It’s definitely superior, since it’s still less noise all around, but almost worthlessly so. And then the same pattern gets covered by switches too...
- jacoblambda 7y agoYep I'm dumb. That's what I get for writing that snippet last night with a few drinks in me. As for comparison against switches, I find that switches are very limited in a lot of languages (looking at you C/C++). The problem with the if else chain as well is that most code formatters seem to blow it up into the fully expanded form which kills readability.