Did You Know About The Ternary Operator?
Welcome to Colour Me Cocoa.
Looks like you're new here. If you like what you see, you may want to subscribe to my RSS feed, or if you prefer sign up to receive email updates.
Thanks for visiting!
I just found out about this operator while working through Brian Christensen’s excellent Write a Screen Saver tutorial.
radius = rect.size.width <= rect.size.height ?
rect.size.width / 2 : rect.size.height / 2;
Weird looking isn’t it? Here’s the simplified version:
(CONDITION) ? EXPRESSION1 : EXPRESSION2;
How Does It Work?
If the CONDITION statement evaluates to true then EXPRESSION1 is run, if the CONDITION statement evaluates to false then EXPRESSION2 is run. You can think of it as a compressed if-else statement.
How Has Brian Used It?
Brian has used the question mark operator to assign a value to radius based on the evaluation of rect.size.width <= rect.size.height.
radius = CONDITION ?
EXPRESSION1 : EXPRESSION2;
We could re-write Brian’s code with an if-else statement.
if (rect.size.width <= rect.size.height)
{
radius = rect.size.width / 2;
}
else
{
radius = rect.size.height / 2;
}
It does the same thing, but uses more lines of code. Whether it is easier to read or not is a matter of personal opinion, but knowing about its existence can only be a good thing.
You never know when you will have to maintain someone else’s code.
April 8th, 2007 at 5:50 am
Yep. I’ve been using this since my javascript days back in primary school, though it has become more prolific in recent times. It’s a good timesaver, but as you said, it’s a matter a personal opinion.
For a real nightmare, you can nest these statements.
direction = (lastTab < toTab) ? (toTab < noTabs) ? CGSUp : CGSRight : (toTab > 0) ? CGSLeft : CGSDown;I pity the guy having to maintain code I’ve written during a bout of laziness. Unless you’re used to these statements and can read them straight off (it’s not that hard, it just requires practice to get some speed), the best way to figure these out is to rewrite it in standard if / else form.
April 8th, 2007 at 6:46 pm
Nightmare is right. That code looks awefull.
I think I’ve managed to disect it…
April 15th, 2007 at 7:48 pm
As a style quibble, I usually put the ? and : symbols at the start of the line:
April 15th, 2007 at 7:49 pm
…though I do indent it.
*shakes fist at the pre tag*
April 15th, 2007 at 7:56 pm
Fixed that up for you Peter.
You make a good quible though, indentation can help greatly when trying to read code.
But again, it’s all personal preference, like the use of the ternary operator in the first place.
April 30th, 2007 at 3:51 pm
The ternary operator can also be optimised in to a single conditional move call (cmov in x86), so there’s other advantages to it too. A good compiler should be able to take advantage of that however you write it, but no harm in making your intentions clearer.
However sometimes it’s better to maintain code clarity rather than brevity, so the ternary operator isn’t always desirable.