![]() |
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() {Code:
boolean getStateTernary() {The layout is: Code:
variable = [boolean condiditon] ? [if its true] : [if its false];Did anyone else use these? |
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:
Code:
why not just this? Code:
|
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
|
Re: Ternary operators
Quote:
@Radical Pi: Yay, can you post some memorable examples of using them? |
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. |
Re: Ternary operators
Quote:
Code:
kickerJag->Set(jr->GetTrigger() || dsio->GetDigital(11) ? 0.0 : dsio->GetAnalogInRatio(1)); |
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"); |
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(isOnCode:
return isReversedCode:
final double csd = isRedFriendlyCode:
m_digitalOut |= ((value ? 1 : 0) << (channel - 1)); |
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.
|
Re: Ternary operators
never mind about the memory leaks
|
Re: Ternary operators
Quote:
|
Re: Ternary operators
Quote:
|
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 |
Re: Ternary operators
I use them like sprinkles, with verbose comments following them
|
Re: Ternary operators
Quote:
Code:
return isReversed ^ wpiSolenoid.get(); // if reversed, then flip solenoid value |
| All times are GMT -5. The time now is 03:34. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi