Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^2: Regex Or Die!

by TheDamian (Priest)
on Sep 27, 2006 at 22:25 UTC ( #575263=note: print w/replies, xml ) Need Help??

in reply to Re: Regex Or Die!
in thread Regex Or Die!

There's nothing wrong with unless
Sure there is. And I spent a page and a half in PBP explaining precisely what was wrong with it. In summary:
  • unless is fine for simple conditionals.
  • But it makes compound conditionals harder to understand (because, in general, people are relatively poor at applying de Morgan's law correctly).
  • In particular it makes conditionals that include partial negations very much harder to comprehend.
  • Even conditionals that start out simple, don't always stay simple; extra subconditions are often identified and handled during maintenance or extension of code

The result is that (as PBP points out):

unless and until are inherently harder to maintain. In particular, whenever a negative control statement is extended to include a negative operator, it will have to be re-cast as a positive control, which requires you to change both the keyword and the conditional....Not only is that extra effort, it's error-prone effort. Reworking conditionals in that way is an excellent opportunity to introduce new and subtle bugs into your program....This kind of mistake is unfortunately common, but fortunately very easy to avoid. If you never use an unless or until, then you'll never have to rewrite it as an if or while. Moreover, if your control statements are always "positive", your code will generally be more comprehensible to all readers, regardless of the complexity of its logical expressions...

I don't proscribe unless because it's never appropriate. I recommend against it because it's often eventually inappropriate. Because, in all but the simplest cases, it ultimately makes comprehension and maintenenance harder and provides an opportunity for bugs to creep in as code is extended.

Now, you may not agree with that arguments or my conclusion, and that's fine. But it's not accurate to assert that I'm merely letting:

personal opinions slip in there

or to imply that I did not have carefully thought-out reasons for my advice.

Indeed, it's easy to demonstrate that the deprecation of unless was not just personal opinion. If you examine the source code of my modules that predate PBP, you will find many instances of unless. So, until I sat down to examine and codify Perl best practice my personal opinion was apparently that unless was fine. It was only after I reviewed the arguments for and against, discussed the issue (vigorously and at length ;-) with my many reviewers, examined the mistakes that I and many of my clients had made using unless, and thought about the long-term implications of deploying the construct, that I concluded it should not be used.

Of course, that still is a personal opinion—just like everything else that everyone else ever says—but it a personal opinion based on reasoned argument and extensive professional and pedagogical experience.

On the other hand, your subsequent point that blind acceptance of my advice is contrary to the spirit of the book is perfectly valid. I spent the first chapter asking people to think about these issues, to evaluate my arguments, and then to decide for themselves. However, not everyone has the time, the will, or the capacity to do that, which is why I tried to make the advice as careful and conservative as possible, so that those who do adopt it wholesale will be unharmed by the suggestions I made.

By the way, to answer the original poster's original question, my suggestion would be:

while (my $line = <>) { croak "I can't handle this crap! '$line'\n" if $line !~ m{\A (foo|bar) }xms; my $dumb_example = $1; .... .... }

Replies are listed 'Best First'.
Re^3: Regex Or Die! (!unless)
by tye (Sage) on Sep 28, 2006 at 02:04 UTC
    • unless is fine for simple conditionals

    I disagree, but the reason is rather subtle and my experience shows that most people just won't buy the explanation until they've personally been burned by it. I've seen quite a few people get burned by it by now, and I'm quite convinced.

    The "not" that is implicit in "unless" is subtle enough that it can get "lost" when you are looking at code. I've seen several people (including myself) stare at code over and over trying to figure out what is going on because they mentally misplaced the too-subtle "not". The code does exactly the opposite of what they expect and they can look at it and break it down over and over and still not recover the missing "not".

    Just a couple of days ago, someone asked one of those head-slap questions in the chatter box to which I responded "because 48 *is* less than 50". They had looked at this very simple code (one simple statement with one simple comparison) over and over and couldn't figure out what was wrong. In this case they hadn't even used "unless", but had started to use "unless" and then changed their mind and used "if" instead. The distinction between the two is so subtle that even after repeated attempts, they failed to notice their mistake.

    This shows that not only shouldn't you use "unless" even for simple conditionals, but shouldn't even consider using "unless". (:

    - tye        

      I don't see why people have this trouble?

      Which is clearer?

      • Don't speak unless you're spoken to.
      • Don't speak if not you're spoken to.

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        • I could care less.
        • Irregardless.

        • Don't speak unless you're spoken too.
        • Don't speak if not you're spoken too.

        Well, the second one would be "Don't speak if you're not spoken to." I'm not advocating that people use improper Perl grammar. And, in proper English, they both seem pretty equally understandable. Many would prefer the former (especially if you drop the "you're", which would also make your second line clear and hence probably explains why you added it) simply because it is an oft-repeated phrase.

        Even clearer would be "Only speak when spoken to." In Perl, that would be something like:

        if( SpokenTo() ) { Speak(); }

        which doesn't quite have the same meaning as the English phrase, at least stand-alone. A closer match would be:

        sub Speak { return unless SpokenTo(); # ... }

        which I don't find as clear as

        sub Speak { return if ! SpokenTo(); # ... }

        As I said, it is subtle. Jumping into a different language isn't likely the best way to try to understand it.

        Certainly, I don't give much weight to a measurement of how likely a poor translation of a Perl construct into pidgin English is to sound like proper English. Programming constructs being similar to English can lower the learning curve in some ways, but if the people maintaining your code try to understand it by translating it word-for-word into English and then trying to understand that, then you've got worse problems than whether or not they use "unless". Only fairly trivial programming constructs make good sense when translating into English. That's why we use tools like charts and tables.

        The "if" of most programming languages is similar to the "if" of English, but not identical, so conclusions drawn from one don't perfectly apply to the other. But there is significantly more difference between the "unless"es of Perl and English. And your choice of examples illustrates this nicely. Let's translate your sentences into Perl code:

        DontSpeak() unless SpokenTo(); DontSpeak() if ! SpokenTo();

        You see, I've never seen a subroutine / method named something like "DontSpeak". Most programming languages don't work that way. So I'll take a small detour on the route to explaining why and showing what that has to do with "unless".

        The "if" of Perl is simply "if <present condition> then <do action (now)>" (though "present condition" could include "remembered state of past conditions"). The "unless" of English is most often used like "unless <potential future condition> don't <take action (future)>". But the "unless" of Perl is "unless <present condition> then <do action (now)>". And it doesn't really fit.

        Tell someone "Speak unless spoken to" and you'll probably be able see their mental cogs spinning as they try to figure out what you really meant.

        So, writing return unless SpokenTo(), then translating to English as "return unless spoken to" can easily beome, in your head, "don't return unless spoken to", because that is how "unless" is normally used in English.

        If you could write:

        unless( SpokenTo() ) dont { Speak(); }

        Then "unless" might be less of a problem. But that pretty fundamentally goes against the way Perl works (it might work in a declarative language like Prolog).

        The implied "not" in "unless" is subtle and there is also a customary second "not" that is subtle but missing from the Perl "unless". So when use "unless" in Perl code it is too easy to switch it to "if" in your head to avoid this dissonance or to add a "don't" to the action. And when you do this, it can be a long and frustrating process to find your back.

        - tye        

        use Acme::don't;
        don't { print "This won't be printed\n" }; # NO-OP
        How about the TheDamian writes another one ;-)
        use Acme::not_do; not_do { print "This won't be printed\n" }; # NO-OP
      The "not" that is implicit in "unless" is subtle enough that it can get "lost" when you are looking at code.

      I never had that problem. For me, the "not" cannot be lost, because without it it's not unless any more.

      This shows that not only shouldn't you use "unless" even for simple conditionals, but shouldn't even consider using "unless".

      No, it does not. That's an undue generalization. You may not advocate the banishment of a language construct as a general rule just because "quite a few" got "burned by it now". There's no count of those which never got burned by using 'unless', and which are quite comfortable using it as a statement modifier or a control construct in the sense perlsyn states:

      "if" executes the statement once if and only if the condition is true. "unless" is the opposite, it executes the statement unless the condition is true (i.e., if the condition is false).

      Subtleties in the english language regarding 'unless' are irrelevant and must not be stressed to daemonize it's use. After all, we're programming perl, not writing english statements.


      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        I'm not banishing unless from Perl. But I have a collection of 'best practices' for writing highly maintainable code and not using unless is one of those.

        I never had that problem.

        And I said that my experience is that people won't "get it" if they haven't seen it. You don't have to try to learn from my experience of having done this and having seen other people do it several times. I didn't see the problem with unless until... I personally saw the problem with unless. (Well, I, like many of us, quickly saw that unless should be avoided in many situations; but I didn't initially see the problem with using it with very simple expressions.) The problem is subtle but the effect is maddening.

        No, it does not. That's an undue generalization.

        You dropped my smiley. Unwad your panties.

        Subtleties in the english language regarding 'unless' are irrelevant and must not be stressed to daemonize it's use. After all, we're programming perl, not writing english statements.

        I wasn't the one who brought the English language into the argument. The English language was used to justify the use of unless in Perl and I countered that argument. You can't banish the use of the English language in an argument about a programming language whose keywords are mostly English words. Besides, the problem with unless have much to do with the use of "unless" in English. Really. So you can tell me that I "must not" make my argument and thus demonstrate that you didn't understand my argument, but I will continue to try to express this difficult-to-express point whether you think my methods should be banned or not. I explained why it is relevent. You can declare that "it is irrelevent" but you'll have to back up such a declaration with more than "we aren't writing English statements". My argument never hinged on being confused that I thought we were writing English statements. My argument is about the word "unless" and subtle ways that it impacts comprehension (by English-speaking programmers) of code that uses that word.

        You must not use emotionally charged words like "daemonize" to mischaracterize my educating people about my experiences and attempting to explain my best practices.

        There's no count of those which never got burned by using 'unless', and which are quite comfortable using it

        I counted as one of those. Now I know better. The people I've seen burned by this were quite surprised when it happened. It took a while to even realize that there was a pattern to this. I don't think that there are two types of people in the world, those who understand unless and have no problem using it and those who have difficulty understanding it. It isn't even in the same ballpark as rocket surgery to understand that, in Perl, "unless" just means "if not".

        Anyway, in case you didn't notice, I got a chuckle out you responding to my use of "shouldn't" (in one sentence with a smiley attached) with declarations that I was trying to "banish" and then you using phrases like "you may not" and "must not". I forbid you take this so seriously, henceforth. q-:

        - tye        

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2022-05-27 14:41 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (95 votes). Check out past polls.