Beefy Boxes and Bandwidth Generously Provided by pair Networks
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
in thread Trinary Operator Semantics by hardburn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found