Debugging

A Practical Guide to Rubber Duck Debugging

Learn how rubber duck debugging works, why explaining code out loud exposes bugs, and how to run a useful session without a duck or a colleague.

On this page
  1. Why Talking to a Duck Finds Bugs
  2. How to Run a Session
  3. A Short Example
  4. When You Have No Duck
  5. Habits That Make It Work Better
  6. What the Technique Cannot Do
  7. Frequently Asked Questions

Rubber duck debugging sounds like a joke, and in a way it is. The idea is that you explain your code, line by line, to an inanimate object, traditionally a small rubber duck sitting on your desk. Surprisingly often, the bug reveals itself before you finish the explanation.

The technique has been around for decades, and it persists because it works. This guide covers why it works, how to run a proper session, and what to do when talking to a duck is not an option.

Why Talking to a Duck Finds Bugs

When you read your own code silently, your brain fills in what it expects to see. You wrote the code, so you know what it is supposed to do, and your attention slides over the places where it does something different. Silent reading lets you skim and assume.

Explaining forces you to slow down and be precise. Spoken language needs complete sentences, so you cannot wave your hand at a confusing section. You have to say what each line does, what each variable holds, and why a condition is there. The moment your explanation and the actual behavior disagree, you notice the mismatch.

There is also a benefit in changing the role you play. As the explainer, you are no longer the author defending the code. You become a teacher, and teachers tend to catch their own gaps in understanding.

How to Run a Session

You do not need a special setup, but a bit of structure makes the technique far more effective.

  1. State the goal. Say what the code is meant to do in one or two sentences. "This function should return the total of all items in the cart."
  2. State the symptom. Describe what actually happens. "It returns zero when the cart has more than one item."
  3. Walk through the code in order. Start at the entry point and explain each line as if the listener has never seen the language. Name the values of the variables at each step.
  4. Stop at every assumption. Whenever you say "this should be," pause. That phrase marks a belief you have not verified.
  5. Check the claim. Print the value, run a quick test, or look at the documentation to confirm it.

The fifth step is where most bugs surface. The duck does not find the problem for you. It makes you slow down enough to find it yourself.

A Short Example

Suppose you have this function and it returns the wrong average.

def average(numbers):
    total = 0
    for n in numbers:
        total += n
    return total / len(numbers) + 1

Explaining it aloud, you might say: "I start the total at zero, add each number, then divide by the count of numbers..." and then you reach the end of the line: "...and then add one." Why would you add one to an average? You would not. A stray edit from an earlier experiment is the culprit. You might have scanned past that + 1 ten times in silence.

When You Have No Duck

The duck is a stand-in for any patient listener. Several alternatives work just as well.

  • A colleague, with a rule. If you ask a real person, explain the problem completely before they respond. Often you will solve it mid-sentence and never need their answer.
  • Written explanation. Write the question as if you were posting it on a help forum: what you expect, what happens, what you tried. Many developers solve the problem while writing and never post the question.
  • A voice memo or an empty room. Speaking aloud matters more than who is listening.
  • A comment block. Write a comment above the troublesome section describing, in plain words, what it should do. Then compare it with the code beneath.

Whichever you choose, the key ingredient is full, ordered, literal explanation.

Habits That Make It Work Better

Explain the code you have, not the code you meant to write. Read what is actually on screen. It is easy to describe your intention instead of the text.

Use real values. Instead of saying "the list," say "the list containing 4, 7, and 9." Concrete numbers expose off-by-one errors and wrong branches quickly.

Do not skip the boring parts. Bugs hide in the code you consider too simple to check: variable names, imports, and configuration values.

Take breaks when stuck. If a full explanation produces nothing, step away. A short walk often resets the assumptions you were stuck on.

What the Technique Cannot Do

Rubber duck debugging helps with logic errors and wrong assumptions. It is less useful for problems that depend on knowledge you do not have, such as an unfamiliar library behaving in a surprising way, or a bug that only appears under particular timing or load. In those cases, explanation will tell you exactly where your understanding runs out, which is still valuable, because it tells you what to look up or measure next.

It also works best before you have spent hours going in circles. If you have been stuck for twenty or thirty minutes, begin the explanation rather than trying one more random change.

Frequently Asked Questions

Does it have to be a rubber duck?

No. Any object, pet, or even an empty chair will do. The value comes from explaining the problem step by step in plain language, not from the listener.

Is it better to talk out loud or write it down?

Both work. Speaking tends to slow you down more, while writing leaves a record you can share later. Use whichever you will actually do consistently.

What if explaining the code does not reveal the bug?

Then you have at least narrowed the problem to the places where your explanation felt uncertain. Test those claims directly with print statements, a debugger, or a minimal example, and consider asking a colleague with your written summary ready.