4 ms·
Imho the problem with the ternary isn't the concept but the literal ?: symbols used for it. So many languages use clear words like "if" and "then" and "else" a
by Pxtl 6y ago
Imho the problem with the ternary isn't the concept but the literal ?: symbols used for it.
So many languages use clear words like "if" and "then" and "else" and parens and curly braces for conditional statements...
But then they go full Perl and mash their faces against the keyboard when developing syntax for conditional expressions.
It's bone-headed language-design that C did wrong and everybody else continues to do wrong because C.
- chihuahua 6y agoYes, the : and ? are single characters that are easily lost/overlooked among the rest of the code on that line. Even worse when there are nested ternaries.
- RHSeeger 6y agoWhen I find a place where a complex ternary is probably the best choice, I'm a big fan of the following format, because I find it clear that each line is a new condition myvar = condition_1 ? value_1 : condition_2 ? value_2 : condition_3 ? value_3 : value_fallback;
- dxdm 6y ago> where a complex ternary is probably the best choice Under what circumstances would that happen? I've never encountered a nested ternary that wouldn't be better replaced with readable code, so now I'm wondering what experiences I've been missing. :)
- kaoD 6y agoReadability is in the eye of the beholder.
- dxdm 6y agoYes, good point. Still, when working on a shared codebase, it's good to take more potential beholders into account. I suppose if there is a common, consistent style that you can expect everyone to be familiar with, you can get used to reading a sensible amount of chained ternaries.
- RHSeeger 6y agoI don't have an example handy, but it's useful in exactly the same cases where you would use a Lisp "cond" construct; testing a bunch of conditions one after the other. Using an "if / else if / ... / else" construct can do the same thing, but it can be less clear sometimes (both to intent and because it takes up more screen real estate, which makes understanding the overall code harder).
- Pxtl 6y agoAnd, to illustrate my point, hypothetical "if statement is also conditional expression" syntax, to illustate how much more readable "ifs" are than "?:": myvar = if(condition_1) {value_1} else if(condition_2) {value_2} else if(condition_3) {value_3} else {value_fallback};
- RHSeeger 6y agoWhile I agree that "if statements are expressions" is a nice feature, I disagree that your version is more readable. That being said, it's readable enough that, if the rest of my team preferred it, I'd be ok using it.
- jarfil 6y agoI've been messing around with some code that uses: #define TERN(c, a, b) c ? a : b I'm not a fan of ternaries, but that macro makes it kind of more ligible.
- wbl 6y agoYou may wish to look at the Bourne shell source for other macros to make C readable.
- aarchi 6y agohttps://news.ycombinator.com/item?id=22191790 https://news.ycombinator.com/item?id=22191790
- Liquid_Fire 6y agoI don't advocate the use of this macro, but if you do use it you should really wrap the parameters and the whole expression in parentheses to avoid precedence issues: #define TERN(c, a, b) ((c) ? (a) : (b))
- sophistc 6y agoExactly, this is C macro 101.
- MaxBarraclough 6y agoThat isn't more readable. It's non-standard. It also looks like a function call, but doesn't behave like one (in a function call, a, b, and c would all be evaluated). It's going to confuse other C programmers. As Liquid_Fire points out, it's also probably incorrect.