Go to Post However I did tell my wife I wanted to lose 20lbs of ugly fat quickly. She told me to cut off my head. Go figure. - Ed Coleman [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

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 04-04-2010, 22:30
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
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?
  #2   Spotlight this post!  
Unread 04-04-2010, 22:36
biojae's Avatar
biojae biojae is offline
Likes Omni drives :)
AKA: Justin Stocking
FTC #5011 (BOT SQUAD) && FTC#72(Garage bots)&& FRC#0399 (Eagle Robotics)
Team Role: College Student
 
Join Date: Oct 2008
Rookie Year: 2008
Location: Lancaster
Posts: 276
biojae is a jewel in the roughbiojae is a jewel in the roughbiojae is a jewel in the rough
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;
}
__________________
FTC Team 72 - No site
FRC Team 399 - http://www.team399.org
2010 Rockwell Collins Innovation in Control Award - (Use of the CAN bus, among other reasons) Phoenix, Arizona!
  #3   Spotlight this post!  
Unread 04-04-2010, 22:38
Radical Pi Radical Pi is offline
Putting the Jumper in the Bumper
AKA: Ian Thompson
FRC #0639 (Code Red Robotics)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2010
Location: New York
Posts: 655
Radical Pi has a spectacular aura aboutRadical Pi has a spectacular aura aboutRadical Pi has a spectacular aura about
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
__________________

"To have no errors would be life without meaning. No strugle, no joy"
"A network is only as strong as it's weakest linksys"
  #4   Spotlight this post!  
Unread 04-04-2010, 22:45
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
Re: Ternary operators

Quote:
Originally Posted by biojae View Post
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?
  #5   Spotlight this post!  
Unread 04-04-2010, 22:48
Al3+'s Avatar
Al3+ Al3+ is offline
ARTist
AKA: Anthony
FRC #0840 (Aragon Robotics Team)
Team Role: Programmer
 
Join Date: Oct 2009
Rookie Year: 2008
Location: San Mateo, CA
Posts: 58
Al3+ is a jewel in the roughAl3+ is a jewel in the roughAl3+ is a jewel in the rough
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.
__________________
cout << "Hello, robotics. Goodbye, world." << endl;

"The two-axis accelerometer provided in the kit of parts (shown in the picture below) is a two-axis accelerometer." - WPILib User's Guide

Last edited by Al3+ : 05-04-2010 at 12:29.
  #6   Spotlight this post!  
Unread 04-04-2010, 23:28
Radical Pi Radical Pi is offline
Putting the Jumper in the Bumper
AKA: Ian Thompson
FRC #0639 (Code Red Robotics)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2010
Location: New York
Posts: 655
Radical Pi has a spectacular aura aboutRadical Pi has a spectacular aura aboutRadical Pi has a spectacular aura about
Re: Ternary operators

Quote:
Originally Posted by Robototes2412 View Post
@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.
__________________

"To have no errors would be life without meaning. No strugle, no joy"
"A network is only as strong as it's weakest linksys"
  #7   Spotlight this post!  
Unread 04-04-2010, 23:44
virtuald's Avatar
virtuald virtuald is online now
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,086
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
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.
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
  #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.
  #9   Spotlight this post!  
Unread 05-04-2010, 00:20
rsisk's Avatar
rsisk rsisk is offline
The GURU Channel
AKA: Richard Sisk
FRC #2493 (Robokong)
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2007
Location: Riverside, CA
Posts: 2,750
rsisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond reputersisk has a reputation beyond repute
Send a message via MSN to rsisk
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   Spotlight this post!  
Unread 05-04-2010, 00:33
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
Re: Ternary operators

never mind about the memory leaks
  #11   Spotlight this post!  
Unread 05-04-2010, 06:24
gvarndell's Avatar
gvarndell gvarndell is offline
Software Engineer
AKA: Addi's and Georgie's Dad
FRC #1629 (GaCo)
Team Role: Parent
 
Join Date: Jan 2009
Rookie Year: 2008
Location: Grantsville, Maryland
Posts: 350
gvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond reputegvarndell has a reputation beyond repute
Re: Ternary operators

Quote:
Originally Posted by Robototes2412 View Post
It can also lead to memory leaks.
I think it can also contribute to global warming...
__________________
Robots never, ever, ever, ever break -- The Robot Repairman (Backyardigans)
  #12   Spotlight this post!  
Unread 05-04-2010, 09:03
360skier 360skier is offline
RTFM!!
AKA: Eric A.
FRC #1334 (Red Devils)
Team Role: Programmer
 
Join Date: Dec 2008
Rookie Year: 2008
Location: Oakville, Ontario
Posts: 45
360skier is an unknown quantity at this point
Re: Ternary operators

Quote:
Originally Posted by gvarndell View Post
I think it can also contribute to global warming...
Not to mention put your robot overweight...
__________________
  #13   Spotlight this post!  
Unread 05-04-2010, 12:55
Zme Zme is offline
Registered User
FRC #2619
 
Join Date: Jan 2009
Location: Michigan
Posts: 83
Zme is on a distinguished road
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   Spotlight this post!  
Unread 05-04-2010, 13:11
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
Re: Ternary operators

I use them like sprinkles, with verbose comments following them
  #15   Spotlight this post!  
Unread 05-04-2010, 14:48
Al3+'s Avatar
Al3+ Al3+ is offline
ARTist
AKA: Anthony
FRC #0840 (Aragon Robotics Team)
Team Role: Programmer
 
Join Date: Oct 2009
Rookie Year: 2008
Location: San Mateo, CA
Posts: 58
Al3+ is a jewel in the roughAl3+ is a jewel in the roughAl3+ is a jewel in the rough
Re: Ternary operators

Quote:
Originally Posted by FRC4ME View Post
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.
__________________
cout << "Hello, robotics. Goodbye, world." << endl;

"The two-axis accelerometer provided in the kit of parts (shown in the picture below) is a two-axis accelerometer." - WPILib User's Guide
Closed Thread


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 23:21.

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