6 ms·
Maybe you'll prefer return (a < b) ? -1 : // a smaller (a > b) ? 1 : // a greater 0; // equal
by illys 7y ago
Maybe you'll prefer
return
(a < b) ? -1 : // a smaller
(a > b) ? 1 : // a greater
0; // equal
- beobab 7y agoThis is a beautiful way to express multiple ?: expressions.
- kazinator 7y agoI don't agree. It occludes the nested structure. We can regard the condition as the head of a clause, and the ? and : as heads of sub-clauses: A ? B : C which leads to: return a < b ? -1 : a > b ? 1 : 0;
- atq2119 7y agoWhat you're writing here is equivalent to insisting that people do: if (a < b) { return -1; } else { if (a > b) { return 1; } else { return 0; } }
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- kazinator 7y agoNope, since I haven't added any non-whitespace characters, rather it is like insisting that people do: if (a < b) { return -1; } else if (a > b) { return 1; } else { return 0; } Unlike your perfectly formatted code there, this has a bit of a visual problem: a one-liner consequent is fully braced, whereas a multi-line alternative isn't. Ternary operators are different. They have confusing nesting and do not support the equivalent of the "if/else ladder" pattern very well.
- naasking 7y ago> It occludes the nested structure. The nesting is irrelevant to understanding the semantics, so this objection doesn't fly. The idiom is basically just a truth table with conditions on the left and the matching value on the right. You read it left-to-right, top-to-bottom, just like all other code. The first condition on the left that matches returns the value on right-hand side.
- kazinator 7y ago> The nesting is irrelevant to understanding the semantics The nesting is absolutely relevant to producing the semantics. The following uses deceptive whitespace to suggest a nesting that is contrary to the actual nesting, interfering with understanding: if (foo) if (bar) xyzzy(); else flop(); The ternary operator A ? B : C is the goofy invention of demented mind. In nested situations, it is mind-bendingly unreadable. It behooves us to style it in a way that reveals the abstract syntax tree structure. In C, I would in fact recommend: #define if3(a, b, c) ((a) ? (c) : (c)) return if3(a < b, -1, if3(a > b, 1, 0)); Now we can have it in one line, yet it's clear.
- naasking 7y ago> The following uses deceptive whitespace to suggest a nesting that is contrary to the actual nesting, interfering with understanding Fortunately, the ternary pattern doesn't exhibit this problem. So again, this objection doesn't fly.
- macspoofing 7y agoWhat's the point? Why not just make it an if-elseif-else block and make it obvious?
- verall 7y agoTernary isn't the same as a branch and you want your this simple comparison function to get inlined every time
- masklinn 7y ago> Ternary isn't the same as a branch It should be. The C ternary is just an expression-oriented version of if/elseif/else. Languages with a functional bend simply make if/elseif/else an expression in the first place e.g. in Rust it'd be return if a < b { Ordering::Less } else if a > b { Ordering::Greater } else { Ordering::Equal } Though obviously that specific version is an overly complex way of writing: return a.cmp(b);
- naasking 7y agoMost compilers will probably reduce if-else and ternary to the same instructions. If it's a conditional value binding it might not be a branch but a conditional move instruction.
- convolvatron 7y agoi find it really odd that of all the knocking back and forth on this thread, you are the only person to suggest that using symbolic values for 'greater than', etc, improves readability.
- masklinn 7y agoIt's not really my suggestion though, it's just what Rust does (inherited from Haskell, interestingly enough OCaml does not do that) and I figured I'd post "proper" rust code rather than a bastardisation.
- 7y ago
- baby 7y agostill less clear