Saturday, December 26, 2009

When the stack trace just isn't enough

When debugging I usually can find the problem simply by looking at the stack trace or by executing the test case in a debugger. Well then there's those cases when the problem isn't so simple.

Here are my tips for these situations:
  1. Don't panic or give up
  2. Make the system fail earlier
  3. Testing testing testing
  4. Find out the exact version where the bug was introduced to the system
Make the system fail earlier

You most likely can spot what went wrong from a stack trace (object foo was null when it shouldn't be etc.) so you can put assertions to the code that will fail (check if foo is set to null whenever someones setting foo). This can also make it simpler to reproduce the bug as there could be cases where things go wrong but from a user point of view everything is OK.

Testing testing testing

You can reveal points about a bug that are difficult to detect from code simply by testing. In my opinion testing is more objective method than diving directly to the source code (you will always look at the part of the code that you assume to be responsible of the bug --- and sometimes at least I my self will blame the wrong piece of code). For example it can be beneficial to figure out the test cases that are nearly identical to the one that reveals the bug but don't reveal it. This can dramatically narrow down the lines of code that could be responsible of the bug in question.

Shortening a test case that reveals a bug is almost always a good idea. A short test case can be executed quickly and it will focus on the components that contain the bug in question.


Find out the exact version where the bug was introduced to the system

With a quickly executable test case finding out the first version where the bug was introduced should be easy. Once the committed change that introduced the bug is known there usually is only few lines of code that could cause the bug.

This method is great as it can work also in situations where the source code is unfamiliar. Unfortunately there are situations where the method fails or doesn't help much. The bug could have been there forever, another bug in version history can get in your way, developers could have bad habits to do huge weekly / monthly commits etc..

No comments:

Post a Comment