2006-04-28

Touching Legacy Code

I agree with Adrian. Refactoring without tests is possibly the worst mistake you can make with a legacy codebase. I have been in situations where I really hated a piece of code I was working with, and so I mistakenly decided to refactor it. Sure, I cut the SLOC by 75%, and it worked better than it ever did before, and I found bugs and fixed them.

Doing it took much longer than I intended, introduced new bugs, and introduced a new maintenance cost. The code had been running fine, now it was not, and I had to fix it.

In my experience, legacy code should be touched as little as possible, and when it is touched, the part that's changed should be tested properly, and the entire thing should have some form of smoketest to make sure you didn't expose bugs elsewhere in the system.

I will now recite the Ode To The Maintenance Programmer

Once more I travel that long dark road
  into someone elses impossible code.
Through "if" and "switch" and "do" and "while"
  that twist and turn for mile and mile.
Clever code full of traps and tricks
  and you must discover how it ticks.
Once again I emerge to ask anew
  "What the HECK does this program DO?"
'rati tags:

No comments: