A lack of tests discourages refactoring

A common cause of not refactoring code is a lack of test coverage.

Why change some code if it works? What if we break it whilst refactoring it and introduce a regression?

If the code has accompanying automated tests, this can’t happen.

The tests should be passing before starting a refactor and should be passing once the refactor is complete.

If the tests fail, the refactor was unsuccessful and should be reverted.

If you have a CI pipeline, a failing test should break the pipeline and prevent any further steps if you’re doing continuous delivery or deployment whilst signalling to the Developer their refactor was unsuccessful and the pipeline needs to be fixed.

Without tests, the code could be broken during the refactor, and we wouldn’t know.

Was this useful? Sign up here and get more like this delivered straight to your inbox every day.