Ternary Operator Guidelines

Short conversation at the office today. I declared that I wasn’t a big fan of ternaries and that I typically used them only for simple one line statements and almost never in returns and never nested. One of the developers challenged me on why the ternary operator is frowned on and I admitted I didn’t have much in the way of data. After some digging on the interwebs.

Github’s style guide has the most common approach to the ternary:

“Avoid the ternary operator (?:) except in cases where all expressions are extremely trivial.”

Github style guide

Support for using ternary’s in return statements is generally supported as well. There is general agreement that nesting ternaries is a bad practice. Static analyzers like Rubocop and Checkstyle include checks for nested ternary operators.

Historically ternary operators bothered me because I missed them sometimes when scanning through code. If and else are hard to miss on a quick scan. I’ve adjusted to using them for one simple one liners as the terseness is nice. Nesting them strikes me as abuse, much like single letter variables or predicates methods that have side effects.