Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and the shadows deepen...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (3)
    As of 2018-02-25 14:20 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (312 votes). Check out past polls.