Isolate by bisection
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
#- Shrink a failing case by halving the input or the code path.
- Use git bisect to find the commit that introduced a regression.
Overview
#Once you can reproduce a failure, the next job is to shrink it. Isolation is the art of removing everything that is not the bug.
Bisection is the general technique. If a list of 64 inputs fails somewhere, test the middle. If it fails, the trigger is in the first half; if it passes, in the second. Six checks locate the boundary instead of sixty-four.
The same idea applies to code. Comment out half the function, stub half the dependencies, or disable half the feature flags. Each step halves the search space. The goal is the smallest program that still fails.
function applyDiscount(order, user) {
const base = order.items.reduce((sum, i) => sum + i.price * i.qty, 0);
const member = user.tier === 'gold' ? 0.1 : 0;
const seasonal = order.coupon === 'WINTER' ? 0.05 : 0;
return base * (1 - member - seasonal);
}
If a discount bug is reported, do not read the whole function. Call it with one item, no coupon and no membership. If that is correct, add one factor at a time. The first factor that breaks the result is the trigger.
Version control gives you the same tool at a larger scale. git bisect finds the commit that introduced a regression by testing the midpoint commit, then halving again. You need a command that reliably tells good from bad, which is exactly the reproduction from the first lesson.
Isolation also means removing the environment. If a failure only happens in production, bisect the difference: same data, same config, same network. Change one axis at a time until the failure follows.
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 bug only appears when a list has more than 100 items. Describe how you would isolate the trigger using bisection on the input, and what the smallest failing case tells you.
Reveal the reference answer
reference.textStart with 101 items and confirm the failure. Then test 50 items. If it passes, the trigger is between 51 and 101; if it fails, between 1 and 50. Repeat, halving the range each time, until the boundary is a single size. That size is the threshold, and the code that runs only above it is the suspect. The smallest failing case becomes the regression test.Authored expected output: A described bisection procedure that halves the input range until it finds the exact threshold size, then names the smallest failing case.
Each test halves the remaining range, so the number of tests grows with the logarithm of the input size. The threshold size points at the code that runs only above it, and the smallest failing case is the reproduction you keep.
Exercise 2Predict
A regression was introduced somewhere in the last 64 commits. Using git bisect, roughly how many commits must you test to find it?
Common misconceptions
#- Reading the whole function is faster than bisecting the input.
- git bisect needs a perfect test, so it is not worth using.
Follow-up practice
#- debugging-review-04-hypothesis-and-falsification
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.