Go to Post No one wants to have the piece that they spent hours swiss-cheesing break in half... - BillCloyes [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 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)
  #2   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...
__________________
  #3   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
  #4   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
  #5   Spotlight this post!  
Unread 05-04-2010, 23:46
Chris27's Avatar
Chris27 Chris27 is offline
Registered User
AKA: Chris Freeman
FRC #1625 (Winnovation)
Team Role: Alumni
 
Join Date: Mar 2005
Rookie Year: 2004
Location: Mountain View
Posts: 196
Chris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant futureChris27 has a brilliant future
Re: Ternary operators

Quote:
Originally Posted by Robototes2412 View Post
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?
I would recommend against coding in this style as not only is it not concise, but there is an overhead to making function/method calls. Why bury a simple boolean test in a bunch of syntax? As a rule of thumb, if you can code something with fewer lines of code, it is typically better to do so.
  #6   Spotlight this post!  
Unread 06-04-2010, 00:05
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

Quote:
Originally Posted by Al3+ View Post
Technically this could be also written:
Code:
return isReversed ^ wpiSolenoid.get(); // if reversed, then flip solenoid value
though the ?: version is arguably more readable.
Once again, it's about clarity of intent. Make it obvious to the reader what you are doing. IMO, if you're choosing what to return conditionally, putting the conditional inside the return statement is more readable (provided, of course, the reader knows what a ternary operator is).

The XOR, on the other hand, would make even someone familiar with that operator stop and think, "now what could he possibly be doing here?"

Quote:
Originally Posted by LukeS View Post
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
Try this:

Code:
C.lcd.lines[0] = "compressor:" + (comp.getPressureSwitchValue() 
        ? "off"
        : "on");
C.lcd.lines[1]= "SideKicker: " + (C.sideKickLoaded < 0 
        ? "released"
        : "loaded");
Code:
roller.set(C.rollerSpeed * (C.rollerForward
        ? 1
        : -1));
Code:
// If they're up, don't bother
fDrive.set(C.upFront
        ? C.fDrive 
        : 0);
bDrive.set(C.upBack 
        ? -C.bDrive
        : 0);
See what some well-placed white space can do for readability? Granted, breaking after ternary operators is my personal preference, but you should at the very least surround your operators with spaces. "Well-placed white space will save mistakes!" Say it with me now...
__________________
Go directly to queue. Do not pass pit.
  #7   Spotlight this post!  
Unread 07-04-2010, 00:39
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

Quote:
Originally Posted by FRC4ME View Post
See what some well-placed white space can do for readability? Granted, breaking after ternary operators is my personal preference, but you should at the very least surround your operators with spaces. "Well-placed white space will save mistakes!" Say it with me now...
Oh, I know, I'm our teams white-space nazi! (if you don't believe me, just check*) I just generally feel that ternaries should be a single line, although, looking at how you did it, I like how newlines looks with text strings. However, I still feel that for the others, a single line is more readable. Looking at the drive code in black, I would probably add more spaces, but with syntax highlighting, I think it's fine how it is.

Also, for text output, the reason the compressor line has NO spaces at all is that with 2 4-character tabs (class and method) we were squeezing for 80 columns (something which I am also a nazi for).

* although, it does look like some spacing got messed up when tweaking numbers in autonomous. I'll fix this and update it today or tomorrow. (that URL is always the newest version)
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