Do you use ternary operators in your code?

Did anyone use Ternary operators in their code this year? Personally, I find them a little bit…odd. I figure using an if-then statement that says:

if(y > 5)
x = 1;

is easier to read and understand, than:

x = ((y > 5) ? 1 : 0);

But, that is just one man’s opinion. What do you all think?

For some more info on Ternary Operators, Binary Operators, and Unary Operators, see

(This is where I got the example)

I really do like ternary operators, for a variety of reasons. First, I do find it easier to read in some circumstances (and it looks kinda cool), but more importantly, its useful for the macros that we use in our code. One example:

#define GetVal(leftVal, rightVal) autoSide ? leftVal : rightVal

This lets us program our autonomous modes for each side into one program. The values are different (obviously) for each side, and this saves us from having to have two entire functions for left/right (important when you are running out of space). While you can write this code into a seperate function, it’s more useful as the macro (as function calls take up a considerable amount of space too).

We also use ternary ops in our Min, Max and Min/Max macros. Very handy.

A warning though: ternary operators themselves take up a lot of space in assembly, even though it’s only one line of code (this makes sense). Using them too frequently can quickly run you out of space (try and use it once to set a value instead of everywhere because it’s easy to write).

One weird thing that we noticed when tight on space: when the ternary op was in a function, it took up more space than when it was in a macro, but moving other small function calls (that didn’t include ternarys) were actually much larger when in a macro. Check the map and proceed with caution.

Just as a warning, those aren’t completely equilivant. For them to be the same, you should do:

if(y > 5)
x = 1;
else
x = 0;

Or:

x = 0;
if(y > 5)
x = 1;

Just pointing that out.

Personally, I think that the extra else clause is worth it in terms of readablitiy. When I’m reading other’s code, I find it hard to spot terenary operators, as they are on one line and it is only noted by a : and a ?.

Maybe it’s just the fact that I like functional programming much more than imperative, but I find myself using ?: all over the place, especially within arguments to function calls. For example, here’s a line out of RoboEmu:

-Rob

note: I do not do any robot programming so I assume “your code” here means just any code at all.

Things begin looking even more usefull with things like this:

(c ? a : b) = d;

I didn’t really use this operator much until I had to learn scheme for school once. Afterwards I was really annoyed that some things are much easier to do in functional programming but are harder in languages like C. As suggested by rbayer, this operator gives one a slight taste of a functional programming in some cases.

if (y > 5)
x = 1;
else
x = 0;

I would use

x = (y > 5);

I also perfer to use x = (y > 5);
it just seems like such a waste to type
if (y > 5){
x =1;
} else {
x = 0;
}

Not to mention inefficient with respect to program space.

I use them all the time, just becuase I think it looks cool

Well,I use them for assignment more than anything because I think its more readable that way and simpler to type (in my eyes at least)

Me too. I ussually write them as (A? B : D) because that makes sense to me.

yes, just remember that not all compilers treat logical TRUE as 1. it’s fine to use it as long as you know it works. more robust, however, is the ternary:

x = y > 5 ? 1 : 0;

If you’re worried about speed, consider that some compilers may give faster code for one idiom over another. Once, I was working on the inner loop of a 2-D image filtering program, so there were four nested loops, which gave width x height x filterWidth x filterHeight loops per image frame, or over 3 million iterations for a 720x480 image and a 3x3 filter – so I spent some time tweaking it for speed.

I was surprised to learn that this

h = ( FW2 - xt );
h = ( x+h  < 0 ) ?     -x : h;
h = ( x+h >= PW )? PW-x-1 : h;

was slower than this

h = ( FW2 - xt );
if ( x+h < 0 )
h = -x;
else if ( x+h >= PW )
h = PW-x-1;

Perhaps it was faster because it only took one branch at a time, whereas the ternary method always executed both expressions. Or maybe it was faster because the compiler wasn’t very good. These days speed isn’t often an issue, and I haven’t come close to the 26.2 ms budget. But when speed is an issue, consider this.

It was probably the else that did it. But good point.
One of our mentors does AMX programming for a local TV group. Aparently, else ifs slow it WAY down.
Besides, Ternaries are probably compiled into Ifs.

I like them because one of our animators also knows some programming so he likes to go into my code and try to change it. So whenever I can, I use Ternaries because they look very cryptic, thereby enhancing my feeling as a programmer and confusing the bejezus out of the animator who eventually gives up because he claims his head is going to explode. hehehe…

-Kesich

That’s out of a Dilbert, isn’t it?