The Trouble with Ternaries

Christina Burger
3 min readMay 13, 2018
Image credit: https://tinyurl.com/y9oj5pvz

I remember seeing a ternary in a friend’s code and thinking how impressive it looked, and how I would never be able to understand it. A couple years on I’d caution against using it in all but a few cases.

The ternary takes its name from the fact that it’s an operator that takes 3 arguments (ter = 3, like bi = 2). In many languages it’s the only operator that takes 3 arguments, so it’s called the ternary. A better name is also conditional operator, or my favourite: the inline if.

Say you were writing some code for a subscription where members pay less for some service. If you were to use a regular old-fashioned if statement it would look something like this:

var fee;
if (isMember) {
fee = "$2"
} else {
fee = "$10"
};

Let’s say you want to fancy it up with some magic using a ternary operator:

var fee = isMember ? "$2" : "$10"

Hey wow! Now it’s just one line! We should use these everywhere!

Hold on a minute… I will admit the first example is much longer and the second example makes you look cool. Here are some reasons why you should think twice about using a ternary.

It’s hard to read

If you take the first example you can say it out loud in words and it makes sense. After all code, humans write and read code, so it should be human readable. Using words like “if” and “else” makes it more readable. Any code that makes other programmers have to stop and think is not “good code”.

It’s got a learning curve

Overly using ternaries makes it more daunting to new programmers. They may also make the wrong conclusions about them. Yes they should learn, and yes they will need to know it at some point. However, I’d still favour using a regular if..else where possible, not just for newcomers, but for the sake of simplicity.

It doesn’t add anything to a language

Rust, winner of the StackOverflow 2018 survey’s “most loved language”, doesn’t have a ternary. In fact they’ve added a very elegant looking let if.

It can be abused

We all know that one person who’s going to go and write the most complex nested ternary they can. Of course we all have code reviews and linting and all the other wonderful things that should catch this. However, it’s easy in our darkest moments to expand on a simple ternary and turn it into a hieroglyphic looking thing like this:

var fee = isMember ? hasCoupon ? "$1" : "$2" : isFirstTime ? "$8" : "$10"

So when should you use one?

When it’s a very simple check, where each argument does not have any nested logic.

It’s also useful for templating languages, to put quick bits of logic into, for example an HTML page:

<form>
<input type='radio' name='gender' value='m' {gender == 'm' ? "checked" : ""} /> Male
<input type='radio' name='gender' value='f' {gender == 'f' ? "checked" : ""} /> Female
</form>

Like most things, it’s a tool in your toolbox. Use it when it fits the problem, but don’t try to apply it to everything. You will also be happy you have it, when you need it.

--

--