Thread: Best Practices
View Single Post
Old 03-24-2010, 03:55 PM   #11
BlackEleven
Redundant Minister of Redundancy
 
BlackEleven's Avatar
 
Join Date: Apr 2004
Location: Montreal
Exp:
Default

Quote:
Originally Posted by photon View Post
Ah ok, I've always called that a ternary operator.

Yeah I'm not much of a fan either. Code readability is important in my opinion and using that instead of something more explicit hurts that, though that kind of is a self defeating argument because if it was used more it would be more readable.

But I'm one who'll do:

Code:
if (a > b)
{
     doStuff();
}
else
{
     // do nothing
}
just for readability...

Yes, its called a ternary operator. Some people definitely overdo it but I find it extremely useful for assignment.

Instead of

if ( y > z ) {
x = 1;
}
else {
x = 2;
}

you can just do

x = y > z ? 1 : 2;

To me, both are just as readable, but one takes 6 lines where the other takes one.

Even if you don't use it you should still know it since you'll be reading other people's code at some point and a lot of people do use it a lot, especially the old school guys.
BlackEleven is offline   Reply With Quote