View Single Post
  #8   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.