WhiteBox
Opening the world of development

David-ism
Watu
Vicariously
Photo Blogs
Form, Function
Write, Think
Web People
Coders
Feel Good


Subscribe to "WhiteBox" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

Home (all entries)  | Technie  | Prattle (personal stuff)  | Books  | Snippets  | WhiteBox


 Friday, May 21, 2004

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    


Click here to visit the Radio UserLand website. © Copyright 2006 David Seruyange.
Last update: 5/23/2006; 8:47:34 PM.
May 2004
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Apr   Jun