The scientific debugging method
This course treats debugging as a four-step method borrowed from experimental science: observe the real behavior, form one falsifiable hypothesis, design the smallest test, and change one variable at a time. Every lesson follows one bug in a disposable ScratchDebugArticle model, where published? returns false even though published_at is set. Each hypothesis is tested at the bin/rails console prompt. Two hypotheses are falsified, and source_location leads to a stray ! that inverts the result. The final lesson replays the whole session in order.
What you'll learn
- ✓Why random code edits fail as a way to debug
- ✓Recording what was called, what was expected and what came back
- ✓Writing a hypothesis precise enough to be proven wrong
- ✓Designing the smallest test that answers one hypothesis
- ✓Testing one variable at a time at the Rails console
- ✓Finding a method's definition with source_location and confirming a fix
Guessing isn't debugging
Picture a bug: article.published? returns false when it's supposed to return true. A common first move is changing something and rerunning it: add a .presence here, swap nil for false there, reorder two lines, sprinkle a puts and squint at the output. Four edits later the bug is still there, or…
Read the full lesson free →Curriculum
7 lessonsGuessing isn't debugging
Contrasts random code edits with the four steps of the scientific method applied to a real bug.
Observe what actually happens
Records what published? returns at the console, comparing the expected true against the actual false before guessing causes.
One hypothesis, stated precisely
Shows how to state one specific hypothesis about the bug so that a test could prove it wrong.
The smallest test that could prove it wrong
Uses one console line, article.title.present?, to falsify the first hypothesis without testing anything else.
One variable at a time
Rules out a nil published_at, then uses source_location to find where published? was defined.
From confirmed hypothesis to fix
Reads the line source_location found, spots a stray !, and confirms the fix by rerunning the same call.
Putting it together: debugging published?
Replays the whole session in order, from the first observation through three hypotheses to the one-character fix.