![]() |
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");Code:
roller.set(C.rollerSpeed*(C.rollerForward?1:-1));Code:
fDrive.set(C.upFront? C.fDrive:0);//if they're up, don't bother |
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. |
Re: Ternary operators
Quote:
|
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.
|
Re: Ternary operators
I use these all the time in my computer science class at the University of Minnesota
|
Re: Ternary operators
Quote:
|
Re: Ternary operators
Quote:
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:
Code:
C.lcd.lines[0] = "compressor:" + (comp.getPressureSwitchValue() Code:
roller.set(C.rollerSpeed * (C.rollerForwardCode:
// If they're up, don't bother |
Re: Ternary operators
Quote:
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) |
| All times are GMT -5. The time now is 23:21. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi