Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

?: gives back a value. Using it in a situation where you don't use the return value is a good indication that you should probably just replace it with if/else. Although Perl allows if/else to give back a value as well, using it in that way is usually a good way to surprise people.

So I use ?: only for fairly simple cases of just picking between two values (or perhaps a few more, via multiple uses of ?:). I use if/else if the primary point is not simply giving back either this or that value. For cases of picking between some values that aren't quite simple, I use sub. Of course, using sub for a simple case of picking between two values is also acceptable and is sometimes a much better choice than inlining a ?:.

Note that using ?: as it was intended (to return a value) also usually means that you don't run into precedence problems and so you usually won't need parens (which can quickly become visual clutter if many are required).

And, using if/else is often appropriate or even better than ?:. For example, in:

$x= cond() ? this() : that(); # vs. if( cond() ) { $x= this(); } else { $x= that(); }

The more verbose version might be preferred just for style reasons, because it is easier to add comments to without impacting readability, or because it is suspected that future maintenance will complicate the two blocks to the point of making ?: inappropriate anyway.

Of course, the ?: has some advantages that can be important sometimes. Obviously, it is more compact. Although making code too compact can make it harder to read, I also find that making code too expansive can make it hard to read. That can distract the viewer by giving too much space to items of relatively little importance or it can make it impossible to view an entire logical structure and get the feel for the "big picture" of it.

Also, $x is not repeated in the compact form. This becomes even more of an advantage in a fairly common example like this:

my $x= cond() ? this() : that(); # vs. my $x; if( cond() ) { $x= this(); } else { $x= that(); }

Note that we had to repeat $x three times for the sake of using if/else.

And you might have good reason to prefer to use if/else as part of a way to simply return either this or that value:

$x= cond() ? this() : that(); # vs. sub pick { if( cond() ) { return this(); } else { return that(); } } $x= pick();

The biggest advantage of ?: is for cases like:

my $x= DoSomething( cond() ? this() : that(), $whatever, foo() ? bar() : baz(), $blah, );

Finally, there are rare cases where restrictions make using the rather surprising ability of if/else to give back a value quite appropriate. For example:

s{ # Some fairly complex parsing regex }{ my( ... )= ( $1, $2, $3, $4 ); if( ... ) { ... "replacementString"; } elsif( ... ) { ... "differentReplacement"; } else { "somethingElse"; } }gex;

- tye        


In reply to Re: ?: = Obfuscation? (value) by tye
in thread ?: = Obfuscation? by Melly

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 chilling in the Monastery: (7)
As of 2024-03-28 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found