|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Ternary operators
Was I the only one that used these on the robot?
For those not in the know, Ternary operators are essentially an inline if statment. Example using if statements, like mama told you to do so: Code:
boolean getState() {
if(this.thing.state) {
return true;
else {
return false;
}
}
Code:
boolean getStateTernary() {
return this.thing.state ? true : false;
}
The layout is: Code:
variable = [boolean condiditon] ? [if its true] : [if its false]; Did anyone else use these? |
|
#2
|
|
Re: Ternary operators
Instead of using the ternary operators or the if statements in your examples, why don't you just return the value of the boolean going into the if or ternary?
Instead if this: Code:
boolean getState() {
if(this.thing.state) {
return true;
else {
return false;
}
}
Code:
boolean getStateTernary() {
return this.thing.state ? true : false;
}
why not just this? Code:
boolean getState() {
return this.thing.state;
}
|
|
#3
|
|||
|
|||
|
Re: Ternary operators
I used them a couple of times in our robot code. It's a little unreadable, but it gets the job done well
|
|
#4
|
||||
|
||||
|
Re: Ternary operators
Quote:
@Radical Pi: Yay, can you post some memorable examples of using them? |
|
#5
|
||||
|
||||
|
Re: Ternary operators
I would use them if I had a reason to. This year I never did.
and @above how so? (booleanValue ? true : false) is equivalent to booleanValue for all intents and purposes, and booleanValue is more succinct. There's no way the former could cause a memory leak, unless the latter does too. Last edited by Al3+ : 05-04-2010 at 12:29. |
|
#6
|
|||
|
|||
|
Re: Ternary operators
Quote:
Code:
kickerJag->Set(jr->GetTrigger() || dsio->GetDigital(11) ? 0.0 : dsio->GetAnalogInRatio(1)); |
|
#7
|
||||
|
||||
|
Re: Ternary operators
I tend to use Ternary operators in printf() statements in C/C++ when trying to print boolean values.
Code:
printf("Some boolean value is : %s\n", some_bool ? "true" : "false");
|
|
#8
|
|||
|
|||
|
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);
Code:
return isReversed
? !wpiSolenoid.get()
: wpiSolenoid.get();
Code:
final double csd = isRedFriendly
? redScore - blueScore
: blueScore - redScore;
Code:
m_digitalOut |= ((value ? 1 : 0) << (channel - 1)); |
|
#9
|
||||
|
||||
|
Re: Ternary operators
I don't see how this could cause a memory leak. I use this type of statement all the time in C without a problem.
|
|
#10
|
||||
|
||||
|
Re: Ternary operators
never mind about the memory leaks
|
|
#11
|
||||
|
||||
|
Re: Ternary operators
I think it can also contribute to global warming...
![]() |
|
#12
|
|||
|
|||
|
Re: Ternary operators
Not to mention put your robot overweight...
![]() |
|
#13
|
|||
|
|||
|
Re: Ternary operators
we used them a couple of times, primarily where an if statement would be inconvenient, or when we are trying to count outputs (if could result in double counting).
some of our mentors really don't appreciate them, so using them is kinda limited |
|
#14
|
||||
|
||||
|
Re: Ternary operators
I use them like sprinkles, with verbose comments following them
|
|
#15
|
||||
|
||||
|
Re: Ternary operators
Quote:
Code:
return isReversed ^ wpiSolenoid.get(); // if reversed, then flip solenoid value |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |