Turnleaf Design Ramblings of a junior developer

30Sep/094

6 Steps for fixing any bug

Hate bugs? Yeah everybody does. Here are six steps that should resolve virtually every bug you encounter.

1.Take responsibility – About 95% (a total guess) of the time if your application is not working as expected it is because you did something wrong. We all take pride in our code, but it pays in this case to be humble. You will save yourself a lot of time and potentially face by first checking your code to see if there are any coding problems.

2.Narrow down where the bug occurs – A natural step, if you want to be able to fix a bug you have to know where to start. Check the application log(s) to see if an exception is being thrown, use you intuition, or otherwise start at the highest reasonable level that would encapsulate the bug and start drilling down from there (e.g. if people can't login, then start with the login page).

3.Write a unit test – Once you have an idea of where the problem might be write a unit test. Check to make sure you are getting the expected results out of your method calls. Keep writing unit tests until you get to the finest reasonable level of granularity. If you do not know how to write unit tests here is a brief tutorial.

4.Use the debugger – Depending upon the complexity of your system or unit test, you may want to step through your code line by line to; see what message are being sent, how the program is being executed (lines executed), and how the value of variables are being modified.

5.Write to the logs – If you are still not able to resolve the problem, it doesn't show up in you unit test(s) (you may want to write better unit tests), or are unable to use units tests or a debugger (for example the bug is in a JSP) then include logging statements in your code and look for where things go awry.

6.Seek help – If you still can't figure the problem out (or can't come up with a solution) seek help from your peers. Find someone who is either really good with programming or knows the application you are working on very well.

Addendum

So if you ran through all these steps and still can't figure out the problem you may not have screwed up. Double check your work and what you have done to make sure you have got everything covered. If everything looks good (or I guess bad) gather test data so the issue can be recreated and when you contact the person/vendor who you believe is responsible for the bug, give them the test data and explain as thoroughly as reasonable how to recreate the issue. If you don't do this then they may blow off your problem, because after all they have work too and don't have time to go searching for problems.

One final note

I wrote this article in contest to this article which was recently posted on reddit . I disagree with the authors steps in resolving a bug. That is not to say the author is incorrect, just I disagree with him/her. I would suggest also reading that article (I agree with the “good practices”) and decide what is the best way to fix your bug(s). if you have any questions are steps you want to add let me know in the comments.

Update (10/2):

It seems this article has become a conversation. Dustin Marx of http://marxsoftware.blogspot.com/ has expanded upon this topic. I suggest giving his article a read. His article contains information that is likely more relevant and useful for more experienced developers. Thanks Dustin!

Bookmark and Share

Technorati Tags: ,

Related posts:

  1. A brief intro into unit testing with JUnit
  2. An Intro into Test Driven Development with JUnit4
  3. Ramblings: Should unit tests talk to a data source?
  4. 8 Signs your code sucks
  5. Mocking JDBC Connections with MockRunner
Comments (4) Trackbacks (1)
  1. Good points, but sometimes very simple bugs occur after a hard work and long time coding, so probably the first step can be: 1- get some rest and recheck your code :) !

    • You’re definitely right there. I know there have been many times I was spending a lot of time working on a bug, however came in the next day and had it fixed within 15 minutes.

  2. Hello from Russia!
    Can I quote a post in your blog with the link to you?

  3. Other variant is possible also


Leave a comment