Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (2)
    As of 2018-02-17 22:30 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (250 votes). Check out past polls.