Thursday, June 26, 2014

I Love the Ternary Operator

I am a software developer by trade, and true to Larry Wall’s three great virtues of a programmer I am full of laziness.
Laziness is defined as:
The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful, and document what you wrote so you don't have to answer so many questions about it. Hence, the first great virtue of a programmer. Also hence, this book. See also impatience and hubris. (p.609)[1]
It’s things like these that drive me towards finding more optimal solutions to my programming problems. One of these solutions I find myself using over and over again is the ternary operator.
In programming, we have things called “operators.” True to their name, they operate upon their input. There are unary operators, such as a minus sign before a number:

-2
There are binary operators, such as a plus sign between two numbers:

2 + 2
And then there is the ternary operator, or ?:, which is used in place of if-else:

obj != null
    ? obj.Foo()
    : throw new CannotFooException()
    ;
This is the same as writing:

if (obj != null) {
    obj.Foo();
} else {
    throw new CannotFooException();
}
Now, you may be wondering, “Why do I need to use this Perlish syntax to do something I’m doing fine already?” Well, let’s me show you where the true power is…
Have you ever written something like this?

string x = "Your answer is  ";
if (y == 1) {
    x += "one";
} else {
    x += "not one";
}
x += ".";
Console.WriteLine(x);
With the magic of ternary operators, you could have instead written:

string x = "Your answer is  "
    + (y == 1) ? "one"
               : "not one"
    + "."
    ;
Console.WriteLine(x);
(I broke it onto multiple lines. This could all be done one one, but I think this way is more readable)
If you’re dealing with a more complex example, like this:

string x = "Your answer is  ";
if (y == 1) {
    x += "one";
} else if (y == 2) {
    x += "two";
} else if (y == 3) {
    x += "three";
} else if (y == 4) {
    x += "four";
} else {
    x += "hrair";
}
x += ".";
Console.WriteLine(x);
(yeah, I could have used a switch here, but I’m trying to keep the concept focused; also we’re assuming that y > 0)
You could use the ternary to make it look like this:

string x = "Your answer is  "
    + (y == 1) ? "one"
    : (y == 2) ? "two"
    : (y == 3) ? "three"
    : (y == 4) ? "four"
               : "hrair"
    + "."
    ;
Console.WriteLine(x);
Doesn’t that just make everything a whole lot cleaner?
The best part is, you can use the ternary where you can’t use if-else. So, if you want to not declare a new variable to hold a variable, you could just drop the ternary into the thing building the string and not have the (albeit miniscule) overhead of declaring a variable. I found this to be of great use when writing some Razor code that would have otherwise depended on a declared variable.
Check out the Wikipedia article on ?: to see what languages support it. You’ll soon find that the languages that are not on this list are ones you’re less and less likely to program in.
So, what are your feelings on the ternary? Love it? Hate it? Indifferent? I want to know!

[1] Wall, Larry. Jon Orwant. Tom Christiansen. Programming Perl, Third Edition. July 2000.

No comments: