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