Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: ?: = Obfuscation?

by brig (Scribe)
on Dec 01, 2006 at 22:44 UTC ( #587323=note: print w/ replies, xml ) Need Help??


in reply to ?: = Obfuscation?

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.


Comment on Re: ?: = Obfuscation?
Select or Download Code
Re^2: ?: = Obfuscation?
by sgt (Chaplain) on Dec 05, 2006 at 00:08 UTC

    Actually I donīt like much the ternary operator, except when it's shorter and readable, and maybe a few C-like uses...

    and I was really surprised by PBP on this; I had thought ?: would be simply rejected, and comes that incredible table layout, really amazing one of the gems of the book

    still only half-way convinced ;) I donīt know why...a gut feeling mainly... funny we thought more or less the same way

    hth --stephan

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-09-19 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (133 votes), past polls