Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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

by tye (Sage)
on Jul 15, 2016 at 16:54 UTC ( #1167845=note: print w/replies, xml ) Need Help??

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

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        

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

Replies are listed 'Best First'.
Re^5: Thoughts on using and, or, and not over && || !? (care)
by Argel (Prior) on Jul 16, 2016 at 01:18 UTC
    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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2022-10-05 08:34 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (21 votes). Check out past polls.