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

Re: Thoughts on using and, or, and not over && || !? (purpose)

by tye (Sage)
on Jul 12, 2016 at 18:24 UTC ( [id://1167638]=note: print w/replies, xml ) Need Help??


in reply to Thoughts on using and, or, and not over && || !?

or was basically created for use in code like open ... or die .... That is why it was given extremely low precedence. It was not implemented as a "pretty" replacement for ||. So, or was meant to be used for flow control between statements while || continues to be meant for use in logical expressions.

Using || for flow control between statements was often leading to bugs:

open FOO, '<', $foo || die "Could not read $foo: $!\n";

That is still an easy bug to introduce, so you should use or for such.

So it should be no surprise that you can easily introduce bugs by using or for building logical expressions. In fact, I've seen quite a few bugs from quite a few different highly experienced Perl developers due to the use of or (or and) for building logical expressions.

They didn't even realize that they had introduced the bugs. So, perhaps you are just way better than all of those highly experienced Perl developers I've worked with. Maybe, the people who say "I've never made such mistakes" are actually correct in their assessment despite the multiple examples of people who thought they had not made that mistake until I pointed it out to them.

So I discourage people from using or for what || was designed for and vice-versa.

No bug:

if( $foo->is_ready() and $foo->needs_munging() ) { $status = $foo->munge(); } else { $status = $foo->status(); }

Bug easily introduced:

$status = $foo->is_ready() and $foo->needs_munging() ? $foo->munge() : $foo->status();

No warning generated. Lots of different ways to make that same mistake.

Hammers are ungainly and weirdly shaped. Screwdrivers are sleek and cylindrical. I pound nails in with screwdrivers because I think it makes the act of pounding in nails easier on the eyes. Some people think I'm silly for choosing my tools based on style considerations over how the tools were designed to be used. But I've never had problems with the nails I've pounded in, so what do they know?

- tye        

Replies are listed 'Best First'.
Re^2: Thoughts on using and, or, and not over && || !? (distinct)
by BrowserUk (Patriarch) on Jul 15, 2016 at 21:17 UTC

    Can you give a second (distinct) example of where you've been bitten by the low-precedence?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      my $do_munge = $foo->is_ready() and $foo->needs_munging(); return $foo->is_ready() and $foo->needs_munging(); $count += $foo->willing() and $foo->able(); push @passes, $foo->tried() and $foo->success(); $separator = $args->{separator} or ','; skip_if( $not_mocked or $in_prod, sub { test_stuff() } ); LOG "Failure to launch.", because => $!, 'retry?' => $retries_left and + $not_fatal;

      - tye        

        Thanks for that.

        For a decade or more I've habitually used and/or/not over &&/||/! in most situations in my code and, whilst I recall having encountered a handful or less situations where the precedence was wrong for my intent the first time I ran the code; I don't recall any occasion where it was something that I didn't discover at first run, or had any trouble locating and fixing. No latent bugs or deeply disguised issues that took any great effort to track down and fix.

        My feeling, without trying to put any great store in the accuracy of that feeling, is that I've been bitten (far) more often by the high precedence versions than the low. Mostly Perl warns me about them, but one or two have taken some effort. Of course, now I should respond in kind with examples; and I've racked my brains and can't reproduce anything that is a good example. I'll come back if I rediscover any of them.

        Please note, I'm not really arguing with your POV; just noting that my own experience doesn't reflect the strength of feeling you have on the subject.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Thoughts on using and, or, and not over && || !? (purpose)
by Argel (Prior) on Jul 15, 2016 at 12:55 UTC
    But couldn't parenthesis be used with the low precedence operators in that example? Sometimes we have to use parenthesis with the high precedence operators (that's why "or" was created for flow control), so how is using them with the low precedence operators any different, beyond what came first/tradition?

    Elda Taluta; Sarks Sark; Ark Arks
    My deviantART gallery

      The problem with parentheses is you need to know where to put them. See e.g. this infamous Python question (I apologize to the God(s) of Programming for staining the Monastery):
      >>> False in [False, True] True >>> not (True) in [False, True] False >>> (not True) in [False, True] True

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        The other problem with parentheses is that if you nest them more than just 2 levels, then people have a terrible time trying to match them up. Yes, I've had people tell me that they don't have a problem with that. But when I've shown such people a slide containing fairly simple code with full use of parentheses, zero of them noticed the bug I had introduced by adding the parentheses.

        Yes, it is certainly possible to use 'or' in a logical expressions in a way that does not introduce a bug. (Just like it is certainly possible to use a screwdriver to hammer in a nail.) But after having found multiple bugs due to such (that made it into Production code) having been written by quite a few different developers, several of them very experienced with Perl, I don't have enough hubris to think that I have such immense intelligence and flawless care such that I can avoid such mistakes.

        There are plenty of details to keep in mind when programming. Using a tool that requires you to be extra careful when you use it, yet in tons of situations causes no problems when you forget those extra steps, and then, when you actually introduce a bug because of it the bug is likely to only be triggered when a fairly specific combination of multiple conditions are met... Yeah, that sounds like a profoundly bad idea to me. And experience shows that it is.

        Using 'or' or 'and' in a logical expression in Perl gives you something that looks like a logical expression. And yet it is something that, only in some situations but still quite a large variety of situations, doesn't behave like a logical expression. It is extremely easy to, in the process of dealing with all of the other details flying around when working on code, to forget that this thing that is being used as a logical expression and that looks like a logical expression is not actually an ordinary logical expression and so can surprise you.

        - tye        

        In my experience, most people who can't figure out where to put the parentheses also can't figure out how to write/read an expression correctly.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found