Ternary Operators and Readability
As a programmer, I can't bear writing too much. So I get in trouble all the time with the temptation of ternary operators. They are so terse and useful even though lots of people don't think they are "readable."
A few days ago I wrote a part of our project, an ASP.NET web application, that would toggle the order in which some processing took place. Based on an event I needed to swap two items either up or down. To know whether to go up or down I wrote first:
int move;
switch(e.CommandName){
case "UP":
move = -1;
// do some stuff
break;
case "DOWN":
move = 1;
// do some stuff
break;
}
Then I thought about it and wrote:
int move = e.CommandName=="UP"?-1:1;
I made a comment on Joel On Software's forums and got two kinds of responses. The big company programmers who thought it was unthinkable and the perl hacker types who couldn't care less. I'm a ways from a perl hacker but I'd rather be aligned with that than assembly line programming.
posted in [WhiteBox]
6:00:21 AM