(Note: the presentation (pdf) is inside the https://github.com/supernelis/workshop-renovating-legacy-codebase repository).
If you have an existing code base without (many) tests and you want to start CI/CD, what do you do?
Often you’re a bit stuck, because you have a catch-22 situation:
I can’t safely improve my design without having decent tests.
It is also hard to add tests without changing the design…
A way forward is a golden master. The term comes from the middle ages, when monks copied books. After the copying, the book was compared to the master’s one.
For programming, you can look at the current system’s output and compare it to the refactored system’s output: are they the same? This is a very quick way to at least start testing the complete codebase.
An important question is is my golden master effective? So: how sure can you be that a bug gets caught if you start changing the code? There are two ways:
Code coverage. A higher percentage gives you more confidence.
Mutation testing. Introduce a couple of bugs in the system and see how many
of them get caught. Change all the if
statements to if not
one after
the other for instance :-) Or change >
to >=
.
Such a golden master is only good for refactoring an existing system without changing the behaviour. As soon as you add features (which changes the output), you suddenly have to change and check all the expected-output files….
Refactoring: improving the internal structure of the code without changing the external behaviour.
Some typical refactorings:
Extract functions/methods.
Change “magic numbers” into constants with proper names. (So no more “1972” but “START_YEAR”).
Extract variables or parameters.
Extract classes.
Etc.
My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):