in reply to An alternative to ?: notation
That's a mighty big assumption to make. In this case the ternary operator is far more maintainable. Someone's going to have a *fun* debugging session if the author of the ternary operator has allowed for result being undef,"Bob" or 17.
The ternary operator allows for a consise testing of true/false and returns a value. The power of this of this operator, if properly exploited and understood, is its ability to generate a concise bit of code. What if we have user input and we need to return a value (computed in a subroutine) based upon their input, or a default value if they gave no input (or an input that evaluates as false)?
Consider the alternative:$result = $input ? somesub($input) : $default;
I know which I'm putting in my code.if ($input) { $result = somesub($input); } else { $result = $default; }
Cheers,
Ovid
|
|---|