liveBook · Manning

Rubber ducking

As explained in Get Programming: Learn to code with Python

A debugging technique in which a programmer explains code and its intended behavior aloud in plain language to expose mistakes.

Rubber ducking is a debugging technique: explain each line of code and its intended behavior in plain English to a real or imaginary helper. This makes hidden assumptions visible and encourages skepticism about every line, even simple ones. section 36.3

Use rubber ducking while tracing a failing test:

  • Start with the inputs that caused the failure. section 36.3
  • Pretend you are the computer and execute the program one line at a time. section 36.3
  • Write down each variable value and ask whether it matches your expectation. section 36.3
  • Explain what every line is trying to accomplish without programming jargon. section 36.3

A failing test

For a failed test such as test_abs_0, trace the call to funcs.absolute_value(0), state the expected value after each line, and question any unexpected result. A debugger may display variable values, but you still determine why the value is wrong. section 36.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Provides a procedural tracing routine but stops short of explaining debugger internals.
Examples
Uses failed tests such as test_abs_0 and test_prime_5 alongside the absolute_value function.
What it assumes
Assumes a failing test and basic ability to follow code line by line.
What you can do after
Diagnose errors by tracing execution, checking values, and locating the first unexpected result.
How it connects
Connects debugging with testing, variable values, debuggers, and breakpoints.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.