|
|
|
![]() |
|
|||||||
|
||||||||
| View Poll Results: Do you use Ternary Operators in your code? | |||
| Yes |
|
17 | 60.71% |
| No |
|
11 | 39.29% |
| Voters: 28. You may not vote on this poll | |||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 http://cplus.about.com/library/gloss...ryoperator.htm (This is where I got the example) |
|
#2
|
||||
|
||||
|
Re: Do you use ternary operators in your code?
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. Last edited by 10intheCrunch : 03-04-2004 at 00:31. |
|
#3
|
||||
|
||||
|
Re: Do you use ternary operators in your code?
Quote:
Code:
if(y > 5)
x = 1;
else
x = 0;
Code:
x = 0;
if(y > 5)
x = 1;
![]() 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 ?. ![]() |
|
#4
|
||||||
|
||||||
|
Re: Do you use ternary operators in your code?
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:
GetMenu()->CheckMenuItem(IDM_AUTO, (COSSpecific::autoMode ? MF_CHECKED : MF_UNCHECKED)); -Rob |
|
#5
|
||||
|
||||
|
Re: Do you use ternary operators in your code?
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: Code:
(c ? a : b) = d; |
|
#6
|
||||
|
||||
|
Re: Do you use ternary operators in your code?
I try to do whatever is most readable but instead of
Code:
if (y > 5) x = 1; else x = 0; Code:
x = (y > 5); |
|
#7
|
|||||
|
|||||
|
Re: Do you use ternary operators in your code?
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; } |
|
#8
|
||||
|
||||
|
Re: Do you use ternary operators in your code?
Not to mention inefficient with respect to program space.
|
|
#9
|
||||
|
||||
|
Re: Do you use ternary operators in your code?
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) |
|
#10
|
|||||
|
|||||
|
Re: Do you use ternary operators in your code?
Me too. I ussually write them as (A? B : D) because that makes sense to me.
|
|
#11
|
||||
|
||||
|
Re: Do you use ternary operators in your code?
Quote:
Code:
x = y > 5 ? 1 : 0; |
|
#12
|
|||||
|
|||||
|
Re: Do you use ternary operators in your code?
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 Code:
h = ( FW2 - xt ); h = ( x+h < 0 ) ? -x : h; h = ( x+h >= PW )? PW-x-1 : h; Code:
h = ( FW2 - xt );
if ( x+h < 0 )
h = -x;
else if ( x+h >= PW )
h = PW-x-1;
|
|
#13
|
|||||
|
|||||
|
Re: Do you use ternary operators in your code?
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. |
|
#14
|
|||||
|
|||||
|
Re: Do you use ternary operators in your code?
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 |
|
#15
|
|||||
|
|||||
|
Re: Do you use ternary operators in your code?
That's out of a Dilbert, isn't it?
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is your most prefered programming language? | Hailfire | Programming | 156 | 19-01-2005 21:42 |
| heres the code. y this not working | omega | Programming | 16 | 31-03-2004 15:18 |
| Interrupt timer, executing code asap? | SeanCassidy | Programming | 10 | 07-03-2004 01:47 |
| Inserting Naviagation code into Default code? | actorindp | Programming | 3 | 28-01-2004 18:12 |
| Does your team use the Default code. | Jeff McCune | General Forum | 2 | 09-01-2003 14:46 |