Iterate on Diffs, Not Rewrites
Once code exists, resist the urge to ask for the whole thing again. Vague follow-ups like "make it better" throw away working code and reintroduce bugs you already fixed. Point at the specific change you want.
That's not right, rewrite the whole thing.
The function works, but two issues:
1. The error on line with `throw new Error` should return a
Result type instead of throwing — match the pattern in
the validateUser function above.
2. The loop re-reads `items.length` each iteration; hoist it.
Show me just the diff for those two changes.
The healthy cycle is a tight loop: the model, read the output, then refine with a small targeted diff — never a full rewrite — until it is right:
┌─────────┐ ┌─────────┐ ┌─────────┐
│ PROMPT │ ──▶ │ MODEL │ ──▶ │ OUTPUT │
└─────────┘ └─────────┘ └────┬────┘
▲ │
│ ▼
│ ┌─────────┐
│ good? │ REVIEW │
│ yes ◀──────────────────┤ diff │
│ └────┬────┘
│ │ no
│ refine (small diff) │
└───────────────────────────────┘
drift? ▶ reset to last good
Asking for a diff (rather than a full rewrite) keeps changes reviewable and preserves the parts that already work. Treat the AI's output like a colleague's : comment on specific lines, request targeted edits. When a change goes sideways and the code drifts further from what you wanted with each reply, do not keep patching — reset to the last version you trusted and re-prompt from there with a sharper description. Iterating forward on a broken base just compounds the mess.