What advice can I provide to help you debug your code?

In this episode, I give you seven tips to help you debug your code. One of the students brought in a programming assignment to the live class last week. This assignment was from another class and I explained some of these tips and we used them to solve the problem.

Here are the tips that I recommend to help you fix software bugs:

  1. Make sure you know exactly what is expected and what’s not working. Just being very clear about what should happen and what’s actually happening will be a huge help. sometimes, the problem is just a misunderstanding.
  2. Try to isolate parts of the code. If you can replace methods with versions that just return known good data, then you can start narrowing down your search.
  3. Try to simplify your data. Just like the previous tip, this will help you to narrow down your search.
  4. Focus on the edge cases and try to purposefully cause some bugs. Fix the bugs and you just might fix the original problem. Sometimes a bug is similar to an edge case. What do I mean by this? Modify your data so maybe there is no data at all. If you have a range of values, try the lowest and the highest. And try values outside of the range. Even try creating malformed data.
  5. Keep track of all the scenarios you identified when working with the edge cases because these make excellent unit test cases. Make use of unit test cases and run them after making a set of changes as soon as your program is back in a running condition. This will help raise your confidence in your changes.
  6. Learn to use your debugger. This is a great tool to use even when you’re not looking for a bug. It can help you become familiar with code that somebody else wrote.
  7. Create a log file and add trace statements. Record information as you enter and leave methods, as you test conditions and branch into various if and else statements. And record your loops and data variables you’re working with. Remember that many times, the clue to helping you fix a bug won’t be what’s written to the log file but could also be what wasn’t written to the log file.

What's on your mind?
On a scale of 0 to 10, how likely are you to refer us to friends?