Thread: Best Practices
View Single Post
Old 03-24-2010, 04:49 PM   #16
Shazam
Franchise Player
 
Shazam's Avatar
 
Join Date: Aug 2005
Location: Memento Mori
Exp:
Default

Quote:
Originally Posted by BlackEleven View Post
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 = 2;
}
else {
x = 1;
}

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.
Gak. I don't like that either.

int x = 1;
if (y > z) x = 2;

I always say, if you want to stuff as much as you can into one line, use PERL. Then, after you have regained your sanity, be verbose instead.
__________________
If you don't pass this sig to ten of your friends, you will become an Oilers fan.
Shazam is offline   Reply With Quote