4 ms·
> Combinations of if/else can seriously damage readability and maintainability. Yes, that's why for selections we usually use switch/case. But of course every
by deng 7y ago
> Combinations of if/else can seriously damage readability and maintainability.
Yes, that's why for selections we usually use switch/case. But of course every programmer worth his money just knows that ?: is right associative and that '&&' is on precedence level 11 while '?:' is on 13. If that's not "clever code", I don't know what is...
- illys 7y agoNever thought the ternary operator required high expertise for associativity/precedence reasons and could sentence me to "clever code"... Besides switch/case do not apply to a mix of true/false conditions but to the different values of a single variable. Funny :-) Thanks.
- deng 7y ago> Never thought the ternary operator required high expertise for associativity/precedence reasons If you chain or nest them. > Funny :-) I'm glad I could brighten your day a little bit.
- jlokier 7y ago> every programmer worth his money just knows that ?: is right associative I would hope every programmer worth their money are very comfortable with it, because chaining like this is a very common syntactic pattern in other people's code: cond1 ? val1 : cond2 ? val2 : cond3 ? val3 : other It is made less readable if you turn it into a right-nested mess of parentheses, so that is rarely seen. Just as nobody thinks of 'if..else' as right associative, but it actually is, and everyone uses that fact without thinking about it. Same for '?:'. The associativity is just a formality that nobody thinks about. > '&&' is on precedence level 11 while '?:' is on 13 On this I agree. I routinely put parentheses around complex conditions on the left side of ternaries for this reason. I do remember the precedence, but I think leaving the parentheses out can raise a little doubt in the reader's mind, because it is common to use logical operators for control flow in languages like JavaScript, and control flow is at the same conceptual level as '?:'.
- coldtea 7y ago>because chaining like this is a very common syntactic pattern in other people's code If it's very common where you work, then run, don't walk, away...
- jlokier 7y agoNo, it's very common in code people will encounter if they read a wide range of other people's code outside work. If you haven't encountered it often enough that it's familiar, than I think you probably don't read much code outside a small bubble.
- ergothus 7y ago"readability" is largely a matter of familiarity, so it's very hard to make such sweeping statements accurately. The difference between "common idiom" and "unreadable mess" is "common", not the code itself.
- kdelok 7y agoStrongly agree with this. My company has its own coding standards and openly acknowledges that they are not objectively "the right way to do it", because such a thing does not exist. Instead, they define rules to make our code (relatively) safe and consistent. The Linux kernel has another set of rules that differ in numerous ways (e.g. not wrapping single-line statements under a conditional in braces). Their rules are not strictly better or worse, just different and define their set of common idioms. Another thing that the MD said to me during my interview was "code ought to be boring, testing can be interesting".
- mayoff 7y agoThis came up recently on the Swift Evolution forum (which is the official forum for discussing changes to the Swift programming language). Dave Abrahams said, “[…] I keep meeting experienced programmers (really smart people!) that have no trouble reading [a chain of if/else statements] and yet are confused by the analogous ternary construction: [a chain of ?: expressions]”. https://forums.swift.org/t/pitch-if-else-expressions/22366 https://forums.swift.org/t/pitch-if-else-expressions/22366