Fix a bug or root cause of making the bug?
What is the root cause?
So you are taking a new bug. What are your next steps? I guess something like:
- try to reproduce it with the (hopefully) provided conditions
- debug code or look directly into the code trying to understand faulty lines
- fix it
- test it to be sure that it’s no more reproducible
These steps are the basic ones, of course much more can be done, but let’s not dig deeper. I just want to add one more important step (in my opinion) between the 2nd and 3rd one:
2.1. understand why the owner of the mistake did it and try to improve the code to help avoiding it
If this is obvious for you, I am grateful, because at the end it should help in the future to avoid further mistakes in this area. Just to be sure that we are on the same page, here are some simple examples of possible causes:
- wrong or confusing variable/function names, which don’t really do what their name says
- too “smart” code logic, which is not easy to understand in a few seconds
- too complicated conditions with AND/OR combination and maybe even combinations of combinations
- and even wrong formatting can bring confusions and mistakes
So I advise you to always try to really understand what caused the mistake and at least think if you can easily improve this code or create some task or a note to take a look in the future. Of course it’s additional time and effort, but this will remove one bomb on the field where you walk almost every day. So definitely it is better to walk more safely instead of hoping to not step on it tomorrow.
Illustration-example of my favorite aligning mistakes
Step 1. We have a requirement to put red and green squares with specific space between
Step 2. Then in the game enters a blue square which also should have a small space between it and the red one
Step 3. After some time we received a new requirement to increase space between blue and red one and we got the next result
Step 4. Somebody found that the spacing between red and green is wrong and created a bug to restore the previous one. If we will not dig deeply enough, then most likely we will move just the green one, the bug is fixed and everybody is happy.
But what if after some time we will receive a new requirement to reduce in half the space between blue and red squares. What is the chance that will be moved just red one and again introduce the “wrong space between red and green squares” issue?
Maybe in this simple example the chance is really low, but when behind the simple squares is hidden a complex code/layout/logic I think the chance is very high. There can be even worse loops “introduce and fix bug” on any change of red square’s position.
But what if, instead of simply moving the green square in Step 4 to fix the bug, we investigate and understand that Step 3 (just moving the red square) is the change that brought us the regression? Maybe we will find that the root cause of the mistake is that red and green squares with defined space between them are not logically or physically considered as one single object.
So wouldn’t it be better to go back into Step 2 (when blue square was just added) then wrap red and green squares into yellow rectangle?
And only after that apply the new request “to increase space between blue and red” squares
This fixes the bug and also protects us to not break space between red and green squares on any red square’s future position change.
Conclusion
Try to spend some time to understand why a mistake was introduced originally and try to improve the code/layout/logic to not let you or other lucky man to make it again in future. As a result you should have much more understandable and logical code, which will reduce the chance to make a mistake.