DEV Community

Shreyas Minocha
Shreyas Minocha

Posted on

What do you think about the ternary operator?

Shortcuts

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)

Collapse
 
mah-rye-kuh profile image
Marijke Luttekes

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.

Collapse
 
bossajie profile image
Boss A • Edited

for me, ternary operator is only good for one expression only.

Collapse
 
drewknab profile image
Drew Knab

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.

Collapse
 
adekoder profile image
adekoder

I love to use them, a way for me to avoid "If ... else" statement when I want to do one-liner

Collapse
 
jvarness profile image
Jake Varness

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:

return expressionThatReturnsBoolean() ? true : false;

or this:

return !expressionThatReturnsBoolean() ? true : false;

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.

Collapse
 
shreyasminocha profile image
Shreyas Minocha • Edited

I agree with you on return expressionThatReturnsBoolean() ? true : false;. Readability can be subjective though ¯\(ツ)/¯.

Collapse
 
jvarness profile image
Jake Varness • Edited

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:

if (expressionThatReturnsBoolean() == true) {
  return true;
}
else {
  return false;
}
Thread Thread
 
shreyasminocha profile image
Shreyas Minocha

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.

Collapse
 
vikkio88 profile image
Vincenzo

every time someone uses an else instead of a ternary a cute cat dies somewhere.

Collapse
 
deceze profile image
David Zentgraf

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 a switch, 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.

Collapse
 
shreyasminocha profile image
Shreyas Minocha

Very true.

Collapse
 
cathodion profile image
Dustin King

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:

(setq variable (if condition something somethingElse))

In Python what it actually is is:

variable = something if condition else somethingElse

Whereas I'd like it to be:

variable = if condition something else somethingElse
Collapse
 
jdsteinhauser profile image
Jason Steinhauser

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:

int Max(int a, int b) => a > b ? a : b;

Old and Busted:

int Max(int a, int b) {
    if (a > b) {
        return a;
    }
    return b;
}
Collapse
 
soerenfandrich profile image
Sören Fandrich • Edited

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.