Introduction
Bugs are part of the daily work of programmers. Everyone hates them: the tester, the developer, the manager and the customer. So it is pretty clear, they should be removed as soon as possible. But what is the most effective way to do it. I can still remember, right at the beginnin of my career I needed to find a bug in a quite complex system and fix it and I was totally lost. So in the post I will share with you my way of “bug hunting”.
Before bug hunting
So the very first step is on the level of understanding: you should understand what should be the normal behavior of the programm and you should understand what is the bug itself and why is the buggy behavior not correct.
As next step you should be able to reproduce the bug. Never start to search for the root cause without reproducing it. It can also happen that the bug is not appearing at all on your system. At the reproduction phase make sure always about the following points: you are testing the same version of code as where the bug appeared, you are using the same operating system, all settings are the same, all other conditions are the same.
So if you would like to document a bug always write down the steps to reproduce the bug, the expected behavior, the current buggy behavior, the exact software version, all software settings and your system info.
Once you reproduced the bug you should find the bug, there are two ways from this point.
Finding the bug
The first one what I found really effective is based on the version controlling of the code. You should find the last version where the bug was still not present. Once you found it check which changes have happened between the last working version and the first buggy version, quite often you will find root cause at this point immediately.
The next method you really need to start with debugging. You need to try to identify first the component which includes the bug. For that you will check the inputs and the outputs of different components. One you found the component try to go one level down: on class level and later on do it on function level.
There are some things you need to think about always: if there is multithreading in your code think about the shared memory areas. If there is dynamic memory allocation always take a look if you did everything well with the memory. And if you are using 3rd party components always make sure if they are providing the currect values for you.
With a professional debugger software like gdb the duration of debugging can be much shorter. The other thing which can make the debugging faster is if the code is covered by unit tests. You simple need to check which unit tests are failing and it will lead you to the root cause.
These are my basic practices and I think with practice it will be just faster and faster to find the bugs, but the bugs can surprise you everytime.