View Single Post
  #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.