Should We Avoid Ternary Operators?

Should We Avoid Ternary Operators?

19th November 2019 - Tuesday
![](https://i0.wp.com/www.aggrippino.com/wp-content/uploads/2021/08/ternary_operator_carbon.jpg?resize=1024%2C512&ssl=1)
I recently read a Facebook post which suggested it was better to use a ternary operator instead of an if…else statement whenever the result would be a single line of code. That doesn’t seem right.

I’ve noticed this trend toward shorter, smaller code, and I understand the appeal. If I can do in one line of code what used to take six lines, it feels more efficient. However, when it comes to executing the code, there’s no performance difference between an if…else statement and a ternary operator.

Code Quality Perspective

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
― Bill Gates

It doesn’t seem very long ago that Software Engineers conflated the number of lines written with the skill of the programmer or the quality of the code… More lines of code meant better code. In Bill Gates’ quote, he’s addressing this misconception, but the point is the difference in perspective from then to now.

Ternary Operator Benefits

Efficiency… but not really
The ternary operator feels like a hack that makes our code more efficient. However, efficiency is most important during execution. During development, the most important thing is readable code.

Smaller code
A web page does load faster if the browser has less to download, but this type of optimization is the job of a minification tool, not a programmer. And it is well known that “premature optimization is the root of all evil“.

Readability… but not usually
Sometimes compressing a complex concept into a single line makes code easier to understand. This is the only time the ternary operator should actually be used. It’s easy to abuse the ternary operator, though, resulting in unreadable code.

Ternary Operator Facts

In favor of ternary operators

In favor of if…else statements

Conclusion

Of course we shouldn’t stop using ternary operators! On either side of the debate, there’s an argument for improving readability of code. So, use it when it’ll improve readability. But there are more reasons to use if…else syntax. We should avoid ternary operators, but it’s a guideline, not a rule. And even when considering rules, we must remember that “A foolish consistency is the hobgoblin of little minds“.