http://www.perlmonks.org?node_id=587226


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.

Replies are listed 'Best First'.
Re^2: Precedence for Idiots
by Melly (Chaplain) 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