[Purdue-pm] RADpools
Phillip San Miguel
pmiguel at purdue.edu
Wed Oct 20 06:43:45 PDT 2010
On 10/20/2010 8:59 AM, Rick Westerman wrote:
>
>> The code in question (with extra comments):
>>
>> for my $i ( 1 .. $mid_length ) {
>> for my $base (qw{A C G T}) {
>> my $fuzzycode = $mid;
>> my $prebase_i = $i - 1;
>> my $postbase_i = $mid_length - $i;
>> $fuzzycode =~ s{
>> ^([ACGT]{$prebase_i}) #capture bases, if any, before
>> current base
>> ([ACGT]) #current base
>> ([ACGT]{$postbase_i})$} #capture bases, if any, after
>> current base
>> {$1$base$3}xms; #replace current base with $base
>> push @{ $mid_pools{$fuzzycode} }, $pool_name;
>> }
>> }
>>
>> Actually, I don't see any problem with this code.
>
> Readability is the issue. IMHO, the author is using a sledgehammer
> when a peen hammer would do. Speedups are not the issue. The
> following is much more clear ... especially to the the novice Perl
> programmer who is not that familiar with regexes.
>
>
> for my $i ( 0 .. $mid_length - 1) {
> for my $base (qw{A C G T}) {
> my $fuzzycode = $mid;
> substr($fuzzycode, $i, 1, $base);
> push @{ $mid_pools{$fuzzycode} }, $pool_name;
> print "$fuzzycode\n";
> }
> }
>
>
> Explaining substr should be a piece of cake compared to regex captures
> and replacements.
>
Your code is more compact. But the original code spoke to me. I could
immediately tell what the author intended to do. Your more compact
implementation does something very similar to what the original code
does, but were I to read it de novo, I doubt I would have any idea what
it was doing.
Regex captures and replacements are important parts of perl. So they
need to be explained sometime. I think you guys are hitting the "I saw a
problem and decided to use a regex to solve it, now I have two problems"
meme too hard. Unfortunately the meme is funny, which gives it more
persistence than it should be allocated.
That said, now that I do understand the code, I would go with the more
compact form.
Phillip
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/purdue-pm/attachments/20101020/c5b3cb3f/attachment.html>
More information about the Purdue-pm
mailing list