Go to Post the whole process is the most valuable part, not the award itself - Rich Kressly [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: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.
  #5   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,749
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.
  #6   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
  #7   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?
  #8   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"
  #9   Spotlight this post!  
Unread 04-04-2010, 23:44
virtuald's Avatar
virtuald virtuald is offline
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,058
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
  #10   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.
  #11   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
  #12   Spotlight this post!  
Unread 05-04-2010, 21:59
LukeS LukeS is offline
4272 mentor, 1024 alumnus
AKA: Luke Shumaker
FRC #4272
Team Role: Mentor
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Indianapolis, IN
Posts: 60
LukeS is an unknown quantity at this point
Re: Ternary operators

Yeah, we do all the time. Although, prior to this year I was the only one on the team who knew about them. I distinctly remember explaining them last year, saying that they are the text equivalent of that triangle thing in LabVIEW (bleh!), though apparently no one remembered.

examples from our code:

text output:
Code:
C.lcd.lines[0]="compressor:"+(comp.getPressureSwitchValue()?"off":"on");
C.lcd.lines[1]="SideKicker: "+(C.sideKickLoaded<0?"released":"loaded");
flipping a roller based on a switch:
Code:
roller.set(C.rollerSpeed*(C.rollerForward?1:-1));
only spinning the extend-able wheels if they're extended
Code:
fDrive.set(C.upFront? C.fDrive:0);//if they're up, don't bother
bDrive.set(C.upBack ?-C.bDrive:0);//if they're up, don't bother
  #13   Spotlight this post!  
Unread 05-04-2010, 22:25
mathking's Avatar
mathking mathking is offline
Coach/Faculty Advisor
AKA: Greg King
FRC #1014 (Dublin Robotics aka "Bad Robots")
Team Role: Teacher
 
Join Date: Jan 2005
Rookie Year: 1999
Location: Columbus, OH
Posts: 636
mathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond repute
Re: Ternary operators

A ternary operator statement is kind of like a small switch statement. They can be useful, and if you like them go ahead and use them. But don't fall into the trap of thinking that they are necessarily more efficient. That depends on the language and compiler, and exactly how the if statement and the ternary statement are set up. Just because one piece of code is longer than another does not mean that it will be compiled into less efficient code. In a great many cases both pieces of code will compile into identical or nearly identical object code.

I would tend to argue that they almost never make code easier to understand than a well written if - else. If you know what you are doing then they may not be any more difficult to read, but I don't think the use of a ternary statement will make an unclear if - else statement suddenly clear.

Using the cRIO and Java or C++, I think the most important thing to do with the code is to make it easily readable. There is no compelling reason to have a small code size, just good efficient code. Think of how much time you spend tweaking and debugging for any program. But then place this in the context of a robotics competition where finding the problem code and fixing it must often be done in a very short, fixed period of time. Code clarity is essential. So use structures that all of the programmers agree upon ahead of time. A set of coding standards is never a bad idea. (And it will help student programmers get some real world experience.) So use the format that your team understands and wants to use.
__________________
Thank you Bad Robots for giving me the chance to coach this team.
Rookie All-Star Award: 2003 Buckeye
Engineering Inspiration Award: 2004 Pittsburgh, 2014 Crossroads
Chairman's Award: 2005 Pittsburgh, 2009 Buckeye, 2012 Queen City
Team Spirit Award: 2007 Buckeye, 2015 Queen City
Woodie Flowers Award: 2009 Buckeye
Dean's List Finalists: Phil Aufdencamp (2010), Lindsey Fox (2011), Kyle Torrico (2011), Alix Bernier (2013), Deepthi Thumuluri (2015)
Gracious Professionalism Award: 2013 Buckeye
Innovation in Controls Award: 2015 Pittsburgh
Event Finalists: 2012 CORI, 2016 Buckeye
  #14   Spotlight this post!  
Unread 05-04-2010, 22:44
Unsung FIRST Hero
Greg Marra Greg Marra is offline
[automate(a) for a in tasks_to_do]
FRC #5507 (Robotic Eagles)
Team Role: Mentor
 
Join Date: Oct 2004
Rookie Year: 2005
Location: San Francisco, CA
Posts: 2,030
Greg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond reputeGreg Marra has a reputation beyond repute
Re: Ternary operators

Quote:
Originally Posted by mathking View Post
Using the cRIO and Java or C++, I think the most important thing to do with the code is to make it easily readable.
I agree 100%. I have heard that code is read on average ten times more often than it is written. Style is personal preference, but would you rather save some keystrokes now, or a debugging headache a month later?
  #15   Spotlight this post!  
Unread 05-04-2010, 23: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 always have three lines of comments after ternary operators explaining them and instructions to yell at me if they dont understand.
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 03:34.

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