Rubber Duck Debugging (or simply Rubber Ducking) is one of the most crazy sounding debugging techniques available to programmers.
The premise is simple, when you get stuck with a problem with your code that the usual debugging techniques have failed to solve, then simply grab an inanimate object and explain to it slowly and step by step what your code does.
How does it work?
There are a few reasons why Rubber Ducking works.
Firstly, when you’re explaining your code to your duck, your are forcing your brain to think about the code much more slowly than when you are simply skimming over the code and thinking about it in your head. This in turn allows your brain more time to consider exactly what is happening in the code.
Secondly, explaining to your Duck switches your focus from an observer of the code, to that of a Teacher. This change of mindset causes you to evaluate your code from a different perspective, which can in turn help with finding and fixing bugs.
It might sound silly (and you’ll probably feel quite silly when you first start talking to a rubber duck), but you would be surprised how well this technique actually works. Especially for logic problems which aren’t easy to spot when simply checking variables in the debug console.
Why a rubber duck?
The name Rubber Duck Debugging comes from a story in the book “The Pragmatic Programmer” by David Thomas and Andrew Hunt, where a programmer would carry around a rubber duck and explain his code to it.
You can of course use anything you like. If you want to explain your code to your mug, then feel free. If you have a favourite cuddly toy then they would be just as happy to listen to you.
Pets can also work, although it’s a little frustrating when they walk off halfway through your explanation.
I’ve even successfully rubber ducked with my girlfriend (who has absolutely no idea about code or computers) whilst taking a relaxing evening walk away from the house and PCs.
I hope you have found this post interesting, and that it gives you another tool to use the next time you’re sat in front of the PC tearing your hair out because you cannot get the code to work.