P is for Practical | |
PerlMonks |
Re^3: Ternary operators: a hinderance, not a helpby Anonymous Monk |
on Aug 10, 2005 at 10:56 UTC ( [id://482563]=note: print w/replies, xml ) | Need Help?? |
As I state in the OP, however, my point about readability and clarity is concerned more with more complex constructs, of which that example is not one :)I got the impression the OP condemned all use of ?:. If your opinion is "use ?: when that's readable, use if/then/else when that is", you and I agree, because that's what I do. I use ?: when I find that more readable if/then/else, otherwise, I use if/then/else.
Chained/cascading ternaries quickly lose readability, especially when combined with other code elements.So do chained/cascading if/then/else constructs. Or nested loops, or nested indices. Your original example, I would write either as: or or depending whether I want to stress the default value of $number. (I might use one of the latter two if $logical_test is expected to have a particular value, and it not having it is an exception, I use the former line if $logical_test could go both ways). If $logical_test is 0 if it's false, I might even write it as:
I wouldn't easily write: as it assigns an initial value to $number in a different scope than its declaration - IMO, that doesn't score bonus points when it comes to readability. It's also 7 lines instead of 1, although it can easily be shortened to: But still, it's three lines. And worse, both blocks have two thirds of their tokens in common - which just shouts "factor out, factor out". Which would lead to: But then I prefer:
In Section
Meditations
|
|