Ever feel like a quack when trying to debug software? When you're stuck, sometimes it just helps to hear yourself talk -- even if you're just talking to a rubber duck.
Debugging sometimes involves running and rerunning a program, making little tweaks and changes, with no clue as to why it won't do what you want.
Try slowing down and interrogating your thought process. This is the concept behind rubber duck debugging, a simple yet effective way to work through coding problems and bugs.
Learn how rubber duck debugging benefits programmers and when to use it.
What is rubber duck debugging?
Rubber duck debugging, or rubber ducky debugging, is a method that helps software developers resolve bugs by articulating the problem they are trying to solve. In rubber duck debugging, the developer goes through code line by line and explains what the code does to an inanimate object, such as a rubber ducky or teddy bear. The developer might also explain code to someone who knows nothing about coding or even a pet. Any subject with no contextual understanding works -- and so does just muttering under your breath or typing out your thoughts in an email. The point of rubber ducky debugging is to carefully articulate a problem. Having a sort of "blank slate" subject to talk to can help with this.
The rubber duck method forces the developer to engage with every line of code and ensures they take no line for granted.
By forcing themselves to explain the code in natural language to a subject that cannot understand or provide feedback, the developer must slow down and explain in detail the logic of a program. In doing so, the developer will often expose details, assumptions or errors that they had previously overlooked. The rubber duck method forces the developer to engage with every line of code and ensures they take no line for granted. Because the subject has no context, the developer must explain both what the code is supposed to do and what the code on the screen does. In meticulously explaining both the expected function and the actual code, incongruities between the two become clear.
The original concept of rubber duck debugging can be traced back to the 1999 book The Pragmatic Programmer: From Journeyman to Master by Andrew Hunt and David Thomas. If you want to try rubber duck debugging the way the authors intended, follow these steps:
Go find a rubber duck and put it on your desk.
Explain the big picture of what your code is supposed to do, then explain line by line what each part does.
As you explain, you'll notice the line of code that's not doing what you want it to and fix it -- hopefully.
Although it might seem simple, rubber ducky debugging works because of a concept called metacognition. Metacognition is the act of thinking about one's own thought process. Educators use metacognition to improve understanding and help learners spot errors in their reasoning, understand their process and become more effective. By carefully articulating the thought process and identifying the problem, the learner becomes aware of the problem and is quicker to identify it in the future. Eventually, the student -- or programmer -- might be able to recognize and solve those same problems intuitively -- the same way a musician can play a piece or improvise on reflex after considerable time logging slow, deliberate, analytical practice. They might be able to recognize and avoid similar mistakes in the future.
Advantages of rubber duck debugging
Some of the advantages of rubber duck debugging include the following:
Solo activity. Rubber duck debugging shows that sometimes you just need to articulate the thought process to solve the problem. You don't need to involve another person to do this. This avoids wasting their time or letting their feedback potentially distract you from understanding your own thought process.
Don't expose your mistakes. Because it's a solo activity, rubber duck debugging is a good way to fix your code without exposing simple issues with it to co-workers or colleagues.
Gain other code insights besides solution. In addition to helping programmers solve the immediate, practical problem at hand, rubber duck debugging helps programmers glean a better understanding of their overall thought process, and the places where they normally get hung up, enabling them to avoid similar pitfalls in the future.
Disadvantages of rubber duck debugging
You can't solve every problem on your own. Rubber ducky debugging has the following disadvantages:
Can be used as an alternative to seeking real feedback or avoiding criticism. One potential disadvantage of rubber ducky debugging is that you might turn to it in a situation where feedback might be beneficial. If you're looking for quick results on a time crunch, it might be better to ask for help than interrogate your thought process in isolation. Don't use rubber ducky debugging to avoid feedback or constructive criticism.
Doesn't work if intention isn't clear. Rubby ducky debugging is good for finding discrepancies between expectation and reality. If you don't know exactly what you want the problem code to do, that's part of the problem. Rubber ducky debugging helps you find out why code isn't doing what you want it to do by making you articulate both of those things clearly -- what you want and what the code says.
Working on other people's code, you might be just as in the dark as the duck. This follows from the intention problem. When you're debugging someone else's code, part of the problem might be not understanding their intention or rationale for approaching a problem a certain way.
Not good for "big issues." This method keeps you from misrepresenting a small issue as a big issue, but it doesn't help when it's actually a big issue. Rubber duck debugging is best when you already know the answer, you just need to think it over. Some problems you won't have the answer for -- you simply don't know how to solve the problem or get what you need on your own.
AI vs. rubber duck debugging
Generative AI can potentially take on the duck's role in rubber ducky debugging. When faced with a difficult problem, it's extremely easy to turn to a free GenAI service such as ChatGPT and ask, "Why isn't this working?" or "Talk me through this code step by step." This can help the debugger articulate the steps in their logic to find the flaw. The debugger can go through the code in parallel with the AI, verifying or disagreeing with its feedback and find the error. The technology can work as a sounding board, asking tough questions, receiving long, articulate answers that reframe the inquirer's perspective.
However, there's an important distinction -- the AI tool does give feedback. Lots of feedback. It can still solve the problem that the programmer brings to it, but it also might produce volumes of unrelated information, perhaps due to vague or misleading language in the user's prompt. The in-depth responses can distract the user and further obscure or muddy the user's original thought process. It introduces entirely new bugs or mystifying solutions that, while functional, leave the user alone on an island with a complicated black box of a program, with no way to retrace their steps back to civilization -- except once again rolling the dice and praying the large language model (LLM) figures it out.
There's also the concern that LLMs inhibit that process of metacognition. While it's possible to just use the AI as a sounding board, double-checking the machine's generated chain of thought with your own, the temptation to just copy the answers it gives is never more than a click away. Because of this, AI has the potential to reduce the user's capacity for learning in general by offering an attractive escape from crucial effortful practice. One of the most important benefits of rubber duck debugging is that all the answers ultimately come from you, through methodical inspection. It helps you find the answer now and teaches you to quickly come to that answer in the future. It helps you practice critical thinking.
Derek Muller, science communicator and creator of the popular YouTube channel Veritasium, touches on this point in his talk on AI and learning for the Perimeter Institute. He says, "How do we force people to do that painful, effortful work when there's a magic machine that'll do it for you?"
While that's a big concern for educators at the time of this writing, you can still force yourself to close ChatGPT, place a rubber duck on your desk to talk to and work manually on honing your own thought process.
Ben Lutkevich is site editor for Informa TechTarget Software Quality. Previously, he wrote definitions and features for Whatis.com.
Dig Deeper on Software testing tools and techniques