Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: ?: = Obfuscation?

by chargrill (Parson)
on Dec 01, 2006 at 16:34 UTC ( #587232=note: print w/ replies, xml ) Need Help??


in reply to ?: = Obfuscation?

I think the ternary operator is perfectly fine when used appropriately. For example, the following snippet:

print $_ ? do { $trip = $trip ? 0 : 1; $_ } && $_ : $trip ? !( $counter % 4 ) ? do { $nexchar = pop @string +_arr; '^' eq $nexchar ? ' ' : $nex +char } || '-' : ' ' : ' ';

... is a fine example of how NOT to use it when attempting to write maintainable code. Indeed, that's pulled from a properly indented and slightly deobfuscated obfuscation. On the other hand,

my $x = $counter > 4 ? 1 : 0;

... seems like an acceptable alternative to the more verbose:

my $x; if( $counter > 4 ){ $x = 1; } else { $x = 0; }

However, this type of shorthand is often best left to be decided by your environment. If you find yourself amidst folk who are comfortable with shorthand, by all means use that as a coding standard: "Where readability is not hindered, the use of the ternary operator in assignments is encouraged". If on the other hand your environment dictates that code be more explicit, whether its due to strict maintainability requirements or the presense of junior developers as maintainers, then adjust your coding standards to suit.



--chargrill
s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)


Comment on Re: ?: = Obfuscation?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-18 01:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls