Sub topic 8
Apply one change at a time
This chapter assumes you can run Python code and tests from a terminal, use basic git commands (status, diff, commit, restore), and have already asked an assistant for a diagnosis. The goal is to apply its suggestion in a way that lets you see what worked and undo what didn't.
Why small steps matter
An assistant often returns a large answer: a fixed function, a renamed variable, a new import, and a refactor you didn't ask for. If you paste all of it in and the program still fails, you can't tell which part helped, which did nothing, and which broke something else.
Applying one change at a time gives you a clear cause and effect. Each change either fixes the failure, leaves it unchanged, or produces a new failure. You learn something from each result.
The loop
- Start from a clean state. Commit or stash your work so
git statusshows nothing unexpected. - Pick the smallest change that tests the diagnosis.
- Apply only that change.
- Re-run the reproduction from the earlier chapter.
- Read
git diffand decide whether to keep, adjust, or discard the change. - Commit the change if it helps, with a message that names the cause.
A worked example
Recall the failing function from the earlier chapters:
def average(values):
return sum(values) / len(values)
print(average([]))Suppose the assistant proposes three edits at once: reject empty input, return 0 for an empty list elsewhere in the codebase, and rewrite the function to use statistics.mean. The first edit matches the diagnosis. The other two are unrelated to the traceback.
Apply only the first:
def average(values):
if not values:
raise ValueError("average() requires at least one value")
return sum(values) / len(values)Run the reproduction again. The error is now a ValueError with a message that tells the caller what went wrong. The behavior matches the expected result you stated in your prompt. Check the diff, then commit:
git diff
git commit -am "Reject empty input in average() instead of dividing by zero"Now consider the other two edits separately. Returning 0 would hide the bug, because callers could not distinguish an empty list from a real average of zero. Switching to statistics.mean is a refactor, not a fix. Leave both out, or handle them later as their own commits.
Keep changes reviewable
- Ask the assistant for the smallest change that fixes the problem. A prompt line such as "Change only what is needed to fix this error" helps.
- Use
git diffbefore every commit. Look for edits outside the lines you expected. - If you must accept a multi-part answer, split it with
git add -pand test each part. - Revert a failed attempt with
git restore <file>before trying the next idea. Stacking failed attempts makes the next result harder to read.
Common errors
| Mistake | Effect | Better approach |
|---|---|---|
| Pasting the whole reply into the file | You can't tell which edit mattered | Apply the edit that matches the diagnosis first |
| Skipping the re-run | You assume the fix works | Run the reproduction after every change |
| Not committing working steps | You can't return to a known good state | Commit each verified step |
| Keeping unrelated edits | Review gets harder and new bugs appear | Split or discard them |
Exercise
An assistant suggests four edits to fix a failing function. You apply all four and the error changes to a different one. Why is this a problem, and what should you do?
Show answer
You can't tell which edit caused the new error, and some edits may be unrelated to the failure. Revert to your last clean commit, then apply the edit that matches the diagnosis on its own and re-run the reproduction. Repeat for the remaining edits one at a time, keeping only those that help.