Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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


In reply to Re: So I'm in a bit of a quandary by toma
in thread So I'm in a bit of a quandary by vek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (13)
    As of 2014-09-19 18:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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











      Results (144 votes), past polls