Go to Post FIRST will continue searching for rules and procedures to keep us from hurting ourselves. However, none of this will substitute for judicious application of common sense. - Richard Wallace [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

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #10   Spotlight this post!  
Unread 04-04-2010, 23:58
FRC4ME FRC4ME is offline
Registered User
FRC #0339
 
Join Date: Feb 2008
Rookie Year: 2007
Location: Fredericksburg, VA
Posts: 324
FRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant futureFRC4ME has a brilliant future
Re: Ternary operators

I use these all the time. In my opinion, they make code more readable (so long as the person reading it knows what they are) and clarify the intent of what you are doing in many places.

Some places I like to use them include when the arguments to a method call are dependent on a condition:

Code:
wpiRelay.set(isOn
    ? edu.wpi.first.wpilibj.Relay.Value.kOn
    : edu.wpi.first.wpilibj.Relay.Value.kOff);
when the return value of a function is dependent on a condition:

Code:
return isReversed
    ? !wpiSolenoid.get()
    : wpiSolenoid.get();
when initializing a variable conditionally (you're almost required to use it here):

Code:
final double csd = isRedFriendly
        ? redScore - blueScore
        : blueScore - redScore;
and in the middle of a complex statement I don't want to write twice just for the conditional (this one's from WPILib):

Code:
m_digitalOut |= ((value ? 1 : 0) << (channel - 1));
I try to follow the rule that a method should have only one return statement. One point of entry, one point of exit. Ternary operators make that much easier.
__________________
Go directly to queue. Do not pass pit.
 


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
Calling all Hams--Amateur Radio Operators Covey41 General Forum 8 09-04-2007 19:45
Do you use ternary operators in your code? NotQuiteFree Programming 14 07-04-2004 11:21
How about a Drivers/Operators page and a Human Players page Anthony S. CD Forum Support 4 02-06-2001 22:47


All times are GMT -5. The time now is 03:34.

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