The other issue with a ternary operator is with future code modification. In the example we've used:
return foo? x : y;
This is reasonable, if slightly confusing (I have to stop and think... x is the true case, y is the false case). I might prefer the if/else construct for readability purposes alone, but it would be more likely that I would use the if/else in case I ever need to add code -- perhaps later that simple statement has to become more complex:
if (foo)
{
log("Foo is true: manipulate x and y accordingly");
y+=x/2;
return x++;
}
else ...
Then I'm converting it from the ternary operator anyway, so may as well write it this way in the first place.
return foo? x : y;
This is reasonable, if slightly confusing (I have to stop and think... x is the true case, y is the false case). I might prefer the if/else construct for readability purposes alone, but it would be more likely that I would use the if/else in case I ever need to add code -- perhaps later that simple statement has to become more complex:
if (foo)
{
log("Foo is true: manipulate x and y accordingly");
y+=x/2;
return x++;
}
else ...
Then I'm converting it from the ternary operator anyway, so may as well write it this way in the first place.