Join thousands of grownups who get articles on teaching kids to love coding.
Back to Articles

How to debug your code like a pro

How to debug your code like a pro
When I joined Bitsbox, my boss offered me the opportunity to make up my own job title for my business cards. I chose Senior Software Craftsman.

Software Craftsman reflects the careful, methodical approach that I take to writing software. But no matter how careful I am, I rarely write a program that has no bugs (that is, errors in the code) the first time I run it.

The process of finding and fixing bugs is called debugging, and it's one of the most important skills you can develop to be a good coder. Debugging can be frustrating, but it's also a great opportunity to learn something new.

When I debug my code, the Single-Character Rule has saved my bacon more times than I can count. Put simply, the rule goes like this:

The longer you spend struggling to fix a bug in your code, the greater the chance that a single character is causing the bug.

That is, you either need a character you don't have, or have a character you don't need.

Of course, there's no guarantee that the bug is that simple. But when something obvious is causing the bug, the bug is often easier to spot than a missing or extra single character. The longer you struggle to find the bug, the greater the likelihood that the problem is subtle.

Every character matters

Another way of looking at debugging has to do with accuracy and attention to detail. Remember, computers can't read your mind. They don't do what you want them to do; they do only what you tell them to do. Every. Character. Matters.

A single missing curly brace can turn an app from a delightful game into a giant stack of error messages faster than a horde of ten year olds can demolish a pizza buffet.

Here's an example to help explain what I'm talking about. In this eight-line app, the tap function updates the variable men. Every time you tap the screen, a new man appears, and a counter increases by 1 and is displayed on-screen. But when you run this code, the counter instead says "[Object object]". Huh?

Look closely at the code and try to spot the error. Take a few minutes. I'll go grab a soda, and meet you below the code.

men = 0
count = text(men,500,50)

function tap() {
  man = stamp('bizguy',x,y)
  men = men + 1
  count.change(man)
}

The problem is on line 7, where it says count.change(man). The variable man should instead be men (which is the counter). Changing one character can make all the difference in the way your program works. Phew, right? That's a subtle bug. I can't tell you the number of times I've wasted entire afternoons trying to find an error just like it. (Although in my defense, I'm usually digging through thousands of lines of code, not eight.)

I can hear some of you protesting that I chose too-similar variable names, and was just asking for trouble. I totally agree. In fact, I'll have another blog post coming soon all about strategies for naming variables and functions to make code reliable and easy to understand.

How to find single-character bugs

Hopefully by now I've demonstrated how small and subtle bugs can be. But how do you find these? My approach is pretty simple:

  1. When you've been working on the same problem for a while, and you're feeling really frustrated, take a break. Recognize that you're feeling stuck.
  2. Stand up, walk around the room, get a drink of water, go do some somersaults — whatever it takes to clear your mind for a moment.
  3. When you come back, look closely at the code. Go line-by-line, and character by character. This approach can feel tedious, but it's usually the best way to find this kind of problem.

Usually, after I remember the Single-Character Rule, I can find the problem in a matter of minutes. Failing that, getting a second opinion can work wonders. Someone else can look at your code with fresh eyes and may spot a bug you've been skipping.

Programming is rewarding, but it also takes focus and persistence to get really good at it.

If you get really stuck, you can also contact us at help@bitsbox.com. We're always happy to help debug.

Jeff Bull
About the author: Jeff is a lifelong coder, meticulous woodworker, and the Senior Software Craftsman at Bitsbox.
Join thousands of grownups who get articles on teaching kids to love coding.
Cute icons Stamp not found