oliverdavies.uk/source/_daily_emails/2023-09-09.md

1,007 B

title pubDate permalink tags
A lack of tests discourages refactoring 2023-09-09 daily/2023/09/09/a-lack-of-tests-discourages-refactoring
automated-testing
test-driven-development
refactoring
software-development

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.