3 ms·
> I agree with the sibling to this comment, though, that a ternary is more readable with newlines and indentation: I think it's even clearer like so: retu
by naasking 8y ago
> I agree with the sibling to this comment, though, that a ternary is more readable with newlines and indentation:
I think it's even clearer like so:
return a < b ? -1:
a > b ? 1:
0;
Conditions/guards on the left, values on the right. So I emphatically disagree with the OP that nested ternary expressions are never ok. In most sane languages with proper precedence and evaluation order, they work great when formatted as above. I say sane languages, because the nested ternary doesn't work the way you'd expect in JavaScript...
- jlokier 8y agoI think your version is reasonably clear, but it won't survive automatic indentation in any tool. I use the parantheses the way I do, because editors will auto-indent the code that way. In other words if I press <tab> in Emacs, things won't move around in my example, so I know it's indented properly. However, within that style, some people do prefer to put the operator at the end of a continuation line, and some prefer it at the beginning.