Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Trinary Operator Semantics

by demerphq (Chancellor)
on May 27, 2005 at 07:55 UTC ( [id://460979]=note: print w/replies, xml ) Need Help??


in reply to Trinary Operator Semantics

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://460979]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found