Quote:
Originally Posted by Al3+
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
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...
