Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: ?: = Obfuscation?

by imp (Priest)
on Dec 01, 2006 at 16:30 UTC ( #587230=note: print w/ replies, xml ) Need Help??


in reply to ?: = Obfuscation?

Like most things in perl the ternary operator can both help and hinder readability.

I prefer ternary for this:

# With ternary my $x = exists $hash{key} ? $hash{key} : 'default' # Without my $x; if (exists $hash{key}) { $x = $hash{key}; } else { $x = 'default'; }
I'm on the fence for this:
# With ternary my $x = $x > 5 ? 5 : $x + 1; # With if if ($x > 5) { $x = 5; } else { $x++; }
And dead set against this:
my $x = $y > 5 ? ($y = 0) : ($y = 1);


Comment on Re: ?: = Obfuscation?
Select or Download Code
Replies are listed 'Best First'.
Re^2: ?: = Obfuscation?
by ikegami (Pope) on Dec 01, 2006 at 20:31 UTC

    And dead set against this:

    my $x = $y > 5 ? ($y = 0) : ($y = 1);

    As you should be.

    my $x = ($y = $y > 5 ? 0 : 1);

    and

    $y = $y > 5 ? 0 : 1; my $x = $y;

    are much more readable.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2015-08-01 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls