The ternary operator has a reputation of reducing readability. However, in the right hands, it may result in less duplication.
Do you think it's use has enough of a negative impact on readability? Are there enough legitimate use cases do justify the potential misuse?
How often do you use it? Have you seen it used in contexts where it aids readability?
Do you think modern programming languages should come with it? Users of languages without the ternary operator (golang etc), do you miss it?

Oldest comments (50)
I feel the same as other "syntax sugar cool" things, when used correctly it improves your code, but most of the time it's going to be over used. I've seen some horrors using the ternary operator you would not believe.
what = (f() ? (g() && x ? a + d : b ) : c)I used it when dealing with simple, 1 word condition and values.
As a Go newbie I don't miss it, having so few language constructs and operators it is very relieving, you need a condition you use
if, simple and efficient. All your peers will do the same, everyone understands it, and it is harder to screw it up.I agree. I've seen it used in some perfectly legitimate ways such as in JSVerbalExpressions.
This pattern is repeated throughout the library. Using
if-elsehere would be painful, but it'd be manageable. At least one wouldn't have to deal with the code like you described. Why people would write code of that sort (except for codegolf) is beyond me.The most common reason I saw (is not specific to this problem) was when a dev had to add some new logic, instead of refactoring the ternary expression into an
if,switchor separate to functions, or objects or whatever added the new complexity in place.The reasons why that happened were various, excuses like "I was in a hurry", "I didn't had the time to understand the code" and so on were common.
Fear of refactoring is a common problem in projects without tests and/or devs without the desire to write good code, or just insane release schedule, you know, common problems.
I actually like the Kotlin syntax that uses the
if/elsekeywords instead of?/:It's much more readable than the JS syntax.
However, I often use ternary operators in both languages as they are just quick to write und don't mess up the code with an if/else clause.
That's interesting. I didn't know some languages did it that way.
F# does it too.
This is valid in all languages where
if elseare an expression instead of statement: Python, Rust, Scala, Kotlin, Haskell,F#....Iirc if you surround the if/else statement in parentheses then it is valid in JavaScript as well and will assign the appropriate value to the variable
Can you post an example ? I only get syntax error :/
Totally wrong. Statement vs expression. Assignment without mutation.
I agree with the others here that they're nice for simple conditions but should be used sparingly. They're particularly good in simple return statements.
Proper formatting goes a long way at improving readability:
Interesting take.
I'd tend to just method extract this, and use explicit return statements inside plain if-statements.
At least in part, that's because the conditions will rarely be as brief (when they are thus numerous).
Wow, Idan, I love your formatting. I’ve not seen a ternary formatted this way, and it really does improve readability!
Thanks for the great tip!
Most modern compilers are smart enough to optimise control statements. It is more readable than it otherwise would be, but why do it in the first place?
I tend to go a bit further and indent the two expressions:
In it's simple use, the ternary operator is rather helping than hindering the reading flow, as illustrated by the example:
However, in golfed or minified code, one usually finds abominable abuses of the ternary operator within already complicated statements that reduce readability. Only use them there, never the code you work on.
I'm a big fan of ternaries in JS, because IMO expressions are better than statements for reducing cognitive load, which is generally my primary goal when coding.
However, I find that my personal favourite formatting for ternaries is:
To be clear though, I do believe that
if/elseis better for readability (especially for less experienced developers), and I prefer languages which only have expressions (and consequently their if/elses are expressions too). But for languages that aren't as nice as that, I always prefer ternaries for assignments.I love ternaries and think they improve readability but:
I like ternaries very much. Sometimes when options are longer i go with folowing formating:
Sadly it's needed in all the languages that don't have if-expressions.
But I have the feeling, newer language designs include if-expressions instead of if-statements more often.
Love it, use it all the time.