Some people find debugging more difficult. Those are people who believe that debugging is to think about what is happening rather than looking at what is happening. For Example consider a web page displaying user data which is randomly failing to display the data. What is your reaction to this question: “Why?”, if you immediately start assuming the reason then you are doing the wrong thing.
The right answer to this question is “I don’t know”.
When you start debugging, realize that you do not already know the answer.
It may make you think that you already know the answer. Sometimes you guess something and that could be right but it doesn’t happen very often. It will make people think that randomly guessing the answer is a good method of debugging which is actually not. Most of the time you will spend hours or days guessing the correct reason and trying different fixes with no result other than complicating the code.
“What should you do?”
Guessing is a waste of time, imagining reasons for the problem is a waste of time. The only things you have to do with your mind are:
1. Remember what a working system behaves like.
2. Figure out what you need to look at in order to get more data.
“Debugging is accomplished by gathering data until you understand the cause of the problem.”
The way that you gather data is, almost always, by looking at something. In the case of web page not displaying the data, you would look at response from the server or you can check the behaviour of the code. You need to reproduce the problem so that you can look at what happens with the server when the problem is happening.
Once you get clarified on the bug, you have to go and look at various parts of the system. Which parts of the system you need to look at is based on your knowledge of the system. Usually it is logs, error messages, server responses etc.
Remember that you have to remember what a working system looks like. Eventually you will find something that a working system does not do. You should not immediately assume that you have found the cause of the problem, you need to get down to the basic cause of the problem. And of course, once you have the cause, you fix it. That’s actually the simplest step if you have done everything else right.
Usually if you fix a bug, you are actually removing some part of code or enhancing your code.
“When debugging, novices insert corrective code, experts remove defective code.” – Richard E. Pattis
Debugging can actually be fun and interesting but note that a good developer should not let his/her software misbehave.