How is it possible that a rubber duck could be the most useful debugging tool you'll ever use?

Despite the silly name—and initially bizarre concept—rubber duck debugging is a popular, well-established programming practice. Even veteran programmers often use it to find problems and issues with their code. When nothing else seems to work, "rubber ducking" has saved many programmers from tearing their hair out in frustration.

The term originates from The Pragmatic Programmer, by Andrew Hunt and David Thomas. You can get the book direct from the authors via their publishing company The Pragmatic Bookshelf.

So what exactly is rubber duck debugging, and why is it so effective as a programming technique?

Rubber Duck Debugging Explained

As the name suggests, rubber duck debugging refers to debugging your code with the aid of a rubber duck. You describe and explain each step of your code, in detail, to the duck, until you notice the problem that's been causing the unexpected behavior.

The first step in rubber duck debugging is to obtain a rubber duck and place it next to your computer. If you're not near a bathtub, you could improvise by drawing a picture of a rubber duck, or finding a similar object.

Next, after exchanging polite greetings with the duck, start to walk through your code line by line. Explain the function and purpose of each line to the duck, and be sure to follow any logical branches or function calls that may take you to other places in the code base. Maintain a balance of eye contact between the duck and the current line of code so that you always know where you are, and use your finger as a pointer to the text that you're discussing.

If all goes well with the debugging, you should eventually notice the point in your code that has been causing issues. Often, just the very act of breaking down the problem into simpler terms, piece-by-piece, is enough to make the issue apparent. It isn’t the duck that fixed the issue — it was your own doing all along.

Why Rubber Duck Debugging is so helpful

When you're writing code, it can be easy to get lost in your own logic and convince yourself that you're doing things correctly. However, just like writers often use editors to get another perspective on their work, programmers need others to look at the code they've written through testing and code reviews.

Rubber duck debugging is intended to cure you of the "tunnel vision" that programmers often fall victim to by forcing you to acknowledge and explain each piece—it's as if you're looking at the code with a fresh set of eyes all over again. This gives you the benefit of having someone else help you examine your code without actually having to ask someone else for assistance. After all, in order to explain something to someone else, you first need to completely understand it yourself.

Why is rubber duck debugging so effective? It makes your brain switch tracks and work in a different way than the usual programming mindset. Rubber duck debugging requires you to slow down, stop taking things for granted, and process the realities of your code line-by-line. You pay more attention to smaller details, like variable names and typos, that could be otherwise easily overlooked.

Another reason why "rubber ducking" has become so popular is because it forces you to make a serious attempt at solving the issue yourself before asking someone else for assistance. Your teammates are often busy debugging problems of their own, so you don't want to approach them unless you're seriously in need of help. Your trusty rubber duck, however, has infinite patience and no other obligations, so it's happy to listen to your tales of programming woe and work through the problem with you.

Of course, if you're really stuck, there's no harm in asking your team for assistance once you've reached the limits of the rubber duck technique. Your rubber duck is always patient and approachable, but its programming knowledge is limited and can't compare to the skillsets of your teammates.

Final thoughts

The benefits of rubber duck debugging aren't just limited to your immediate code issue. Learning how to describe things in simple, clear, and concise language is an incredibly valuable skill that will serve you well in the workplace and in the rest of your life.

When it comes down to it, "rubber ducking" is just another version of talking through your issues by yourself. When forced to take a step back and examine your own behavior from a distance instead of glossing things over, it's surprising what new insights and ideas you can uncover.

Thanks for reading! Be sure to subscribe to our newsletter below to receive more awesome content like this.

Do you have questions about ZenHub? We'd be more than happy to help on Twitter @ZenHubHQ or email us directly.