Just another Perl shrine | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
It is true that they are functionally identical. In other words, if you create two functions with each construct and give them the same input, they will always return the same output. Well, insofar as you can use one to simulate the other always, however the relationship is not symmetrical in that the ternary generally speaking (that is without the use of do{}) cannot simulate the if(){}else{}. To me this is much more important issue than their relative efficiency. When you use a ternary you are signaling that your conditional logic is used to select a value. When you use an if statement you are signalling that your conditional logic determines what set of actions occur. To me these are totally different signals which are very useful in understanding the code. If a conditional has no side effects then I prefer it to be in a ternary (or even a chained ternary) so that it is very clear that there are no side effects to be considered. As soon as my eyes see an if statement they start looking for multiple side effects, when they see a ternary they start looking for where the resulting variable is used because that will be where the side effects manifest themselves. These are totally different cognitive processes IMO.
--- $world=~s/war/peace/g In reply to Re: Trinary Operator Semantics
by demerphq
|
|