develooper Front page | perl.perl5.porters | Postings from February 2015

[perl #123898] [PATCH] Modernize the second part of perldsc.pod.

Thread Next
From:
James E Keenan via RT
Date:
February 21, 2015 19:55
Subject:
[perl #123898] [PATCH] Modernize the second part of perldsc.pod.
Message ID:
rt-4.0.18-14251-1424548499-599.123898-15-0@perl.org
On Sat Feb 21 07:49:48 2015, shlomif@shlomifish.org wrote:
> This patch modernizes the second part of perldsc.pod. It adds declarations
> using my, replaces "-w" with "use warnings", extracts repeating expressions
> into variables, and some other changes.
> 

Shlomi,

In some of the cases where you "extract repeating expressions into variables," I think that that extraction is not needed and just adds one more line to the code example.

For example, under the section "Access and Printing of an ARRAY OF ARRAYS", we currently have this code example (blead, starting at line 387):

#####
 # print the whole thing one at a time
 for $i ( 0 .. $#AoA ) {
     for $j ( 0 .. $#{ $AoA[$i] } ) {
         print "elt $i $j is $AoA[$i][$j]\n";
     }
 }
#####

After your revision, this section would read:
#####
 # print the whole thing one at a time
 for my $i ( 0 .. $#AoA ) {
     my $row = $AoA[$i];    
     for my $j ( 0 .. $#{ $row } ) {
         print "elt $i $j is $row->[$j]\n";
     }
 }
#####

The assignment to $row *does* mean that the lines in the inner loop are easier to read.  For training someone in Perl, that's perfectly fine.  But $row is, IMO, an example of what MJD long ago described as a "synthetic variable" ripe for refactoring away.  AFAICT, it does not reduce the number of array element lookups we have to do subsequently.

I'm currently attuned to this because I've been refactoring one of my older CPAN distributions with the objective of speeding up its performance without changing the interface.  I've found that eliminating unnecessary assignments to synthetic variables is an easy way to get a measurable (though usually modest) improvement in function performance.

The other changes in this patch are, IMO, quite satisfactory.

Thank you very much.
-- 
James E Keenan (jkeenan@cpan.org)

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=123898

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About