“Clarity over cleverness.”
— Someone, somewhere, talking about what makes a good communicator
(and for that matter, a good developer)
As developers, we are communicators. At first glance, it may not seem apparent, but we are simultaneously sharing instructions with a computer to execute, and leaving breadcrumbs of our thought process for the next person to jump in.
Learning new ways of articulating problems or arriving at solutions is exciting, and can expand our mindset the next time we encounter similar (and drastically different) situations. It’s important to remember though, that we’re not just telling the computer what to do and when — another person will eventually touch our code, and need to understand what’s going on.
There are a number of different ways to handle conditional situations, but there is no “one size fits all” approach. Conditional situations can be treated like a fork in the road :
if A happens
then do BORif A happens
then do B
otherwise
do C
or a flow-chart :
regarding D
if it looks like E
do F
if it looks like G
do H
if it looks like I
do J
if it doesn’t look like any of those
do K
What happens when you have a basic yes/no question? You can absolutely use the first way, the “fork in the road approach” — called an if/then statement, or an if/else when you have two conditions (if X do this, else do that). Sometimes, the condition you are checking for can be answered as simply as “yes” or “no”, and that can be a great use for the ternary operator.
The ternary operator is represented like so:
condition_you_are_checking ? if_yes : if_no
Clean and simple. However, it’s not exactly clear. So let’s dig in.
The “ternary” part of the name comes from the three arguments or pieces to the puzzle:
- The condition
- The “if yes” part : what should happen if the condition is true
- The “if no” part : what should happen if the condition is false
The way you can distinguish between each argument is their location in relation to the punctuation marks in the ternary operator’s statement : the question mark (?) and the colon (:).
Let’s look at the statement again:
condition_you_are_checking ? if_yes : if_no
Whatever code is written on the left side of the question mark is the condition you’re checking for, which can also be seen as the question you’re asking. Luckily for many left-to-right based languages, this looks very similar to how we write questions normally — and a great point to remember that our programming languages are meant for people to understand too!
The way that I remember what is happening on the right side of the question mark also draws on regular life/non-programming yes-or-no questions. Take for example, the question: “Are you hungry?”
Written out in a ternary operator, this looks like the following:
Are_you_hungry ? yes_please_make_food : no_thank_you
The “if yes” answer will always be the first answer-side argument evaluated. When I say “yes, I’m hungry”, it doesn’t matter to me what could happen if I am not. Simply put, if I say yes, problem solved — food is on the way.
In order to get to the “if no” part of the answer, I have to first work my way through the “if yes” part. If I say no, I know what would have happened if I said yes (food) AND what is going to happen because I said no.
And just like that — the smoke clears!
Ternary operators are often used as a shortcut for the if/else conditions, because they look cleaner. Hopefully, with this breakdown, you too will feel comfortable both using the ternary operator, and when reading code where others have used it before you.