Quote:
Originally Posted by photon
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.