Rubber ducking: not just a funny phrase
When you have a coding problem, is your first thought to grab a rubber duck? No? Maybe it should be.
Known as rubber ducking, your bath time buddy might just be able to help you find that pesky bug that’s stopping your software from running. But how can a rubber duck help you with your code? They aren’t exactly known for their technical prowess.
Here’s why rubber ducking is so much more than a funny phrase.
What is rubber ducking?
Rubber ducking is the shortened term for ‘rubber duck debugging’. It’s a method of debugging code that involves the classic bathroom toy: a rubber ducky.
The practice of rubber ducking comes from a story in the book The Pragmatic Programmer by Andrew Hunt and David Thomas. In the story, a programmer would debug code by explaining it, line by line, to a yellow plastic waterfowl.
Have you ever had a eureka moment while explaining something to someone? That’s what rubber ducking seeks to replicate. Essentially, rubber ducking is about talking through your coding problems with a friendly duck that won’t judge you.
What are the steps?
You hit a hurdle with your code. It isn’t working, but you can’t work out why. Instead of pestering a co-worker or accepting defeat, you grab your trusty duck-shaped buddy.
Step one: Acquire a rubber duck. Name it if you wish.
Note, other bath toys, inanimate objects or even pets (that will sit still while you talk) will do. We prefer to stick to origins and use the traditional rubber ducky.
Step two: Explain your broken code, line by line, out loud to the rubber duck.
A good way to go about this is to first explain your code and its goals in general terms. Then, go into detail, explaining the code you’ve written line by line. Don’t skip any details. If you’re still not seeing the error, start explaining the intermediate states and transitions.
Step three: Have your eureka moment.
While explaining, you’ll likely find the bits of code that don’t do what you thought they did or that need fixing.
That’s all there is to it.
How does it work?
There are a few reasons that rubber duck debugging is effective.
First, explaining something out loud makes your thinking slow down. Our brains can process between as much as 350 and 500 unspoken words per minute — for example, when reading. But we can only tend to articulate around 120-180 words per minute. When we explain something out loud, then, it forces us to slow down and spend more time considering what we’re saying.
Second, it forces you to methodically analyse every bit of code until you find the bug. Instead of skim-reading over bits of code, your duck friend wants to hear every line and every detail. You’re going over your code with a fine-tooth comb — and so you’ll catch the bugs.
Third, computers ‘think’ very differently to humans. Computers need everything spelt out for them, right down to the last detail. Now, so does your rubber duck. This means that when you explain to your duck, you’re forced to think like a computer — and you’ll stumble when you hit a bug.
The benefits of rubber ducking
Why would you choose rubber ducking over other debugging methods?
There are many merits to opting for rubber ducking. For a start, you gain a better understanding of your code and your goals. When you code, it’s easy to lose sight of your end goal. When you talk through your code with a duck, though, you bring the goal of the code back into focus.
Plus, rubber ducking lets you solve the problem yourself. As a result, you gain the opportunity to learn from your mistake. And, you’ll feel more confident in your skills as a coder (after all, you were able to solve the issue) which can reduce impostor syndrome.
Not to mention, rubber ducking adds a dash of colour and whimsy to your desk.
The merits of the duck
All things considered, talking to a rubber duck still seems a bit strange. But there are some benefits to using a rubber duck rather than talking to a colleague.
- You don’t disrupt or distract your colleagues.
Bugs are a common occurrence in coding. If everyone distracted their colleagues while debugging, no one would get anything else done.
- You don’t have to worry about being judged — the rubber ducky would never think less of you.
Another issue with talking to a colleague is that you can feel judged for not being able to solve this problem yourself. It’s easier to be more open to discussing your mistakes with a duck friend that would never think less of you.
Not just a bath toy
They might not have the latest coding know-how, but rubber ducks can help you debug your code. Next time you run into a problem with your code, why not grab a plastic duck, and see how rubber ducking works for you.
Because it turns out, rubber ducks can be more than a bath toy, and rubber ducking is more than just a funny phrase.