When you’re stuck on a coding problem, it can make you feel crazy. It’s easy to want to rip out your hair or the screen or whatever’s nearby. But instead, maybe your first thought should be to grab a rubber duck.
Yes, the kind that little kids have in the bath. How can a rubber duck help with your code? Read on to find out why rubber ducking is a process that software engineers worldwide use.
What is rubber duck debugging?
Rubber duck debugging is a method of finding that bug preventing your software from running. The Pragmatic Programmer by Andrew Hunt and David Thomas tells a story about how a programmer would bring a rubber duck around with them and debug their code by making themselves explain it, line-by-line, to that duck.
These days, the phrase refers to verbally walking through your coding problems with a non-judgmental duck. It’s when coders talk their code aloud to a rubber duck to uncover solutions to errors they previously didn’t consider.
Many software engineers have faced the issue of conveying a problem to someone else, maybe someone who has zero programming experience, then unexpectedly finding the solution through the process. Explaining the situation allows them their Eureka moment.
“We use ducks and other stand-ins because of the vast difference between the way most humans think and the way that computers do. When you hit a roadblock when programming, it’s likely that you’re not thinking the way a computer does. Computers are painfully precise. Humans are, generally, forgiving and loose in their understanding. So many of the ‘bugs’ and issues you have when solving a problem in your program originate in not being clear and explicit enough in your instructions.”
Why is rubber duck debugging important?
There’s specific psychology around the process. Walking through a process out loud slows down your thinking. While our brains can take in as many as 350 and 500 unspoken words per minute, like when we’re reading, we can only say about 120–180 words per minute when we’re speaking. Simply taking the time to say something out loud automatically makes us process the information differently.
The method also means that instead of skim-reading bits of code, you have to analyse every piece of information until you stumble across the bug. This careful analysis has to happen because your duck needs to hear every line and detail, making it easier to find the error.
Lastly, computers process information differently and require every bit of logic spelled out to them. So does a rubber duck. This means you’re forced to think more like a computer during your explanation, and more likely to stumble upon the solution.
How to go about rubber duck debugging
You might have hit a wall when it comes to your code — there’s a bug, but you can’t figure out what it is, and you need to work out why but feel stuck. Time to grab your duck (although it’s important to remember that rubber duck debugging doesn’t actually require a duck. You can use anything that makes you slow down and thoughtfully explain your code — a stuffed toy, an actual dog, even a plant if you wish).
- Find your rubber duck.
- Start explaining your code and its goals, setting the context for the duck.
- This is where you move into details — don’t skip them. Start going through line-by-line, the flow of the whole function or method that isn’t working.
- Then move onto all the intermediate states and transitions.
- You’ll stumble upon the solution! Whether it was a typo, the condition, or something else, you’re bound to find it by walking through the process.
What are the benefits of rubber duck debugging?
When you’re coding, it’s easy to get lost in the details and lose sight of the bigger picture or the end goal. But when you talk through the code with a duck, you make the goal visible again. You’re forced to look at the code from an entirely new angle.
Importantly, the process allows you to solve the problem yourself and you don’t have to take up others’ time by asking them to help. It also means that you learn from the mistake and build your own confidence and independence as a developer.
When you talk it through with someone else, you might feel judged for not being able to fix the issue. With a rubber duck, you can be as open as you want because they’re an inanimate object, incapable of judgment.
What’s great is that you can use the technique beyond just coding. It’s useful for technical communication in general, but even if you’re putting together a presentation, it can be helpful to walk something like a rubber duck through the process.
Hopefully, this has helped you see how rubber duck debugging works and why it is such a popular method used by developers everywhere. Now when you’re stuck, you can work through coding errors by grabbing your duck and starting up a chat!