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