Why AI refactors are risky
A refactor is supposed to change how code reads, not what it does. An assistant asked to “clean up” a file can produce something that looks tidier and still renames a status string, drops a .trim(), or moves an event listener so it no longer fires. The instructor does not treat the refactor as cosmetic: the goal is better readability, structure or polish, with proof that the board still behaves the same.
Key ideaRefactoring with AI is risky because clean-looking code can quietly change behavior. Write the contract first, then refactor.
Write the behavior contract first
The contract is a short list of behaviors you check by hand, each marked pass or fail, before and after the refactor. If an item already fails, fix it before refactoring. The notes require eight items but do not list them; prompt D1 names the eight behaviors to cover.
- Three status columns render with correct counts.
- Cards sort by priority inside each column.
- Loading state appears before tasks load.
- Empty columns remain visible.
- Error state appears when the backend is stopped.
- Valid drag sends PATCH and updates the board.
- Invalid drag/server 422 reverts and shows the server message.
- New Task and Edit modal flows still work, including title validation and dismissal.
Read with careThis list comes from prompt D1 in the library, not the lecture notes. Prompt D3’s review list groups the same ground differently: it splits title trimming and the dismissal paths into their own items and folds the loading, empty and error states into one. Either works; what matters is that you write yours down and use it both times.
The safe refactor workflow
Seven steps. Steps 2 and 5 are the ones that make the rest recoverable: a checkpoint to roll back to, and a diff you actually read.
- Run the app and mark each contract item PASS before refactoring. If a behavior already fails, fix it first.
- Commit or otherwise checkpoint the working state so rollback is easy.
- Select one section at a time: render logic, drag handlers, modal functions, submit handler, or CSS block.
- Ask the assistant for a focused cleanup of the selected section only.
- Inspect the diff before accepting. A diff is a proposal, not a decision.
- Run the browser and re-check the contract after every accepted change.
- If something breaks, select the smallest broken section and fix that specific regression. Do not ask for a full-file rewrite.
- D1Contract passesAll 8 items PASS
- CommitA checkpoint to roll back to
- D2Select one sectionFocused cleanup only
- D3Inspect the diffA proposal, not a decision
- Re-check the contractAfter every accepted change
If one item regresses, fix only that section (D4) and re-check.
A diff is a proposal, not a decision
Reading a diff is faster when you know what to look for. The notes list seven red flags, each tied to a behavior from the earlier lessons.
| Red flag | Why it matters |
|---|---|
| Status strings changed | Sending "In Progress" instead of "InProgress" can break the API contract. |
| URLs or methods changed | GET /tasks and PATCH /tasks/{id} must stay aligned with the backend. |
| Class names or data attributes removed | Rendering, CSS, or event handling may depend on them. |
| .trim() removed from title validation | Whitespace-only titles can slip through the client layer. |
| 422 handling or revert path removed | The UI may appear to save a change the server rejected. |
| Event listeners moved carelessly | Re-rendering with innerHTML can remove listeners if the code does not reattach or delegate correctly. |
| The assistant rewrites the whole file | Large diffs are harder to inspect and more likely to introduce hidden regressions. |
Recover one regression surgically
When one contract item fails after a change, the tempting move is to ask the assistant to redo the refactor, or to undo everything. Both throw away what is working. Use the contract to name the exact behavior that broke, select the smallest section involved, and ask for a fix to that section only, without changing the check you expect to pass. Prompt D4 frames this as: what worked before, what is broken now, the selected code, and a patch that restores one contract item.
Key ideaOne observed failure, one selected section, one focused patch, one re-check.
The habit that transfers
The same four moves apply to any AI-assisted change to working code, not only to this board. Write down what must not change, make rollback cheap, keep each change small enough to read, and verify after each one.
Key ideaContract first, checkpoint second, selected refactor third, verification after every accepted change.
Key takeaways
- Write the eight-item contract and see it pass before you refactor.
- Commit first, so rollback is one command away.
- Refactor one selected section at a time and read the diff.
- Watch for changed statuses, URLs, selectors, .trim(), revert paths and listeners.
- Fix a regression in the smallest section, never with another full-file rewrite.