Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: So I'm in a bit of a quandary

by toma (Vicar)
on Jul 09, 2002 at 05:16 UTC ( #180415=note: print w/ replies, xml ) Need Help??


in reply to So I'm in a bit of a quandary

I have run into this situation many times, although the code I have to upgrade is typically my five-year-old perl.

I have come up with a refactoring methodology that works for my own code, your milage may vary.

It's not such a big deal. Just add use strict; to the top and fix it until it passes
perl -c old_crufty.pl

Here's a cool trick where perl really shines at refactoring: as you add variable declarations, typically my $var;, try to put the my as deep as possible into the enclosing braces. This minimizes the scope of all the variables, and will help later.

The suggestions in this thread about unit test are excellent. Build a few tests and run them on the big mess of code that passes use strict;. Even a few simple tests are vastly superior to no tests.

If you aren't using revision control, start now. I only have to remember two RCS commands to make my life safe from a bad coding day.

Usually the crufty code is a long main that can be broken into a simple sequence of subroutines. Perform this breakup and pass the needed variables to the subroutines. This is the tricky part. If it's really okay code, just written in a linear style, there will be natural breaks and this won't be so hard. If the existing code is really a mess, this is where you give up and do a rewrite. Since you say the code works, there is hope that it is not so bad.

If you can figure out how to make one big pile of code into a collection of slightly smaller piles, you win big. I have a really ugly trick for making this work. I take the mass of globals that get passed about and put them into a hash or two or three, grouped by their functionality. Then I pass references to these hashes to the subroutines, where I use something like my $var= $ugly_hash->{var}, changing all the global variables into new local variables which are just copies. To pass the variables back from the subroutine, I just use my $ugly_hash->{var}= $var.

Once your code has survived the testing and been through this procedure, you have some code that is still quite crufty, but it has a few key advantages. It is still readable by your boss, who hasn't ever written OO perl, it is much easier to maintain. If you get the chance someday, it is easy to clean up, and you might even get to convert it to true OO perl.

I actually think this sort of work is fun. It does not have to be drudgery and it does not require an onerous amount of analysis. You should see much improvement with only forty hours of effort, or you should use a different approach such as a rewrite.

It should work perfectly the first time! - toma


Comment on Re: So I'm in a bit of a quandary
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2014-09-18 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (105 votes), past polls