The Psychology Behind Rubber Duck Debugging
It’s not crazy. I can explain.
As a programmer, have you experienced having a roadblock while solving a specific problem in your program? It is not insane and unusual to talk to yourself while fixing the code in front of you. After talking for a while — or maybe for hours — finally, you found the simple block of code that is causing the problem.
A lot of times, I’ve experienced some programmers that will ask my help about a specific bug they are fixing. I will then ask them how their application and their code works. I literally have no idea how to fix a program that is not mine and have no idea about the flow. However, I let them explain the flow of the process and the connection between functions and files. Oftentimes, they think of a solution before I even understand what is happening. Many people have been so thankful for me — for doing literally, nothing.
As for my own experience, I can understand some topics or ideas when I am explaining it. The more I explain it to others, the more I understand the content and flow of the logic.
That, my friend, is the reason why the rubber duck debugging works. It is a methodology wherein a programmer will talk to a rubber duck and explain to it the problem of their program. They will explain it to them as thoroughly as possible — and soon enough, experience the miracle of realizing which is the bug and why on earth it isn’t working for a long time.
Here’s a crazy and short process on how to do it:
Using a rubber duck as a debugger
- Get a rubber duck or any inanimate figure. It’s up to you if you want to give them a name.
- Explain the main problem. Where does it stop working? What is the output supposedly, and what is the undesired output?
- Explain your code line-by-line. Give the step by step of the flow of the program. Be as detailed as possible. Make it as if you are teaching the rubber duck the program itself.
- Notice that while explaining, you fully understand where the program is going. Along the way, you will find the bug or the incorrect process you are doing. Or on a larger scale, find the real blocker of the process.
What kind of sorcery is this
The process actually works without a rubber duck. We can do it with other inanimate objects. Or someone who just keeps asking us: “why?”
The real magic doesn’t happen on rubber duck itself (sorry, Duck Norris). However, it happens in our minds. It uses the same psychological principle wherein we are encouraged to explain to ourselves why we did such actions and have a self-realization about what we’ve done.
It is usually used by most psychologists to fully understand a person, at the same time, for the person to understand himself/herself fully. Hence, they keep on asking questions, and they wanted you to narrate to them as they listen to your explanation and observe you if you are having self-realization by answering or narrating your story.
Some examples are from the teachers. The reason they keep on asking questions is for the students to explain and defend their answers (no, teachers are doing that to teach and not to torture you. Or — for some — maybe both, I am not sure. hahaha). Students will not learn when the teachers keep on spoon-feeding them the answer. So he/she must guide the students to a self-realization about the mistake they are doing. For them to be independent and learn to assess themselves and find their own mistake and correct it.
How does it work
“Learners who engage in explanation go beyond the obvious to look for broad and simple patterns underlying what they’re trying to explain, and in so doing, they often make useful discoveries about shared properties or causal structure. That’s why explaining is so important for cognitive development, for education, and for scientific advance.”
- Tania Lombrozo, Ph.D. (Explaining to yourself can be a powerful mechanism for learning)
Another explanation of this type of learning is when we know something — let’s say, a belief — but then suddenly someone asks us, “why?”. It would appear that we don’t have an explanation in stored, so we nervously explain it, and we find our defense not convincing. It turns out, we can’t explain it.
However, it would give us a jump-start to analyze this certain belief and will have a fix explanation about it. In the end, we will learn more about it and find the flaw in it by which we can correct, or on a drastic note, we throw that belief out.
In application to programming, explaining our process breaks our ego — thinking we’re doing it right. When facing a bug, we often say, “But I’ve done everything I needed to do!”. Trying to explain it to ourselves raises the question of this prideful claim with a “Did I do everything I needed to do?”.
Our explanation needs a full-circle. In our minds, our idea makes perfect sense. The idea makes a reset when we speak out those ideas, from our minds to our mouths. Then to our ear and lastly to our thoughts to asses what we’ve heard. In our thought assessment, we will process this input from the outside and will be fair in judgment if it is correct or not.
While talking to a rubber duck, our mind is experiencing a subtle shift.
First, it is slowing down our thinking. It breaks down every piece of information in our mind instead of a more general idea. By breaking down ideas, our mind is open to a new pattern based on these pieces. Hence, while explaining, we’ll see our work from a different perspective.
Second, it develops our reasoning. The rubber duck doesn’t know anything, and yet we are trying to explain the complexity of our code.
During these shifts of perspective, our brain develops a different path to follow, which makes the flaws in the code or the process more visible.
Can I do it with my pet?
Well, you can. But I think it would be best with an inanimate object. Dogs and Cats need our full attention and can be distracted by many things. And we might get distracted and end up playing with them instead of working. With the rubber duck or any inanimate object, however, it gives us the feeling of a silent student who is ready to learn and will listen to whatever we are teaching him. It gives us the need to explain everything to someone who doesn’t know everything literally.
How crazy will it look?
Annoyingly crazy, I admit. I mean, who wants a workmate that keeps on talking to an inanimate object all the time. It’s scary and unusual — not to mention some of us want to work quietly.
The idea is great, but at least tone it down a little base on the office environment.
Normal:
Whisper your process to yourself, enough sound to hear your voice but not too much to be heard by others.
Not normal:
Hearing your workmate arguing with a rubber duck.
Normal:
Having a piece of an object, a rubber duck is enough for this method.
Not normal:
Having a shrine for his/her rubber duck with lots of its minions conquering the desk and praying for salvation.
Okay, I admit that the last one is a bit overboard, but still, you get the point.
Conclusion
We are free to come up with any methodology that can help us cope with the hardship of our every day job. May it be as simple as following a Youtube video of HowTos — up to a crazy idea like the rubber duck debugging — everything will always come down to one crucial part of our career: Learning.
As for my suggestion, instead of a rubber ducky, we try to collaborate with our co-workers about our code. Especially to those who are new to the field. They will learn from us, and probably we will learn how to explain things to them — and unlike the rubber duck, they will learn a thing or two about our bugs and our mistakes.
Rubber duck debugging method may also help us improve our teaching skill which is a benefit to the team and to the new members that still need to learn more about the process of the company.
Sometimes in life, we will stumble upon a weird way to adapt and to learn. The programming career is no exception. What we just need is to be open to a new idea because this is the key to innovation.
Resources
https://www.livescience.com/34000-explaining-helps-understand.html
https://gitduck.com/blog/improve-how-to-code-with-rubber-duck-debugging/
https://www.thoughtfulcode.com/rubber-duck-debugging-psychology/
https://www.apa.org/science/about/psa/2016/03/explaining-yourself