Hypothesis and falsification
Turn a vague failure report into a reproduction, isolate the cause, form and falsify hypotheses, instrument the code, fix async and state bugs, lock the fix behind a regression test and review changes for real trade-offs.
What you will be able to do
#- Rewrite a vague guess as a specific, testable hypothesis.
- Design the cheapest test that could disprove the hypothesis.
Overview
#A hypothesis is a specific, testable claim about the cause. It is probably a caching issue is not a hypothesis. The response is cached because the request omits the Authorization header, so the browser serves a stale 200 is.
Good debugging is the scientific method under time pressure. State the claim, predict what you should observe if it is true, then run the cheapest test that could prove it false. If the prediction fails, the hypothesis is wrong and you discard it.
The trap is confirmation bias. Once you believe the cause, you start noticing evidence that fits and ignoring evidence that does not. Counter it by asking what you would see if the opposite were true, before you look.
// Hypothesis: the list is empty because the API returns an empty array.
const res = await fetch(url);
const data = await res.json();
console.log('status', res.status, 'length', data.length);
If status is 200 and length is 0, the hypothesis holds and the bug is upstream. If status is 401, the hypothesis is falsified and the real cause is authentication. One log line distinguishes the two.
Change one variable per experiment. If you clear the cache, restart the server and change the config at once, a fix tells you nothing. Keep a short written log: hypothesis, test, result. It stops you from re-testing the same dead end, and it becomes the timeline for a later postmortem.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Explain
A teammate says the page is slow because of React. Rewrite this as a falsifiable hypothesis and describe the cheapest test that could prove it false.
Reveal the reference answer
reference.textHypothesis: the page is slow because React re-renders a large list on every keystroke. Prediction: typing in the search box triggers a full list render and the frame time spikes. Cheapest test: open the profiler, type one character and count the renders of the list component. If the list does not re-render, the hypothesis is false and the cost is elsewhere, such as a network call or a layout read.Authored expected output: A specific claim with a prediction and a cheap measurement that could disprove it.
A hypothesis becomes useful when it predicts an observation. The prediction is what you look for, and a cheap measurement that could contradict it keeps the cost of being wrong low.
Exercise 2Predict
You believe a bug is caused by a stale cache. Which observation would falsify that hypothesis?
Common misconceptions
#- A likely cause does not need a test if it feels right.
- Changing several things at once speeds up a fix.
Follow-up practice
#- debugging-review-05-instrumentation
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.