Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

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

Terse Good, Obfuscated Bad

I have been reading Perl Best Practices by thedamian. When I first read what he had to say about using ?: I was only half listening (if that makes sense ;) - but I was struck by how he laid out the following code. At first I didn't like how it was spread out but as I traced the logic I realized it was beautiful.

# When their name is... Address them as... my $salute = $name eq $EMPTY_STR ? 'Customer' : $name =~ m/\A((?:Sir|Dame) \s+ \S+) /xms ? $1 : $name =~ m/(.*), \s+ Ph[.]?D \z /xms ? "Dr $1" : $name ;
Code From: Perl Best Practices, Damian Conway PBP @ O'Reilly

My big gripe has already been addressed above, ?: has to be used in the correct circumstances. The way I generally use it is usually as a one line choice of functions:

$conditional ? foo() : bar();

or

$conditional ? foo($arg_1) : foo($arg_2);

What I don't like to see is extra processing in the choices:

$conditional ? $foo++ : $foo += 2;

When there are assignments involved it can get confusing and I would agree that a regular if else block is more appropriate and more easily expanded with extra logic.

??|| IMHO (I'm sorry) but that is a messy solution to a non language problem. The problem is not that the ?: is hard to see, it is that it gets abused... Kinda like forcing all cars to have square wheels to keep drunks from driving.

I hope this 2¢ helps :)
--Brig

Update: Added link to Damian's node.

Update: The reason I am tolerant of the "extra processing" in the first example is that the processing is a string search. When the search hits, the code directly to the right executes. Therefore I believe this is a very readable construct. The only drawback is that the regex must be reasonably short, I am not strict on the 80 column rule but it is a good rule.


In reply to Re: ?: = Obfuscation? by brig
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 cooling their heels in the Monastery: (6)
As of 2024-04-18 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found