DEV Community

Cover image for The Rubber Duck Was Right. Explaining Code to Yourself Makes It Stick.
Spartan Rich
Spartan Rich

Posted on

The Rubber Duck Was Right. Explaining Code to Yourself Makes It Stick.

Explaining things to yourself, line by line, is one of the best-studied learning habits in cognitive science. Programmers found it by accident, with a bath toy.

In 1999, Andrew Hunt and David Thomas put a short section called "Rubber Ducking" into The Pragmatic Programmer. The advice was simple. When you're stuck, explain the code to someone, line by line. They don't need to say anything. "The simple act of explaining, step by step, what the code is supposed to do often causes the problem to leap off the screen and announce itself." The name came from Greg Pugh, a research assistant Dave Thomas worked with at Imperial College, who carried a small yellow rubber duck around and set it on his terminal while he coded.

Every developer has had the duck moment. You open a chat to ask a coworker about a bug, you type out what the function should do, and halfway through the second sentence you see it. You delete the message. The coworker never knows.

What most of us don't know is that cognitive scientists had been studying the same thing, from the learning side, for ten years before the book came out. They gave it a drier name: the self-explanation effect.

A sparse scatter of glowing points on the left becomes a dense, bridged network on the right

A handful of students and a physics textbook

In 1989, Michelene Chi and her colleagues at the University of Pittsburgh's Learning Research and Development Center ran a slow, careful study. Ten recruited students worked through the mechanics chapter of a physics textbook in the lab, spread over several weeks. None had taken college physics. While they studied three worked examples (strings, inclined planes, pulleys), they talked out loud, and the researchers taped every word.

Afterwards everyone solved problems, and the group split into "Good" and "Poor" learners by score. The split wasn't explained by what they knew going in. Both groups had done about equally well on the earlier questions about Newton's laws.

The difference was in how they read the examples. The good students kept stopping to explain each step to themselves. Why does this force point that way? What would change if the surface had friction? Which law is this line using? They also noticed, out loud, when something didn't make sense. The poor students mostly paraphrased, said they understood, and moved on.

Later, when they got stuck on a problem, the poor students went back to the worked example and reread it from the top, hunting for something to copy. The good students jumped to the one line they needed.

If you've ever scrolled a Stack Overflow answer from the top for the third time, looking for the bit to paste, you've been in the second group. So have I.

Then someone made it an experiment

A study this small only shows a link. Maybe good students explain more because they're good students. So in 1994 Chi's team tested whether you could cause the effect.

They took 24 eighth graders and a textbook passage on the human circulatory system. Fourteen were asked to stop after every single sentence and explain to themselves what it meant. That was the whole intervention: no training, no special technique. The other ten read the same passage twice.

A student pausing over a textbook, with a faint watercolour diagram of the heart's double loop behind them

Both groups improved from pretest to posttest. The prompted group improved more, 32% against 22%, even though the control group got a second full read. On the hardest questions, the ones that needed inference and couldn't be answered by pointing at a sentence, the gap was wider: 22.6% against 12.5%.

The detail I like most is about mental models. Blood circulation is a double loop: heart to lungs and back, then heart to body and back. Every one of the students who explained a lot ended up with that correct model. Many of the others didn't. They had read the same words. The explaining is what turned the sentences into a working picture.

Why talking to nobody works

Explaining forces you to find the gaps. Reading lets you skip them.

When you read code or a doc, your brain fills holes with "yeah, makes sense" and keeps going. When you try to say why line 14 comes before line 15, there's nowhere to hide. Either you can connect it to something you already know, or you can't, and now you know you can't. Chi's good students were doing exactly that: linking each step to a principle and flagging the places where the link broke.

That's also why the duck finds bugs. A bug often lives in the space between what you think the code does and what it does. Explaining makes you state the first part out loud, so the second part has to match it.

Does it hold up?

Mostly, yes, with fair caveats.

In 2018, Kiran Bisra, John Nesbit, Philip Winne and colleagues at Simon Fraser University pooled the research on prompting people to self-explain: 69 effect sizes from 64 reports, almost 6,000 participants. The overall effect was g = 0.55, a solid, medium-sized benefit. Against doing nothing extra, it was larger (0.67). Self-explaining even beat being handed a ready-made explanation by an instructor (0.35).

The big 2013 review of study techniques by John Dunlosky and colleagues is more cautious. It rated self-explanation "moderate utility". It works across ages and subjects, but most studies test people within minutes, not weeks, and it can add a lot of study time. Some students also need practice, because left alone they paraphrase instead of explaining. "This line loops over users" is a paraphrase. "This loops over users because the API returns them paged, so we can't index directly" is an explanation.

So: a real effect, well replicated in the lab, with open questions about how long it lasts on its own.

Try it on the next thing you learn

Next time you read unfamiliar code, a new library's docs or a tutorial, stop after each chunk and answer one question in a sentence: why is this here? Not what it does. Why.

Write it as a comment, mutter it, or type it into an empty file nobody will see. If you can't answer, that's the spot to slow down. You've found your gap before it found you in production.

It works on worked examples especially well. Instead of copying a snippet, go through it line by line and justify each line to the duck. It feels slower. Chi's good students spent close to twice as long on the examples as the poor ones did. They also didn't have to come back.

Where I've been using it

I built Recl because I kept saving talks and articles and losing them a week later. It turns what you watch or read into key takeaways and brings them back later as recall questions. The habit above makes it work better. When a question comes back, I try to answer the why in a full sentence before I check, instead of just recognising the answer.

A saved item in Recl showing its summary and key takeaways, with an Overview and Recall tab

You don't need any app for this, though. You need one question and a willingness to sound a bit silly talking to a duck. The research suggests it's worth it.


Asutosh writes about memory and how brains learn, and builds Recl, an app for remembering what you watch and read.

Sources

Top comments (0)