DRY fixing bugs
Being a developer we often spend more time fixing bugs than developing features(introducing new bugs 😜).
As the code evolves, bugs increase as well. We often see more bugs being added to bug backlog than being fixed in the sprint.
Bugs in production may not just cost millions of dollars but often days of the time that would rather have been spent in development.
One fine example of such a bug is Papa Musks’ PayPal accidentally credits man $92 quadrillion
Many practices such as Clean Code, Test Driven Development, Code reviews aim to minimise bugs in production. But even with these, it is almost impossible to have completely bug free systems.
More often than not we might end up fixing the same bug multiple times as the code evolves.
Let us see a few simple steps to the DRY (Do not repeat yourself) approach when fixing bugs.
Making it a habit to always cover the bugs that you have fixed with tests makes sure that the bugs never go into production again.
The test fails whenever some change causes it to break thereby enabling early detection in the CI pipeline. These tests provide a strong safety net around the code to make sure at least it doesn’t break for the same reasons. These test increase confidence in code and in yourself ensuring you actually understood the bug and fixed it correctly.
Moreover, the same tests act as documentation for whoever looking at the code for the existing or potential behaviour.
Your peers and code reviewers always appreciate tests around your work as it explains how well you understood the problem and your thought process fixing the bug. It also helps to point out any incorrect assumptions if any that you might have as per the expected behaviour.
All in all writing tests for bug fixes saves a lot of money and effort by ensuring early detection.
A bug caught in production costs a million times more than in testing.
To ensure this once you are done with fixing the bugs make sure to follow these 3 steps:
- Does fixing the bug break any existing test? If yes, fix it.
- Do the existing tests cover the entirety of the bug? If no, add more tests
- Make sure to add tests for every place you have touched in case they aren’t there already making sure you don’t introduce new ones on your way to fixing the existing one.