Let’s talk about rubber duck debugging.
In this post, we’re going to find out:
- what is rubber duck debugging
- how it can help you become a better programmer
- and how to implement this technique and make the best of it
Before we begin, I would like to introduce you to Chipicao, my own rubber duck who has been my programming buddy for more than 6 years.
He doesn’t talk because he has seen a lot of horrors in his life.
Alright, let’s begin!
What is debugging?
First and foremost, let’s see what debugging really means.
Debugging is the process of finding and resolving software defects or bugs, as we normally call them.
It is maybe one of the most difficult skills for beginner programmers to learn because it can really be frustrating and time-consuming.
Everyone got stuck at least once trying to solve a pesky bug we eventually ended up seeking help on StackOverflow, reaching out to a co-worker or a programming buddy.
This is where rubber duck debugging comes in handy.
Rubber duck debugging
Rubber duck debugging is basically explaining your code, line-by-line, to a rubber duck.
The principle behind this technique is similar to finding out a solution in the middle of trying to explain a problem to a person, or when someone catches errors when reading out lout their own code.
Sometimes talking through a problem is all it takes to solve it.
So the rubber duck acts as a substitute to another person and helps you diagnose and fix your own mistakes independently, rather than relying on others to point out the bug.
Next time you want to interrupt your co-worker, first try to explain the code to a rubber duck to save time and energy for both of you.
By the way, you don’t really have to use a rubber duck per se.
Any object will do.
I don’t recommend explaining your code to your pet because it’s not worth stressing them out like this.
How does it work?
What’s the psychology behind rubber duck debugging, how does it work?
There is a big difference between the way most humans think and the way that computers do.
Most of the time, bugs are the result of not thinking the way a computer does.
Computers are precise while humans are generally not being clear and explicit enough in giving instructions.
Maybe you’ve heard about the learning-by-teaching method or the protégé effect.
The protégé effect is the psychological phenomenon where teaching others helps a person learn that information.
For example, in our case, you are teaching the rubber duck the ins and outs of your code and by doing that you are deepening your understanding of your own code.
Just like when you’re programming, explaining the code line-by-line to the rubber duck, you’re going through the same mental process of explicitly telling the computer what to do but this time with a different perspective.
And sometimes a different perspective is all it takes to find that bug in your code.
Improve your debugging skills
Now that we’re familiar with rubber duck debugging and its benefits I would like to give you some guidelines that I’m using on a daily basis that help me in the process of debugging.
Ask yourself the following questions:
- What is your code supposed to do? What should the output be?
Start by telling your duck about the goal or purpose of your code. - What is your code actually doing?
Describe the error you are facing. - Where does the code go wrong?
Describe out loud each line of code and try to trace the problem back to its source. - What have you done so far to try to fix the issue?
Describe any approaches you have tried and how they didn’t work.
Lastly, if you really end up in a situation where you really cannot find a solution to your problem, then it’s time to ask a co-worker for a pair programming session.
The more eyes the better.
Summary and conclusions
- Debugging can be hard and frustrating
- Explaining your code, line-by-line, to someone or to an inanimate object will help you find that bug
- Teaching as a way of learning is an effective way to consolidate your understanding of a certain subject
- Don’t stress your pets with programming stuff
Let me know what you think about this article in the comments section below.
If you find this article helpful, please share it with others and subscribe to the blog to support me, and receive a bi-monthly-ish e-mail notification on my latest articles.