Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Precedence for Idiots

by virtualsue (Vicar)
on Dec 01, 2006 at 16:16 UTC ( #587226=note: print w/ replies, xml ) Need Help??


in reply to Precedence for Idiots

At first glance, this node appeared to revolve around avoiding problems that mainly exist for a developer if the familar, self-documenting and more maintainable construct "if {} else {}" is discarded and replaced by "?:". It doesn't seem to be about operator precedence in general.

I'm not overjoyed by the examples, either.

$x?$y=1:$y=~s/.*/0/;
What?? How about
my $y = 1; $y = 0 if (!$x);
or, if you actually need a regular expression:
my $y = "a big string of something"; if (!$x) { $y =~ s/somebigoldregex/etc/; }
The above are more readable and make it fairly obvious what is going on. These attributes are very important. If someone (might even be you) has to debug a problem involving your code at 1 am on 5 cups of bad coffee, they're going to understand it faster and will be less likely to want to murder you when they're done. Even better, by writing clearer code you reduce the chances that you or somebody else will need to debug your code in the wee hours in the first place.

This may seem like a harsh response, but I gave you a ++ for your effort and enthusiasm. I would however like to propose that this node comes out of Tutorials, but only until it is reworked into a straightforward "how to use ?:" tutorial which does not even remotely look like it is encouraging the replacement of if/else statements in general. There are (a few) places where the ternary is useful and good, and it's fine to help people write them correctly. Coming up with more realistic examples would be nice. In addition, the title should be changed to something more meaningful.


Comment on Re: Precedence for Idiots
Select or Download Code
Re^2: Precedence for Idiots
by Melly (Hermit) on Dec 01, 2006 at 16:26 UTC

    I agree with 1.5 of your main 2 points - I must admit that my personal jury is still out on ?: vs. if/else/etc. - and I definitely agree that the tutorial needs expanding to include, e.g., precedence for 'and' and '&&', etc. if the title is to be appropriate.

    Thanks for the comments - not harsh at all.

    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://587226]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2014-09-20 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (158 votes), past polls