8 ms·
Ifs are generally bad for readability though, because they force the reader to understand your control flow. Replacing an if with a ternary makes it more readab
by lmm 6y ago
Ifs are generally bad for readability though, because they force the reader to understand your control flow. Replacing an if with a ternary makes it more readable, not because the generated code is any different, but because a reader immediately knows that they don't need to scan through the two sides for control flow constructs (because the two sides of the ternary are guaranteed to be expressions rather than blocks). An if might occasionally be clearer than some convoluted algebra, but not often.
- cortesoft 6y agoSome languages allow blocks in a ternary... ruby, for example
- caturopath 6y agoDo you know if there's any evidence that ternaries beat ifs for people understanding code in practice or in people's self-assessments? Would not have been my guess at all.
- ZephyrBlu 6y agoAn anecdotal data point: I prefer ternaries unless the if statement is for control flow. They're terser and don't stick out as much in the code.
- memco 6y agoIf Hillel Wayne’s talk on empirical software design is any good measure then either method is fine[0] so long as you and the team you work with can agree about it’s utility in code review. If it’s just you on the project then do whatever is most comfortable. [0] https://m.youtube.com/watch?v=WELBnE33dpY https://m.youtube.com/watch?v=WELBnE33dpY
- nopeYouAreWrong 6y agoI have been reading HN for a long time but your comment is the first time I violently felt the need to create an account and respond. I want you to consider how significant that is. Ternaries are NEVER easier to read.
- xvedejas 6y agoMaybe ternaries aren't easier to read, but they make the surrounding code easier to read because they duck out of the way? Devoting several lines to what is a very quick if/else expression could be distracting I suppose. Playing devil's advocate.
- Buttons840 6y agoIt's important to note that ternaries are expressions, they evaluate to a value. When I see a ternary I know that a boolean is being mapped to a value. This is true both technically and almost always in practice. If-statements are the wild west. Who knows what an if statement will do? An if-statement may be as simple as a ternary, or it might be a branch point and the resulting logic streams will never meet again.
- microtherion 6y agoWe're talking C. As soon as a ternary calls a function, all bets are off what could be happening in addition to the value being computed. And thanks to the preprocessor, any identifier could potentially be calling a function.
- boulos 6y agoHmm? Which part implies “all bets are off”? There’s the condition (evaluated first), the true case and the false case. Only one of the cases is evaluated. I’m assuming you were alluding to some sort of “x++ = x++ * *x++” style situation, but the ternary expression isn’t a statement and there are sequence points in between. As a bonus, the first result for “ternary operator evaluation order” gave me someone who linked to the standard [1]. Quoting from the standard that they quoted: > The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand(whichever is evaluated), converted to the type described below. [1] https://stackoverflow.com/questions/65109494/order-of-evaluation-for-ternary-operator-in-c https://stackoverflow.com/questions/65109494/order-of-evalua...
- sk5t 6y agoThe ternary and unary increment/decrement operators are things I thought I’d miss, but in fact absolutely do not miss. May they forever perish. If-as-expression always!
- solipsism 6y agoFirstly, you can't compare if statements to ternary expressions. To compare apples to apples, you need to compare: 1) if statements with single assignment statements in both branches, assigning to the same variable, and 2) assignments with ternary expressions on the right side. In that apples-to-apples comparison, it's really a toss up which is more readable. Looking at the if statement, anyone who has been reading code for any amount of time is going to immediately recognize the pattern. There's no "scanning two sides".
- stitched2gethr 6y agoSorry, perhaps I don't understand how ternary operators are any different than if; then; else. In fact for readability I very much prefer guard statements that don't include an else at all.
- lmm 6y agoif/else can contain arbitrary control flow (return, break, throw, goto) in the bodies, so you have to read the whole body to know what's going on. Ternaries are just expressions so however complex the body is, you know it's always just going to evaluate to a value.
- knuthsat 6y agoEarly return ifs are great. I usually just transform functions with 2+ if statements into functions without `if` statements by extracting `if` statements into other smaller functions where I can write it with an early return. Looks great.
- progval 6y ago> because the two sides of the ternary are guaranteed to be expressions rather than blocks But expressions can contain blocks. Therefore, this is valid: int main(void) { while (1) { 1 ? ({ break; }) : ({ break; }); } } The right way to easily spot control flow construct is syntax highlighting.