Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Re: Re: Re: Re: Code review: validation regexes

by l2kashe (Deacon)
on Jul 09, 2003 at 14:05 UTC ( [id://272680]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Re: Code review: validation regexes
in thread Code review: validation regexes

This comes back to the age old, do I code what I can or do I code for whomever may have to maintain this after me. If I bang out a piece of code, I expect that the next person will have a comparable level of experience, be willing to go through the perldocs, or leave the code alone. I tend to shy away from detracting functionality or speed from a code base, due to future maintenance. If they do play with the code, and don't understand what a particular line or block is doing, I expect them to copy the code to another place and examine it there, or deal with breaking the code or other ramifications. I also keep a current/most recent copy of all the code I write in my home directory, just in case I get that phone call "The program is broken.. no we didn't edit it" and diff v1.pl v2.pl produces output. (where to go from there is a different story)

With that said, I'd like to comment on your counterpoints.

If you don't use (?:..|..|..), do you also not use ?!, ?=, ?<, ?>, ?<=, ?>=, ?{}. Its a shame to give up the functionality provided by these idioms, simply due to the fact that they look funny. Worst case a simple # perldoc perlre, would suffice to point the next person in line to the appropriate documentation for those operators.

In terms of my way of collecting a month versus your way. It appears that you didn't actually test the line to see what ends up in $mth, but rather assumed it was the numerical value, which it is not. Its actually the 3 letter abbrev for the current month. Going back to comparable Perl knowledge levels, it's interesting that you called the 1 magical. For me it is a simple powerful feature of perl. Any time parentheses come into play, the context within becomes a LIST (Im pretty sure this is true, though if any monks can think of a situation where this is not true please let me know) With that in mind, a LIST and an array are basically the same (*peers around waiting for the lightning strike*), and can be sliced the same. Here is an example.
my @date = split(/\s+/, localtime); # note the @date, when using an array slice my($mth, $date, $year) = @date[1,2,4]; # Versus my($mth, $date, $year) = ( split(/\s+/, localtime) )[1,2,4];
Both of the lines which assign the values to the scalars, deal with LIST context. The ability to force LIST context on the left or right is an extremely simple and powerful concept, and should be in every perl coder's bag of tricks, along with slices.

One last comment. Yes it is good to provide beginners with a productive way to get something done, it is also good to toss out information that may cause them to have to look at the docs. Lets say I use the (?:blah|foo) construct with the comment of perldoc perlre. If they have a newer version of perl and an older version of the LLama ( as well as the Camel {I checked: some or the ? tokens are talked about briefly on page 68 of Camel 2nd Ed}) book, they will see stuff in that document they would have never known existed, if they relied strictly on the books.



MMMMM... Chocolaty Perl Goodness.....

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Re: Code review: validation regexes
by bigj (Monk) on Jul 10, 2003 at 08:23 UTC

    With that said, I'd like to comment on your counterpoints.

    I didn't want to offense you, all I wanted to say is there are more than one way to do it, and they are all acceptable.

    If you don't use (?:..|..|..), do you also not use ?!, ?=, ?<, ?>, ?<=, ?>=, ?{}. Its a shame to give up the functionality provided by these idioms, simply due to the fact that they look funny.

    In fact, I like to use the non-capturing groups, positive/negative look aheads and behinds and closefisted regexp parts. I use them where they are implied by the algorithm. (In doubt, I choose the way with the least type strokes and the least reading effort). The ?: construct is needed when we want to capture some parts, and some other group parts are only needed for simple grouping. In addition, the ?: construct is important, if the regexp is reused in other regexps (e.g. build with the qr-operator). In the OP's case, he only wanted to find out whether a string is exactly one of some alternatives. In that case you need a group consisting of the alternatives and enclosed by the ^ and $. It doesn't matter whether it is captured or not and in doubt I would prefer the easier solution. The ?: solution is not worse, nor better as you wrote for this task. (It's perhaps only a bit quicker, but for the price of a longer and a bit more complicated script).

    In terms of my way of collecting a month versus your way. It appears that you didn't actually test the line to see what ends up in $mth, but rather assumed it was the numerical value, which it is not.

    To say the truth, I confound the list to the scalar context and I also confound the min of the doc as mon(th) :-( But please note, that it is also simple to get confused, as the list context is suggested by the (...)[1] part, allthough it comes from a split. Of course, it was my error, but as I think making errors is typical for humans - at least for me - thus I prefer to program as directly as possible. That involves, trying to express the algorithm without any indirection. A split call has nothing to do with time per se, the (from me called magical) 1 has nothing to do with months per se, it is only technical. Of course, it is a common idiom, but the algorithm is hided on the first glance.

    In contrast, my prefered solution my $month = strftime "%b", localtime; is at least not more cryptic, it's shorter and for a lot of typical date formats, there are handy, simple to understand shortcuts (like %d for day, %m for month (number), %y for year, %w for weekday(number) and so on). It is simple to change and often simple to understand, even without a manual.

    Don't misunderstand me, I don't want to say, that your style is wrong. Indeed, I like it also. But it is also a good style to program in another way and it is also quite full acceptable to advert readability and error preventing style.

    Greetings,
    Janek
      Well spoken :)

      I also wasn't trying to offend, simply trying to extend the conversation a bit. Personally I think that threads like these will be beneficial for people passing through later, as they can see a few different sides of an issue and follow not only the technical reasoning, but also the stylistic reasoning for why this snippet is better in this case, over a different snippet.

      With that said, I was glad to chat with you, and I think I'm done with this thread ;).

      P.s: I've been thinking about the problem, and I think the way I would solve the particular issue of "reserved" usernames might be
      my @reserved = qw(administrator mail-admin spam-admin); die "Please choose another name\n" if ( grep(/^$username$/i, @reserved +) );
      A hash would also work, but then if you want $hash{$user}, you have to do your own manual case checking (or can force a particular casedness), or grep (/blah/i, keys %reserved), or possibly grep(/blah/, each %reserved)



      MMMMM... Chocolaty Perl Goodness.....

Log In?
Username:
Password:

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

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

    No recent polls found