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

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

by Argel (Prior)
on Jul 15, 2016 at 12:55 UTC ( [id://1167831]=note: print w/replies, xml ) Need Help??


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

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

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

Replies are listed 'Best First'.
Re^3: Thoughts on using and, or, and not over && || !? (purpose)
by choroba (Cardinal) on Jul 15, 2016 at 13:41 UTC
    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        

        According to perlop, the only difference is the precedence. For example, for "Logical And" it says that
        "Binary "and" returns the logical conjunction of the two surrounding expressions. It's equivalent to && except for the very low precedence.

        Are you saying that perlop is glossing over some "black magic" going on behind the scenes?

        Elda Taluta; Sarks Sark; Ark Arks
        My deviantART gallery

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 09:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found