When you write code for software, if there’s one thing a programmer knows for sure, it’s that at some point it will get stuck. No matter how much experience you have, you will find yourself at a dead end with a programming problem that you can’t solve right away. The question to ask yourself in such situations is: how do you solve the problem? One of the most popular techniques is called Rubber Duck Debugging
Why is this called the rubber duck?
The idea of talking to a plastic duck comes from the book The Pragmatic Programmer: From journeyman to Master by Andrew Hunt and David Thomas, in which a student explains his code line by line to a plastic duck for easier debugging.
Since the publication of this book, it has been very common to see this little plastic animal on programmers’ desktops, so much so that over time it has become an iconic figure in the world of programmers and developers. Using the rubber duck allows you to reproduce the same exercise (without having to disturb one of your colleagues), in addition to offering unlimited attention and listening.
What is Rubber Duck Debugging?
The Rubber duck debugging methodology, also known as “rubber ducking”, is the process of explaining a programming problem to a plastic duck in a meticulous manner and out loud. This exercise highlights inconsistencies and solves the programming problem.
Let’s take the following example. You write an algorithm and your line of code doesn’t work. The piece of code you just wrote has an inconsistency and does not respond in the way you would have imagined. What do you do? You try to find a solution on your own, without much success. Then you get up from your desk, go to your friend-colleague who is also a programmer for the same company and start explaining your situation so that he can help you. You explain what you’re working on and then all of a sudden, it hits you, the solution is obvious!
“You’ve all heard it before: to ask the question is to answer it. This is even truer when we’re trying to explain our code. When someone explains their code to you and then says something like “don’t pay any attention to that, it’s just to make it work in the meantime…” or “this method is supposed to do such and such”, chances are it’s hiding a problem. Another example is when someone tells you: “I’m not sure what this bit does, so I didn’t touch it”, that’s a big red flag. Don’t take it for granted that what you’re told is true – always double-check.
As simple as it may sound, it is very common for programmers to find themselves in this kind of situation: they face an inconsistency in their code, go for help and the solution suddenly appears to them. Simply expressing your problem out loud and talking to a colleague can help you solve the problem situation. This is called the rubber duck method.
Why does the Rubber Duck Debugging method work?
The reason is simple, when you explain your situation to someone else, or in this case to a rubber duck, you are obliged to explain the problem in detail to the person you are talking to. You explain the context, each of the steps and lines of code, and then you finally get to the bottom of your problem. It is by doing this process that the solution appears to you in a logical way. In other words, what you’ve just done is to take a break, step back and you’ve gone through the film in your head.
By doing so, you have a better visualization of the situation. When you explain your problem to a rubber duck, it doesn’t know anything about the code (it’s a duck) and so you have to explain your problem to it, put it in context. And that’s how by explaining your project from the beginning in the smallest details, going step by step, up to the problematic situation, you will find by yourself the answer to your problem. When we work for hours on a project, we tend to get too “involved”, and when a problem or inconsistency arises, all our attention is focused on it. By taking a step back, the solution becomes more obvious.
Do you have a problem that needs help or advice? Our software architecture support service will help you in your projects. It is a kind of online Rubber Duck Debugging service with a team of experts at your disposal.