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?

Latest comments (50)
A single ternary is fine, as long as you don't write down a long line of complicated logic. It's especially joy when providing default values for variables that may or may not have a value.
Screw multi-ternaries though. Even when aligned properly I hate reading those and figuring out what they do exactly.
for me, ternary operator is only good for one expression only.
I like them quite a bit. Like everyone else has said, they're pretty easy to abuse or sneak into a place they don't belong.
I love to use them, a way for me to avoid "If ... else" statement when I want to do one-liner
Absolutely love it for it's simplicity and how widely it's been implemented in almost every programming language.
But I absolutely cannot stand it when someone does something like this:
or this:
or any variation of taking an expression that returns a boolean and immediately transforming that boolean with a ternary operator to return a boolean.
I've had many people tell me that this improves the "readability" of the statement, but I strongly disagree. I think all it does is add redundancy to the code and add more complexity with little benefit.
I agree with you on
return expressionThatReturnsBoolean() ? true : false;. Readability can be subjective though ¯\(ツ)/¯.Readability is subjective, but I would say that when your code reads: if true return true, if false return false... I mean you might as well be writing:
Perhaps.
Readability is also contextual. I might do similar things in two different ways depending on how readable it would be over there. Doing it your way might be my preference, but sometimes I'll find myself using a ternary for the same.
every time someone uses an
elseinstead of a ternary a cute cat dies somewhere.Everything can be written in multiple different ways. The Best Way™ to write something depends on what it is you're trying to write. Sometimes it's much more readable to write a ternary expression, and sometimes it's much more readable to write that same code in an
if, and sometimes in aswitch, and sometimes using a dictionary or whatever other idiom your language of choice offers. The important thing is to (re-)write code in the most readable way whenever you touch it. There are no hard and fast rules. A piece of code may start out as a ternary expression, but when added to must be rewritten into something else to remain readable. Things become unreadable when you leave something as is (e.g. as a ternary expression) while adding to it because you don't feel like rewriting it.Very true.
I think languages need it. I think it's necessary for making the code flow in the same order as how things work in your mind. But I don't think it improves readability except in Lisp (because it's just a regular If statement).
In Lisp (It's been a few years, so forgive me any syntax errors) it's:
In Python what it actually is is:
Whereas I'd like it to be:
I tend to use the conditional operator mostly with one-line functions or properties. The idiomatic syntax is a lot easier in C# (my most used language) for one-line functions using the conditional operator.
New Hotness:
Old and Busted:
It's like an if-else in Scala, that returns a value. Nothing wrong with that, it's just that ? : is not intuitive to read. But if used carefully, and not just showing off how smart you are ;-) it can actually improve readability.