What is a bug and what is debugging?
A bug is an error, flaw or fault in a computer system or program that causes it to generate an incorrect or unexpected result, or to behave in unintended ways. Debugging is the process of finding, identifying, and fixing the bugs. It is commonly recognized as one of the most difficult skills for beginner programmers to learn. Enter, the rubber duck.
What is rubber duck debugging?
The idea of using a rubber duck to debug a program comes from The Pragmatic Programmer (Hunt & Adams, 1999). By explaining your problem with a program to another person, you may be able to find the cause. The other person only needs to listen to your explanation, all the while nodding their head up and down– much like the bobbing of a rubber duck in a tub. This person’s (or the rubber duck’s) purpose is to be someone to whom you may explain the situation, thus allowing you to discover the problem’s solution on your own.
Since it might be hard to persuade or bribe a friend or classmate to behave like a rubber duck, you should use an actual rubber duck or an object that can represent a rubber duck.
What are the steps in rubber duck debugging?
Just talk to the duck. Explain the following to it:
- What is your program supposed to do? Does it take any input? What is the expected output?
- What is your program doing? How is that different from what you want it to do? If there are many differences, start with the first one that you see.
- Describe the purpose of each variable and each line of code. Pretend that the duck knows nothing about programming.
- Where is the program first going wrong? Which line do you suspect is the start of the incorrect behavior? Why do you think so?
- What have you tried so far to fix your program? What changes did you make?
- What is the next approach you have in mind? Why do you think this might work?
Does rubber duck debugging actually work? Why?
Explaining a problem out aloud causes the mind to slow down. Our mind can absorb or process between 350 and 500 unspoken words per minute. However, we can only speak at a pace of 120-180 words per minute. This slowing down in our articulation makes us spend more time examining what we are doing in the code.
Secondly, as the rubber duck knows nothing about your code, you have to describe every line in detail without skimming over any parts. This forces you to pay attention to code segments that you assumed were working fine and were not causing the problem.
Apply the method
Rubber duck debugging provides a comprehensive yet customizable set of guidelines that novice programmers can use to debug code. Students learn to evaluate their own interpretation of the problem, to compare the fixes they have tried, and to arrive at new solutions. Rather than depending on others to figure out where they went wrong, they learn to identify and correct their own errors.
So, the next time your code is not working, find or fabricate a rubber duck, make eye contact with it, and start explaining the program. Sooner or later, you will hear yourself telling the duck what is wrong or missing in your program. Feel free to thank the duck.