Go to Post How many robotics kids does it take to turn on a light? - demosthenes2k8 [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
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

Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 02-04-2004, 22:48
NotQuiteFree's Avatar
NotQuiteFree NotQuiteFree is offline
Do the WAAAAVE!!
#0957 (Watson)
Team Role: Programmer
 
Join Date: Dec 2003
Location: Albany, Oregon
Posts: 22
NotQuiteFree will become famous soon enough
Send a message via MSN to NotQuiteFree
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)
__________________
Yo.
  #2   Spotlight this post!  
Unread 02-04-2004, 23:28
10intheCrunch's Avatar
10intheCrunch 10intheCrunch is offline
Who's John V-Neun?
AKA: Alex Baxter
None #0254 (Cheesy Poofs)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: San Jose, CA
Posts: 129
10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough
Send a message via AIM to 10intheCrunch
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.
__________________
~Alex Baxter
Programming, Arms operation, Team 254

Last edited by 10intheCrunch : 03-04-2004 at 00:31.
  #3   Spotlight this post!  
Unread 03-04-2004, 07:55
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: Do you use ternary operators in your code?

Quote:
Originally Posted by NotQuiteFree
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);
Just as a warning, those aren't completely equilivant. For them to be the same, you should do:

Code:
if(y > 5)
    x = 1;
else
    x = 0;
Or:
Code:
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 ?.
__________________

  #4   Spotlight this post!  
Unread 04-04-2004, 21:22
rbayer's Avatar Unsung FIRST Hero
rbayer rbayer is offline
Blood, Sweat, and Code
no team (Teamless Orphan)
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Minnetonka, MN
Posts: 1,087
rbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of lightrbayer is a glorious beacon of light
Send a message via AIM to rbayer
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
__________________
New C-based RoboEmu2 (code simulator) available at: http://www.robbayer.com/software.php
  #5   Spotlight this post!  
Unread 04-04-2004, 21:51
piotrm's Avatar
piotrm piotrm is offline
Registered User
AKA: Piotr Mardziel
FRC #0190 (Gompei and the HERD)
Team Role: College Student
 
Join Date: Dec 2002
Rookie Year: 2001
Location: Dudley, MA
Posts: 96
piotrm has a spectacular aura aboutpiotrm has a spectacular aura about
Send a message via AIM to piotrm
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;
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.
  #6   Spotlight this post!  
Unread 04-04-2004, 22:39
JJG13's Avatar
JJG13 JJG13 is offline
Yoda
AKA: Joshua Graffman
None #0180 (SPAM)
Team Role: Alumni
 
Join Date: Dec 2002
Rookie Year: 2003
Location: Martin County, Florida
Posts: 105
JJG13 has a spectacular aura aboutJJG13 has a spectacular aura aboutJJG13 has a spectacular aura about
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;
I would use
Code:
x = (y > 5);
__________________
Behold the power of SPAM.
  #7   Spotlight this post!  
Unread 05-04-2004, 00:13
wun's Avatar
wun wun is offline
Registered User
AKA: David Wolever
#0865 (Warp7)
Team Role: Programmer
 
Join Date: Dec 2003
Rookie Year: 2001
Location: Toronto
Posts: 84
wun will become famous soon enough
Send a message via AIM to wun Send a message via MSN to wun
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;
}
__________________
Help Microsoft stop piracy. Use Linux!

I have three gmail invitations, message me if you want one
  #8   Spotlight this post!  
Unread 05-04-2004, 00:44
10intheCrunch's Avatar
10intheCrunch 10intheCrunch is offline
Who's John V-Neun?
AKA: Alex Baxter
None #0254 (Cheesy Poofs)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: San Jose, CA
Posts: 129
10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough10intheCrunch is a jewel in the rough
Send a message via AIM to 10intheCrunch
Re: Do you use ternary operators in your code?

Not to mention inefficient with respect to program space.
__________________
~Alex Baxter
Programming, Arms operation, Team 254
  #9   Spotlight this post!  
Unread 05-04-2004, 19:41
pickle's Avatar
pickle pickle is offline
Talking vegtable
AKA: Jeremy
#1296 (Full Metal Jackets)
Team Role: Programmer
 
Join Date: Apr 2004
Rookie Year: 2004
Location: Rockwall,TX
Posts: 42
pickle is on a distinguished road
Send a message via AIM to pickle
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)
__________________
2004 Rookie Inspiration Award winner - Midwest Regional
2004 Autodesk Visualization Award winner - Lone Star Regional
2004 Quarterfinalist - Lone Star Regional
2005 Team Spirit Award - Lone Star Regional
  #10   Spotlight this post!  
Unread 06-04-2004, 13:28
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
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   Spotlight this post!  
Unread 06-04-2004, 20:52
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Do you use ternary operators in your code?

Quote:
Originally Posted by JJG13
I try to do whatever is most readable but instead of
Code:
if (y > 5)
  x = 1;
else
  x = 0;
I would use
Code:
x = (y > 5);
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:
Code:
x = y > 5 ? 1 : 0;
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
  #12   Spotlight this post!  
Unread 06-04-2004, 22:00
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
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;
was slower than this

Code:
  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.
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.
  #13   Spotlight this post!  
Unread 06-04-2004, 22:04
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
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   Spotlight this post!  
Unread 06-04-2004, 22:32
Anthony Kesich's Avatar
Anthony Kesich Anthony Kesich is offline
Programmer Turned Engineer
AKA: SonKenshin
#1097 (Site 3 Engineernig)
Team Role: Leadership
 
Join Date: Jan 2003
Rookie Year: 2003
Location: Sacramento, CA
Posts: 186
Anthony Kesich has a spectacular aura aboutAnthony Kesich has a spectacular aura about
Send a message via AIM to Anthony Kesich
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
__________________
C is screwing up my English--I'm ending all my sentences in semi-colons;

Horray for most things! -George Carlin

"Sure, I'll play rugby. Besides, I have a lot of experience running from really big guys who want to cream me." -Me, Freshman year
  #15   Spotlight this post!  
Unread 07-04-2004, 11:21
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: Do you use ternary operators in your code?

That's out of a Dilbert, isn't it?
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 15:45.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi