AI-Assisted Coding · Module 3Lesson 5

Study explanation · Lesson 05 · 13 min

Refactor safely

Part 3.4 treats a refactor as a change that must prove nothing changed. Write an eight-item behavior contract, commit, refactor one selected section, inspect the diff for red flags, and re-check after every accepted change.

Listen to this lesson

Hear the lesson read aloud, with each sentence highlighted as you go.

Narrated by an AI voice.

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.

  1. Run the app and mark each contract item PASS before refactoring. If a behavior already fails, fix it first.
  2. Commit or otherwise checkpoint the working state so rollback is easy.
  3. Select one section at a time: render logic, drag handlers, modal functions, submit handler, or CSS block.
  4. Ask the assistant for a focused cleanup of the selected section only.
  5. Inspect the diff before accepting. A diff is a proposal, not a decision.
  6. Run the browser and re-check the contract after every accepted change.
  7. If something breaks, select the smallest broken section and fix that specific regression. Do not ask for a full-file rewrite.
Contract, checkpoint, selected refactor, verify · Lecture notes pp. 9-10
  1. D1Contract passesAll 8 items PASS
  2. CommitA checkpoint to roll back to
  3. D2Select one sectionFocused cleanup only
  4. D3Inspect the diffA proposal, not a decision
  5. 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.

Diff review red flags · Lecture notes p. 10
Red flagWhy it matters
Status strings changedSending "In Progress" instead of "InProgress" can break the API contract.
URLs or methods changedGET /tasks and PATCH /tasks/{id} must stay aligned with the backend.
Class names or data attributes removedRendering, CSS, or event handling may depend on them.
.trim() removed from title validationWhitespace-only titles can slip through the client layer.
422 handling or revert path removedThe UI may appear to save a change the server rejected.
Event listeners moved carelesslyRe-rendering with innerHTML can remove listeners if the code does not reattach or delegate correctly.
The assistant rewrites the whole fileLarge 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.

Built from the Module 3 lecture notes, prompt library and additional readings; the module videos and slides were not supplied.

Your study checkpoint

Ready to continue?

Mark the lesson complete once you have read it and tried its idea on something real.

Next · Lesson 6