x = if y > 5 { "foo" } else { "bar" }This is just superior to anything else
I honestly can’t see how this is more readable than
x = (y > 5) ? "foo" : "bar"I get that it’s a syntax that needs to be learned, but it’s just so clean and concise!
What I like about using
ifandelsefor that is that you’re already using those keywords for branching in other parts of the code.Though my least favorite is probably Python’s:
x = "foo" if y > 5 else "bar"It just seems backwards to me
While Python’s version does feel a bit backwards, it’s at least consistent with how list comprehensions are set up. They can also feel a bit “backwards” imo, especially when they include conditionals.
List comprehension is another thing I don’t like about Python :)
There’s more of those, but one thing I do like about Python is that I get paid for writing it, so I try not to complain too much
I love list comprehension. Best part of the language, imo. To each their own.
At my previous workplace we had a C macro that was something like
#define CheckWhatever(x__, true__, false__) \ whatever(x) ? (true__) : (false__)I don’t remember this shit, so I’m just paraphrasing cursed C. The question one would ask is… why? Well, because you also want to do
#define CheckWhatever2(x__, true__, false__) \ CheckWhatever((x__ ##1), (true__), (false__)) \ CheckWhatever((x__ ##2), (true__), (false__))And, of course
#define CheckWhatever3(x__, true__, false__) \ CheckWhatever2((x__ ##1), (true__), (false__)) \ CheckWhatever2((x__ ##2), (true__), (false__))Long story short, someone wanted to
CheckWhatever6inside another macro. While debugging code old enough to vote, my editor suggested expanding the macro, which expanded to ~1400 lines for a single ternary operator chain. Fun times!yeah… yikes. c is a beautiful language but thing like these are why macros may be it’s largest blemish. hope that codebase doesn’t keep planes flying!
I have bad news for you
Bah
Ternary is just a compressed if-elseif-else chain with a guaranteed assignment.
If you format it like a sane person, or like you would an if/else chain, then it’s way easier to read than if/else chains.if else chain? believe of or not, straight to jail.
Control structure conditional:
- verbose
- boring
- may result to nothing
Ternary expression:
- terse
- all action
- always leads to a result
I love ternary for assigning to constants.
Don’t you just love the readability
a = a > b ? (b > c ? (a < d ? c : a) : d) : (b < c ? a : d )Weird example. 3 nested conditionals is not the typical use case for a ternary, and 2 of the 5 branches result in a pointless a=a assignment. I agree this is bad code, but it’s just as bad and hard to parss in a normal if-else structure too:
if (a>b) { if (b>c) { if (a<d) { a=c; } else { a=a; } } else { a=d; } } else { if (b<c) { a=a; } else { a=d; } }In another situation, though, it’s perfectly readable to have a much more typical ternary use case like:
a = c > d ? c : dAnd a pair of parentheses never hurt readability either:
a = (c > d) ? c : d


