The conditional (ternary) operator
Samuel A.
Posted on April 3, 2019
Breaking the ice
For those of us that never heard of it, what is clear about the conditional (ternary) operator, or conditional operator, or ternary operator, or even c24r, is that it has a looooong name. But what is it exactly, and how and when to use it?
The operator comes as a compact syntax meant to simplify conditional code (if-then-esle), and is available in lots of programming languages. But like everything powerful, it must be used with responsibility, at the risk of loosing its intended simplification purpose.
As the name implies, the operator takes three arguments: a condition, and two results, the first when condition is true and the other when false, and is used in assignments. Enough theory, give me some code!
When to use it?
Let's review the following code:
<?php
if ($a > $b) {
echo "a is greater than b";
}
else {
echo "a is lower than b";
}
?>
Simple enough. First, let's reshape it to isolate the logical components:
<?php
$isGreater = $a > $b;
if ($isGreater) {
$word = 'greater';
} else {
$word = 'lower';
}
echo "a is $word than b";
?>
This code is a typical candidate for the conditional operator: an assignment that depends on a condition. Let's refactor:
<?php
$isGreater = $a > $b;
$word = $isGreater ? 'greater' : 'lower';
echo "a is $word than b";
?>
Already looking good, but we can do better:
<?php
$word = $a > $b ? 'greater' : 'lower';
echo "a is $word than b";
?>
We gained one line, but at the cost of the explicit condition semantics. Here we can already feel the tradeoff: compact vs readable. Let's move forward.
When not to use it?
Going further is tempting. We could simplify even more to get a great one liner:
<?php
echo 'a is '. ($a > $b ? 'greater' : 'lower') . ' than b';
?>
But that's where we need to stop. Here too much is lost and we are corrupting the original purpose of simplification. Indeed, even though the code seems simpler, as everything stands on one line, it has become less explicit/readable, and one must go over it several times to understand its meaning.
Wrap up
We've seen how the conditional operator can simplify the code, but also make it less readable when abused. Code is not meant to be compact, that's a compiler's responsibility, but rather readable, as targeted to humans, and thus easier to maintain.
So, are one liners really to ban? No. The correct balance is to limit their use to assignments, and sometimes to postfixes:
<?php
//assignments
$word = $a > $b ? 'greater' : 'lower';
$colspan = $row > 0 ? 5 : 2;
$backgroundColor = $row % 2 == 0 ? '#E3E3E3': 'none';
//postfixes
$formattedValue = $value . ($isPercent ? '%' : '');
$amount = $value . ($isEuro ? '€' : '$');
?>
What about you? Are you a conditional (ternary) operator addict? Are you just discovering it? Do you have a special magic code trick? Is this post making you angry? Please share your experience and feelings in the comments!
Posted on April 3, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.