Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Precedence for Idiots

by Tanktalus (Canon)
on Dec 01, 2006 at 15:22 UTC ( #587211=note: print w/ replies, xml ) Need Help??


in reply to Precedence for Idiots

What I've attempted a few times, long ago before readability became important to me, is this:

$x ? $y = 1 : $z = 1;
That doesn't work, either. Parenthesis can help, of course. But that's not the right answer. The right answer is:
if ($x) { $y = 1; } else { $z = 1; }
Why? Because it's readable. If you're golfing, then maybe that's not what you want to hear. But for anything that needs maintenance, please consider this ;-)

(I'll echo BrowserUk's comments, though - a tutorial that moves someone from "initiate" to "novice" is sometimes more useful than moving someone from "novice" to "advanced"...)

Update: I stand corrected. jdporter's reply seems much more readable. <cough><cough>

*boggle*

(Yeah, I know, it was meant as a joke after my maintenance comment. At least, I *hope* it was a joke... ;->)


Comment on Re: Precedence for Idiots
Select or Download Code
Re^2: Precedence for Idiots
by jdporter (Canon) on Dec 01, 2006 at 15:40 UTC

    f00f.

    ${ $x ? \$y : \$z } = 1; ${ (\($y,$z))[!$x] } = 1;
    We're building the house of the future together.
Re^2: Precedence for Idiots
by Melly (Hermit) on Dec 01, 2006 at 16:10 UTC

    Hmm... I half agree with you. A week ago, any code containing ?: would have confused the hell out of me, but purely because I'd never used it or made myself familiar with what it did (or, to be honest, even knew it existed).

    However, I think one has to make a distinction between "this is obfuscated because I don't recognise the operator" and "this is obfuscated because I don't understand what the use of this familiar operator is going to produce under these conditions".

    IMHO, once the basics of ?: are understood, then one could argue that one line of code to assign a value to a variable, rather than 6 is no more or less obfuscated, but may make the code more readable purely on the basis of being shorter... still, this might make an interesting discussion point in meditations, so I'll raise it there, and edit my tutorial accordingly according to the consensus...

    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm@tomandlu.co.uk

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (16)
As of 2014-08-21 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls