On Saturday I spent the day near Leuven on the first publicly published Legacy code retreat. We were given an existing code base which seemed rather small, I guess around 400 lines of code, but after digging a bit deeper my colleagues and I found out that it was quite a monster.
Several techniques were covered. Subclassing to override behaviour, introducing a golden master, moving behaviour in collaborating classes and making “pure” functions.
The golden master is probably best suited if you encounter a black box and need to make a change. The code base had a bunch of console.writelines so if you redirect the output and run the applications a number of times with different kinds of input, 10000 times was suggested as a good number, you end up with a number of test files. With those in place can then make the change and compare the new output with your golden master. Automate this and you have a reasonable case. It all depends on having some kind of instrumentation in place so you can harvest this kind of information.
Creating subclasses to override, or rather get around, methods in order to test which paths the program flow follows is something I had done before. Heck you even do that with “new” code when you’re stubbing/mocking, but we were not allowed to use any sort of framework so we had to hand roll them. This eventually leads to a situation where you’re testing more the subclass rather then the system under test.
The answer to that resulting code base is to abstract the code you have to other new classes which are injected through the constructor. Typical dependency injection and inversion.
The last technique was the most eye opening to me. It was suggested to make “pure functions”, meaning any method you wrote was not allowed to directly change the object state. Somehow I was able to see code duplication and underlying algorithms a lot faster throughout the code base. Probably need to look a bit into functional programming as it was quite interesting.
The last two iterations we were allowed to keep the code we were working on, in sharp contrast to a normal code retreat, wich gave us some more sense of accomplishment by improving the code base.
I really liked my first code retreat, gave me a change to work with people who work in different parts of the industry, embedded programming to name one and from different parts of Europe.
Looking forward to the next one.