Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^5: regex return true instead of false (precedence)

by Marshall (Canon)
on Aug 26, 2019 at 21:29 UTC ( [id://11105090]=note: print w/replies, xml ) Need Help??


in reply to Re^4: regex return true instead of false (precedence)
in thread regex return true instead of false

Actually, I like that better.
As I remember, a senior Monk yelled at me once when I posted something like that. But yes this is the way that I would do it in my code. I didn't want to get into an argument that detracts from the main point.
Update: As I remember the argument against this was like "injecting spacing like that is bad because it is manually done and IDE's don't do it that way". BS. The compiler doesn't give a darn about spaces. Whitespace is added to help the humans. If it helps the humans, it is a good idea.
  • Comment on Re^5: regex return true instead of false (precedence)

Replies are listed 'Best First'.
Re^6: regex return true instead of false (precedence)
by AnomalousMonk (Archbishop) on Aug 26, 2019 at 22:37 UTC

    I agree with your and GrandFather's leftist leaning when it comes to formatting complex logical expressions, but I think it's tangential to the main point made by haukex here: the statements
        if     (    condition  ) {} else {code }
        unless (    condition  )         {code }
        if     ( ! (condition) )         {code }
    are all exactly equivalent, so the condition expression within them does not need to change at all.


    Give a man a fish:  <%-{-{-{-<

      Well part of the point that I'm trying to make is that condition should change to make the logical sense of this easier to understand.

      unless ( (!($args =~ /\-rs(\s+)(\S+)/)) && (!($args =~ /\-p(\s+)(\S+)/)) && (!($args =~ /\-P(\s+)(\S+)/)) ) {..... # I figure this is easier to understand: if ( $args =~ /\-rs(\s+)(\S+)/ #update changed unless->if or $args =~ /\-p(\s+)(\S+)/ or $args =~ /\-P(\s+)(\S+)/ ) {....
      I was trained as a hardware engineer. DeMorgan's Theorem is an important part of hardware design. An AND or an OR gate is actually pretty rare. The most common gate is NAND, followed by NOR. The reason for that is that the fastest gates have a logical inversion - that is due to the way the hardware works. Inverting that output slows things down.

      Changing the formulation of condition can make things easier to understand. The compiler should generate similar code for the above examples. I just think that the second is easier for a human to understand.

        unless ( (!($args =~ /\-rs(\s+)(\S+)/)) && (!($args =~ /\-p(\s+)(\S+)/)) && (!($args =~ /\-P(\s+)(\S+)/)) ) {..... # I figure this is easier to understand: unless ( $args =~ /\-rs(\s+)(\S+)/ or $args =~ /\-p(\s+)(\S+)/ or $args =~ /\-P(\s+)(\S+)/ ) {....

        I agree the second form is easier to understand, but it's also not equivalent to the first:

        c:\@Work\Perl\monks>perl -wMstrict -le "for my $s (qw(p X)) { printf qq{for '$s' case }; unless ( (!($s =~ /p/)) && (!($s =~ /q/)) && (!($s =~ /r/)) ) { pri +nt 'A' } unless ( $s =~ /p/ or $s =~ /q/ or $s =~ /r/ ) { pri +nt 'B' } } " for 'p' case A for 'X' case B
        My understanding of haukex's point here is that the two if/unless-statement syntaxes that ovedpo15 was worried about are exactly equivalent, and so the condition expression doesn't have to change at all.

        If I had written the original code, I think I would have written something very much along the lines you and GrandFather suggest (or like this). But if the original condition expression was correct, convoluted as it was, and did not have to change, I would say it was the better part of wisdom to just leave it alone.

        I don't think we're really very far part on this one. :)


        Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-20 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found