As someone who's doing this as a side-project (gotta love those!) right now, I can tell you these are the steps to take:
  1. Print out the whole thing and take it out of the office. Sit down in a Perkins/Denny's/etc. and, with a bottomless cup of coffee and a new pack of smokes, read it the whole way through. Make notes. Highlight everything. Cross out everything you think it useless.
  2. Go back to the office the next day (very important ... you need a day to digest all you just read) and run it once, without modification. This is to make sure you have a baseline set of output.
  3. Make a copy in another directory. DO NOT EDIT THE COPY YOU WERE GIVEN. You will make mistakes and need to start over.
  4. Add "use strict". This is so critical I'd repeat it if I was being pedantic.
  5. Add "use strict". (Ok, so I'm being pedantic.)
  6. Once "use strict" runs and the output is identical, you can now start editing.After every change, re-run the code and verify the output hasn't changed, except for what you deliberately changed. I cannot emphasize that enough. You're trying to clean up someone else's code. Don't add your own bugs. If you do, you'll go nuts, cause you won't know what part of the code is your bug and what part is his badly-written schlepp.
  7. Identify repeated blocks of code and encapsulate them in functions. These blocks just need to be functionally equivalent, not letter-for-letter equivalent. You can always pass in parameters or hashrefs or whatever.
  8. Start to make a stab at clarifying the data structures. For example, if someone is using $A, $B, and $C, make a hash with keys of ('A' .. 'C'). Most likely, you'll be able to reduce the number of lines and improve readability. You'll definitely save your sanity. (And, possibly, improve run-time.)
  9. Get rid of EVERY global variable. Then, if you find that you absolutely needed that one global, re-add it and document the hell out of it. Everywhere. Don't miss even one spot.
  10. Remove un-needed code. Do this very delicately. It's the place most likely to break your output.
    1. Comment out a section.
    2. Re-run the code.
    3. Delete the commented section.
    4. Rinse and repeat.
  11. Think about making what you're doing a module (or group of modules) and write a script that calls those. Maybe you should even make this OO (though that rarely is called for).
Remember, you're trying to save the next poor schmuck the heartache you just went through. Document and comment thoroughly. Don't take a short-cut just to save 20min. Do it right.

Another suggestion - in your specific case, I'd look at getting rid of the SQL script and using DBI. I'm a little suspicious of marrying Perl and SQL scripts. There may be a good reason for it, but that's what I'd look at. Also, if there are time issues in a run, most 1200 line SQL scripts tend to be very badly written. I remember re-writing a 5000 line PL/SQL module, primarily just changing the order of the SELECT calls, and changing the run-time of a user screen from over 1 minute to under 1 second. That made the screen (one of the most important in the tool) actually useable.


In reply to Completely rewriting someone else's deprecated code... by dragonchild
in thread Seeking advice about learning another's code by E-Bitch

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!
  • 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, details, 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, summary, 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.