Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: next unless condition

by GrandFather (Saint)
on Mar 31, 2016 at 21:06 UTC ( [id://1159231]=note: print w/replies, xml ) Need Help??


in reply to Re: next unless condition
in thread next unless condition

I was with you all the way until $cell or next;. That is a clever trick which doesn't convey intent and thus impedes understanding and maintenance.

From an understanding and maintenance point of view:

next if !$cell;

is succinct and clear and thus maintainable. In general I find if with a negated expression clearer than unless, especially if the expression is non-trivial. It also scales better so there are fewer surprises during maintenance if the expression needs to change.

Update: s/cleaver/clever/. Thanks kennethk - I'd like to claim the pun, but it wasn't.

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^3: next unless condition
by haukex (Archbishop) on Apr 01, 2016 at 07:38 UTC

    Hi GrandFather,

    The reason I added the explanation of $cell or next; to my post was because I first saw the output of B::Deparse, so perhaps the narrative of my post would have been better that way around - "here's what B::Deparse gives you, and here's what it means".

    Personally, I find $cell or next; is short enough to be readable in all three variations (next unless $cell;, next if !$cell;), and if I was writing it I'd lean towards next unless $cell;. If either side of the or had been more complex or becomes more complex later, then I might agree with you. As for the general case of if/unless vs. and/or, I write it however I find most readable. For example, sometimes I find $cond or die "some long error message" better than die "some long error message" unless $cond, even if the latter is wrapped on two lines. Or, I've recently found myself writing $DEBUG and print "..." more often, I find it helps me skip over those lines quicker when I'm skimming the code. On the other hand, if the condition is more complex, I'd start considering maintenance costs (usually precedence issues creeping in), and I'd probably switch over to the "safe" if (...) {...}. And it's a matter of TIMTOWTDI and taste too, I guess :-)

    Regards,
    -- Hauke D

    Update: Fixed one of the code samples.

Re^3: next unless condition
by Anonymous Monk on Mar 31, 2016 at 21:35 UTC

    GrandFather: I was with you all the way until $cell or next;. That is a cleaver trick which doesn't convey intent and thus impedes understanding and maintenance.

    In what way does it not convey intent?

    In what way is it unclear?

    It also scales better so there are fewer surprises during maintenance if the expression needs to change.

    How does it scale better? If you need to change the expression, why can't you change the entire line?

      In what way does it not convey intent?

      or is a boolean operator and implies an expression. Using if as a statement modifier is altogether different than forcing the role of implied if on the or operator.

      In what way is it unclear?

      By not conveying intent. Using or in the role of an if does not convey the intent of the code so the code is unclear.

      How does it scale better? If you need to change the expression, why can't you change the entire line?

      Completely replacing a chunk of code in order to make a small change to it means that the replaced code didn't scale at all!

      The "It" in "It also scales better" was referencing if compared to unless. Using unless tends quickly to get into double negative land making it harder to think about complicated expressions and harder to maintain code using them.

      Premature optimization is the root of all job security
        While I definitely agree that or doesn't clearly convey intent in this case, my understanding is that or was added as an alternative to || primarily for flow control purposes in constructs such as open my $file, '<', 'somefile.txt' or die; where the high precedence of || would cause unexpected issues. So I'd say that using or for flow control is completely appropriate in general, it's just not appropriate in this particular case.

        As for unless, I'm a big fan when it helps the code to read more like natural English. When you start getting into double negatives and complex expressions, then you swap it out for if because that's what happens in natural English.

        In what way does it not convey intent?

        or is a boolean operator and implies an expression. Using if as a statement modifier is altogether different than forcing the role of implied if on the or operator.

        What? I don't understand what you're saying. The page you linked says

        Binary "or" returns the logical disjunction of the two surrounding exp +ressions. It's equivalent to || except for the very low precedence. T +his makes it useful for control flow: print FH $data or die "Can't write to FH: $!";

        So I don't see how intent isn't conveyed,  ... or ... is very common pattern, very familiar, seems like intent

        Completely replacing a chunk of code in order to make a small change to it means that the replaced code didn't scale at all! The "It" in "It also scales better" was referencing if compared to unless. Using unless tends quickly to get into double negative land making it harder to think about complicated expressions and harder to maintain code using them.

        Does it really matter what "It" you're refering? Ok, I'll give it a shot

        die unless $live; ## the original line, which I expand a minute later die unless $live or $reanimated; ## first change, and first error; mo +ving on for now die unless $live or $reanimated or !$cooked; ## second change double n +egative, scary, still error, abandon unless, abandon unless, abandon +unless die if !$live or !$reanimated or $cooked; ## clarity, it works ... but + some people think differently, so choice, die if !($live or $reanimated) or $cooked; ## whooops, error again, lo +gic not practiced die if !($live and $reanimated) or $cooked; ## works again, choice is +choice

        So yeah, I don't see any issue with how it "scale"

        But I do see how having many things in unless gets confusing... if you cannot instantly write it correctly the first time

        because I don't practice using unless this way, I don't think about it that way

        but it didn't take a lot of practice to learn to use if, unless is the same way, just have to practice and learn

        but yeah most people don't do that

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-26 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found