It amazes me how rare the skill of debugging software is. It is even more amazing to me that after programming for over 27 years now, I still have trouble with this. So, as a checklist for myself, and as a possible help to others along the way, I offer you “How to Debug Your Code”
Is It Plugged In? Is It Turned On?
Another way of stating this is, “Validate your assumptions.” I learned this phrase while I was traveling with a high school music group as a sound tech. I was one of two guys who was responsible for making sure the sound equipment got from one site to the other and got setup at each site and was ready to go in time for the group to practice at least once before the performed.
The thing with sound equipment is, there are lots of connections. Lots of places to miss. There were many times that we would set everything up and test it all out and there would be no sound, or partial sound. I’m ashamed to tell you how many hours we wasted tracking the problem down only to find that something wasn’t connected. An amp wasn’t turned on. A plug had come out of the wall. Cables had become disconnected.
And the same sort of thing happens with our code. You deploy code to the wrong directory and wonder why it isn’t showing up in the browser. You deploy code to the right directory, but you forgot to get it from version control before you did.
Read the Error Message
This is especially true with .NET but even when all you have is an error code, the error message is useful.
In .NET, when you get an error message, it all but tells you what line of code you need to fix. And even if you don’t understand the error. You can copy and paste the error into a search engine and you are likely to come up with multiple possible solutions. Even if all you have is an error code.
Sometimes the Error Message Is Hidden
Run the Debugger
Once I’ve eliminated the obvious, the next thing is to pull out the debugger and set break points. Now, this can be tedious. How many times have you decided to step over when you should have stepped in? Arrrgh. Now I have to start over again.
Well, here’s how I speed things up.
At the top level function. I initially step over until I find out what line is causing the problem. If you have an exception that is being thrown, this is easy to do because you just have to wait for the exception. If you are getting a wrong answer, it may be a bit more difficult because you’ll have to verify your expected results along the way. But the point is, you need to find out what line is throwing things off or you will be there all day, stepping through code one line at a time.
Once you know which line in that function is causing trouble, you step into that line on the next run and step over each line in the lower level function until you find out what line is causing a problem there.
Rinse, lather, repeat, until you find the problem.
Step In To Libraries
This goes back to validating your assumptions, but I call it out here separately because we never think that we’ve made an assumption about something that is supposed to be a “black box” when in fact, we have.
Back in the day when I was doing a lot of Visual C++ coding, I learned a lot about how MFC worked by stepping into the MFC code. So much so that I became the expert in my office regarding fixing C++/MFC related bugs.
The Search Engines Are Your Friends
This one should be obvious. But, unfortunately it isn’t.
The hardest part about searching is trying to figure out what other people may have used to describe the problem so that you can find the answer that is just sitting out there waiting for you. I’m convinced that as intelligent as the search engines have become, using them is still a kind of art.
Explain the Problem to a Co-Worker
You would be amazed at how often the act of describing a problem to someone else brings the obvious solution to light. If you’ve ever done this, you know that often, in the middle of describing the problem, you’ll says, “… oh, never mind. I know what the problem is now.”
Which leads to my alternative solutions…
Explain the Problem to Your Coffee Cup
Why? Because it isn’t your co-worker who helped you solve the problem when you explained it to him. It was the act of explaining it. Once you’ve articulated the problem well enough to explain it to someone else, that act actually often hands you the solution.
Step Away For a While
I don’t know why it is, but for some reason, we seem to think that if we aren’t coding, we aren’t working. But you can’t work non-stop as a programmer for 4 – 8 hours without taking a break. And banging your head against a bug when you’ve already done that for 15 minutes probably isn’t going to produce any better results if you do it for another 4 hours.
Yeah, you may try a bunch of stuff. You may look busy. But the fact of the matter is, you might be better off finding something else to do for an hour or so and then coming back to the problem once you’re brain has had a rest from it.
Well, those are my tips for now. What tips do you have? Leave them in the comments.
Other Places Talking About Debugging Code
Other post in Programming Best Practice
- Treat Warnings As Errors - May 15th, 2014
- TDD Saves Time – A Story - May 22nd, 2014
- DRY Programming - May 29th, 2014
- I, J, and K Should Die - June 5th, 2014
- Avoiding Code Complexity - June 19th, 2014
- Defining “Done” - July 10th, 2014
- YAGNI - You Aren’t Going To Need It - August 14th, 2014
- Technical Debt Is Inevitable - October 16th, 2014
- Raking Leaves and Writing Code - November 20th, 2014
- Magic Strings and Magic Numbers - December 18th, 2014
- Limiting Beliefs of Programmers - April 9th, 2015
- How to Become a Better Programmer - April 16th, 2015
- Code Comments & Agile Programming - May 14th, 2015
- Debugging Software - June 25th, 2015
- 15 Ways To Write Beautiful Code [That Have Nothing To Do With Testing] - August 6th, 2015
- 5 Reasons Learning Terminology Increases Your Effectiveness [As A Programmer] - August 27th, 2015
- Values, Beliefs, Green Civics and Programming - December 10th, 2015
- How Not to Choose a Framework - June 2nd, 2016
- How to Sabotage Estimates - August 23rd, 2016
- The Myth of Sloppy Code - October 18th, 2016