Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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; .... .... }

In reply to Re^2: Regex Or Die! by TheDamian
in thread Regex Or Die! by pileofrogs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found