Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Ternary operators (http://www.chiefdelphi.com/forums/showthread.php?t=84948)

Robototes2412 04-04-2010 22:30

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;
    }
}

Example using the ternary operators:
Code:

boolean getStateTernary() {
    return this.thing.state ? true : false;
}

It does the same thing but is way easier.

The layout is:
Code:

variable = [boolean condiditon] ? [if its true] : [if its false];
This works in C, C++, Java, and I believe C#.

Did anyone else use these?

biojae 04-04-2010 22:36

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;
    }
}

or this:

Code:


boolean getStateTernary() {
    return this.thing.state ? true : false;
}


why not just this?

Code:


boolean getState() {
    return this.thing.state;
}


Radical Pi 04-04-2010 22:38

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

Robototes2412 04-04-2010 22:45

Re: Ternary operators
 
Quote:

Originally Posted by biojae (Post 948107)
why not just this?

Code:


boolean getState() {
    return this.thing.state;
}


Its bad practice, according to my dad, who uses java at his work. It can also lead to memory leaks.

@Radical Pi: Yay, can you post some memorable examples of using them?

Al3+ 04-04-2010 22:48

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.

Radical Pi 04-04-2010 23:28

Re: Ternary operators
 
Quote:

Originally Posted by Robototes2412 (Post 948120)
@Radical Pi: Yay, can you post some memorable examples of using them?

This one got me messed up a few times

Code:

kickerJag->Set(jr->GetTrigger() || dsio->GetDigital(11) ? 0.0 : dsio->GetAnalogInRatio(1));
Explanation: We use a rotating kicker mounted on pneumatic sliders with a clutch so that we can spin it inside the robot then slide it out to kick. If the trigger on the right joystick or the big red kick button is pressed, then disable the spinner motor (no reason to leave it spinning freely). If neither button is pressed (kicker retracted), then set the speed to the value of a potentiometer on the DS.

virtuald 04-04-2010 23:44

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");
Generally speaking, ternary operators are usually more difficult to read, so I try to avoid them. However, if you space it out and format them correctly then they aren't so bad.

FRC4ME 04-04-2010 23:58

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.

rsisk 05-04-2010 00:20

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.

Robototes2412 05-04-2010 00:33

Re: Ternary operators
 
never mind about the memory leaks

gvarndell 05-04-2010 06:24

Re: Ternary operators
 
Quote:

Originally Posted by Robototes2412 (Post 948120)
It can also lead to memory leaks.

I think it can also contribute to global warming... ;)

360skier 05-04-2010 09:03

Re: Ternary operators
 
Quote:

Originally Posted by gvarndell (Post 948302)
I think it can also contribute to global warming... ;)

Not to mention put your robot overweight... :p

Zme 05-04-2010 12:55

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

Robototes2412 05-04-2010 13:11

Re: Ternary operators
 
I use them like sprinkles, with verbose comments following them

Al3+ 05-04-2010 14:48

Re: Ternary operators
 
Quote:

Originally Posted by FRC4ME (Post 948205)
Code:

return isReversed
    ? !wpiSolenoid.get()
    : wpiSolenoid.get();


Technically this could be also written:
Code:

return isReversed ^ wpiSolenoid.get(); // if reversed, then flip solenoid value
though the ?: version is arguably more readable.


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

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