Refactoring Perl code

Posted on October 15, 2003
Filed Under /dev/null/ | 61 views |

Michael Schwern has written a cool article on refactoring Perl code, via a step-by-step analysis of an existing function: A Refactoring Example.

About a year ago, a person asked the Fun With Perl mailing list about some code they had written to do database queries. It’s important to note that this person was posting from an .it address; why will become apparent later.

I decided that rather than try to read through this code and figure out what it’s doing and how to make it faster, I’d clean it up first. Clean it up before you figure out how it works? Yes, using a technique called Refactoring.

One of the most important principles of Refactoring is that you work in small steps. This re-indentation is a single step. And part of this single step includes running the test suite, logging the change, and checking it into CVS.

It’s nice to see that I’ve been doing a large chunk of what he recommends on the current perl project I’m on (porting non-portable code from one server to another); I also picked up some exellent tips from this article (though this:

my $totalvisit_sth = $dbh->prepare(<<'SQL');
SELECT count(*)
FROM lognew
WHERE pageid = ? AND
data >= ?
SQL

is the oddest way to write a prepare() statement I’ve come across). A worthwhile read. (Via The Farm)

Comments

Leave a Reply