Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Completely rewriting someone else's deprecated code...

by dragonchild (Archbishop)
on Aug 02, 2001 at 21:19 UTC ( [id://101717]=note: print w/replies, xml ) Need Help??


in reply to Seeking advice about learning another's code

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.

  • Comment on Completely rewriting someone else's deprecated code...

Replies are listed 'Best First'.
Re: Completely rewriting someone else's deprecated code...
by rvosa (Curate) on Jul 30, 2005 at 00:37 UTC
    Might I suggest using a revision control system instead of (or in addition to) copying the source to your own "work copy"? You can simply run subversion on your own machine, and you can even commit from within Komodo, for instance. You'll feel far, far more comfy making changes if you can always revert to whatever earlier version you ever committed.

    Oh yeah, and write some tests, to make sure you don't break anything while working through the code.

    And run the code through perltidy (possibly after deparsing).
      I wrote that four years ago. In that time, a few things have happened, like the creation of subversion and improved testing facilities within Perl. I fully agree with everything you're saying, and would add those items in as part of a refactoring process.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found