View Single Post
  #12   Spotlight this post!  
Unread 05-04-2010, 22:25
mathking's Avatar
mathking mathking is offline
Coach/Faculty Advisor
AKA: Greg King
FRC #1014 (Dublin Robotics aka "Bad Robots")
Team Role: Teacher
 
Join Date: Jan 2005
Rookie Year: 1999
Location: Columbus, OH
Posts: 642
mathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond repute
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.
__________________
Thank you Bad Robots for giving me the chance to coach this team.
Rookie All-Star Award: 2003 Buckeye
Engineering Inspiration Award: 2004 Pittsburgh, 2014 Crossroads
Chairman's Award: 2005 Pittsburgh, 2009 Buckeye, 2012 Queen City
Team Spirit Award: 2007 Buckeye, 2015 Queen City
Woodie Flowers Award: 2009 Buckeye
Dean's List Finalists: Phil Aufdencamp (2010), Lindsey Fox (2011), Kyle Torrico (2011), Alix Bernier (2013), Deepthi Thumuluri (2015)
Gracious Professionalism Award: 2013 Buckeye
Innovation in Controls Award: 2015 Pittsburgh
Event Finalists: 2012 CORI, 2016 Buckeye