First Step Legacy Code Cleanup

“Legacy” probably means code written without a sense of craftsmanship, much less a code standard. It works, but the comprehension burden on the support team is high, as has been documented numerous times.

Many podcasts and articles mention that software development is still done today without source control. As bizarre as this seems, if this state still exists, then your worst legacy code nightmare is also present many times over! Here is a simple approach to start to move from the burden of “legacy” to something you can take pride in.

As you develop the new feature or bug fix, take note of the junk:

  1. Commented out code that is so old, that context is no longer understood.
  2. Constructors that don’t use initialization lists.
  3. Un-used variables.
  4. Unfortunately, many folks learned C as a first language. It used to require that variables be defined at the beginning of routines instead of close to usage.
  5. Methods or procedures that are hundreds of lines long.
  6. Extremely poorly named variables: my favorites are NP and MP, both of which point to new projects in a 300 line method.
  7. What about duplicated code? Are you looking out for that?
  8. Approximately 5,000 more sloppy practices…

This sort of junk greatly impedes comprehension. It can take hours to develop basic understanding, with low confidence! And for this, we’re paid big bucks.

So what to do:

  1. Assume ownership of the code. You are king and you rock!
  2. Fix to your hearts content, do not commit.
  3. Compare your work with the original.
  4. Commit your work, *except* for the feature/bug fix.
  5. While waiting on the review of this, re-implement the feature/fix and commit as a 2nd action.

Why two commits? The first gives the team confidence that the clean up is stable. It takes “extra” time, but the journey of 10,000 miles starts with a single step. Not only does this fix up a particular bit of code, the team probably also needs growth along these lines as well.

Just to belabor the point, a method was recently found to be of the form:

int max = 50;
for( int x = 0; x < max -50; x++ )
loop work...

Clearly a unit test would have found this. How many of us don’t have unit tests? Ahem. Be honest. It’s time to start somewhere!

PS. the PMD tool is great for tracking down duplicated code. Invaluable.

First Step Legacy Code Cleanup

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s