Something I hear and have heard a lot in my life as a programmers is
We should rewrite this, I have no idea what it is doing
Now, your instinct to rewrite is almost always the correct thing to do, but almost always the implementation is wrong. Why is this? Well it’s because of the same reason we want to rewrite it, we don’t know what it is doing. So often we end up causing regressions, or accidentally leaving features out. How do we get around this? This is where I stand up and go to put on a broken record. The answer is unit tests, in fact ask me anything I’ll probably say unit tests.
- “How do I make Koha do this?” “Well first you write a unit test and then …”
- “I want to change this template” “Have you run the xt/tt_valid.t on that?”
- “Want some coffee?” “Is it unit tested?”
- “Where’s the remote?” “Oh, I wrote a unit test for that.”
But seriously, if we invest time in writing tests, forcing us to learn the behaviour of the current implementation, we can then make sure our refactor/rewrite still passes these tests. We can also make sure any new behaviours we add have tests written for them too, after having to implement tests to test existing code, writing tests before writing the code seems a lot nicer.
I’m sure this is new to no one, and I’m really writing it as a reminder to myself, but hopefully it may be some help to others too. Go forth and refactor/rewrite 🙂