Have courage to change legacy code.

By akash_rawal, created: 2023-10-27, last modified: 2023-11-18

Are you afraid to change existing code because 'if it ain't broke, don't fix it'? You might want to change your mind.

Changes are inevitable.

Your software works well today. Tomorrow your customer might ask for a new feature. Security vulnerabilities may be discovered in your software or libraries and frameworks your software uses. Your software may need to adapt to newer standards or interoperate with other software built recently. Or your management is asking you to increase test coverage.

Either way, I have seen simple changes being delayed by days or weeks, just because the engineers don't have courage to change the code. It is fruitless to be afraid however.

There will be issues.

Legacy code often does not have tests to provide a safety net. The code can fail in ways you might not foresee. You'll uncover bugs that have laid dormant for years. When change is necessary, be prepared for these outcomes.

Recognize the patterns.

Have respect for those who wrote the code. They were limited by what they knew at that time and did their best. So there will be patterns. However bad they are today, those patterns were the 'good practices' they knew at that time. Learn to recognize them.

Setup a fast build system.

This will help you change the code easier by reducing your code/test cycle time. If you learnt to recognize the patterns, this might be easier than you think.


Always improve the code that you changed in some way. Focus more on improving code that is changed more frequently. Refactoring the code that has no business reason to change is often fruitless, so channel your energy elsewhere.


Use automated testing to reduce the likelihood of adding defects. Add approval tests to avoid changing behavior of existing code. You might still break things in brand-new ways, so be prepared.

There will be payoffs.

You'll be the one who can fix complex issues in the codebase. You'll lay the groundwork for setting up automated testing that catches real bugs. You'll enable the rest of the team to work more quickly and deliver sooner.