“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:
- Commented out code that is so old, that context is no longer understood.
- Constructors that don’t use initialization lists.
- Un-used variables.
- 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.
- Methods or procedures that are hundreds of lines long.
- Extremely poorly named variables: my favorites are NP and MP, both of which point to new projects in a 300 line method.
- What about duplicated code? Are you looking out for that?
- 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:
- Assume ownership of the code. You are king and you rock!
- Fix to your hearts content, do not commit.
- Compare your work with the original.
- Commit your work, *except* for the feature/bug fix.
- 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.