We all have been there. The frustration that we feel when your code comes out as broken, or the output you want is far from what you’ve got is completely normal. So what do you do when you have a bug in your code? One might run through the code several times to pinpoint any obvious errors only to find none.
Sometimes, the errors are minor details that you may have skipped due to your programming speed. What we know about software is that a developer needs to be as exact as they can while giving out commands and setting conditions. Since the computer thinks only in crystal-clear lines of instruction, we need to work on getting better at speaking its language.
That’s why the rubber duck debugging method is so popular. We’ve all heard of the term, but it is often met with raised eyebrows and confused expressions. It is a lesson in psychology for developers and highly effective methodology to debug your code.
How does a rubber duck fit into coding for programmers? Well, you’re about to find out! Once and for all, we are here to clear the air and explain exactly what rubber duck programming means.
Let’s dive into the details without beating around the bush!
What is Rubber Duck Debugging?
In the world of software engineering, a rubber duck method helps to debug codes. It helps you to find bugs and problems in your code. The term rubber duck refers to an entity that has less than half a brain and understands almost nothing about a given problem.
Rubber Duck Debugging is a programming methodology where you explain the task you are performing from the goals you are trying to achieve to all the details in your code, line-by-line. By describing the problem the programmer force themselves to express their ideas in a clear way and go through all the details. This explanation is called “telling the duck your problem”.
When a coder does so, the bug will be easily be spotted as you will be forced to face the logic problems you might have ignored previously. All with the help of rubber duck programming.
How Rubber Duck For Debugging Works?
The computer thinks differently than humans. Where one uses only logic and facts, the human mind is obviously much more complex and meant to use all five senses and then to come into decisions (let’s not talk about intuition).
When we come to being blocked due to a bug, the most common reason is that we are not using our minds the way the computer thinks to solve the coding roadblocks.
To write the perfect code, one needs to be as specific as you can get since computers don’t understand vague instructions. Hence, the majority of the bugs a programmer faces in his code are caused due to the lack of providing explicit information and instruction to the software.
That’s why this can be solved with the help of a rubber duck for debugging. Any inanimate object works, but we will stick to the rubber duck methodology for convenience. The process begins when you try and talk to your duck regarding your bug problem in order to decode it. Here’s what to do:
- Mention your flawed code, its final output and what you’re trying to accomplish. Keep the details general as you’re just setting up a context for now.
- Explain the flow of your broken code to the duck line-by-line while not missing out on a single step. The more detailed your explanation is, the easier it will be to find the bug.
- Explain all the transitions and intermediate details too. The duck is a curious type.
- Can you see the solution to your problem? The best aspect of rubber duck programming is that coders often find the bug as they explain the steps and flow to the duck. A typo, the wrong variable or syntax might be highlighted due to your fruitful one-sided conversation.
The Psychology Behind Rubber Duck Method
Don’t think of this as a silly concept that people came up with. There is actually a strong foundation on why the method works so well.
It is observed that while explaining a concept or a problem to someone, there are two subtle but ground-breaking shifts in your brain. Let’s go by the example of describing a bug in your code for now.
The first shift slows down your thinking. You’re able to describe the details more precisely without missing out on any information in comparison to all your neurons firing up at great speed while you’re typing out the code. The second shift blends into the first one by the assumption that you’re talking to a rubber duck that’s a half-wit and understands nothing about the concept of coding bug.
By assuming that the duck doesn’t know details of the problems as clearly as you do, you’re forced to churn out the nitty-gritty bits of the code and may find a solution in the same shift. Often it is a silly mistake in the code that was owed to speed and negligence. This psychology is a part of Piaget’s theory of cognitive development about how a child reacts when he meets people who might have a different understanding than he does.
All humans develop this skill and don’t even understand when they apply it in real life situations. Think of the last conversation you had with your mom or best friend explaining relationship problems to find a solution if you want to make it relatable!
Rubber Duck Explained: Teach The Duck About Coding
It is said that teaching helps reinforce the knowledge one already has. This is because a teacher begins from scratch with the assumption that the people they are addressing have no clue about the subject in discussion. By running through the basics and then building up the concepts, the teacher’s understanding of the subject deepens as well.
As developers, when you assume that the rubber duck knows nothing about coding or the problem at hand, you’re forcing yourself to dumb down and come to ground zero.
By starting from the bottom while explaining every line of the code to the rubber duck, you’re hyper-focused on all the information you’re providing to help the other party understand. During this process, you are open to catching a flaw or two that you missed out on as you were busy with the big picture of the code.
When a developer finds a broken code, their brain probably goes into an overdrive trying to find the mistake. But explaining to a harmless rubber duck helps the programmer to step out of themselves and their emotions to look at the broken code in a different light.
When the perspective shifts, you’re more likely to catch something you ignored or bypassed before. The change leads you to process the codebase as the computer does, in black and white with no room for human error, which makes it all the more easier to catch and fix the bug.
How To Go About Rubber Duck Programming?
Now that you know what rubber duck programming and the psychology behind it is, we’ll help you out with how to go about it. You’re allowed to giggle, but this technique is used by millions and is 100% effective. As long as it gets your code to work and give you the desired output, you can give it a try. Who knows; maybe you’ll love it and create a habit out of it!
- Get a rubber duck. It can be a physical one or a digital one like GitDuck.
- Now start explaining to the duck your code. Start from your goals, your strategy to accomplish it.
- Now explain your code in detail without skipping anything. Go through all the lines of your code and speak as clear as possible.
- Record yourself rubber ducking, so you can also share it with your team members if you want them to understand your code as well.
- By going through this process, you will reach the point where the error in the code will be identified and you will be able to fix it.
Over to You
Now it’s your turn. We created GitDuck to be the tool for doing rubber duck debugging so you record it and help your team understand your code as much as you do. Create your free account at GitDuck.com and start explaining your code!
🦆