Working with Claude Code: 4. Iterating

This is the most common scenario in software development. You get a working draft, but it needs polish.

The biggest risk here is context bloat. If you just keep saying "fix this, now fix that," Claude's context window will fill up with the history of the old code, the bugs, and the fixes, eventually causing it to lose track of the current state of the file.

Here is the best workflow for iterating on code while keeping Claude's context perfectly clean and up to date.


1. The "Checkpoint and Clear" Strategy (Golden Rule)

The most important habit to build is creating checkpoints. When Claude gives you that "OK" initial version, you should treat it as a save point in a video game.

The Workflow:

  1. Review the initial code: Claude writes the feature. It works, but needs improvements.
  2. Commit the code: Do not ask Claude to fix it yet! First, drop to your terminal (or ask Claude) and commit the current working state.
  3. Make the improvements: Tell Claude exactly what needs to be fixed (more on this below).
  4. Clear the context: Once the fixes are applied and working, type /clear.
  5. Start fresh: Claude's memory is wiped. However, because you committed the code, the files on your disk are up to date. When you start your next task, Claude will read the current, updated files.

Why this works: You don't carry the history of the "bad" code in Claude's memory. It only sees the final, improved version.


2. How to Prompt for Modifications (Be Targeted)

When asking for those initial improvements or bug fixes, be highly specific to prevent Claude from rewriting the entire file from scratch (which wastes tokens and can introduce new bugs).

Rule: Tell Claude what to touch, and explicitly tell it what NOT to touch.

3. The Feedback Loop for Bug Fixes

If the bug requires running the code to see the error, let Claude do the debugging. Do not copy-paste terminal errors into the chat if Claude can run the command itself.

The Workflow:

  1. State the problem: "When I run npm start, the app crashes when I click the submit button."
  2. Ask Claude to investigate: "Please run npm start, look at the error, identify the bug in Form.js, and fix it."
  3. Claude runs the command: Claude will execute npm start, read the stack trace from the terminal output directly into its context, find the line in Form.js, and propose the fix.

Because Claude reads the terminal output itself, it gets the exact, unmodified error message and the exact file state simultaneously.

4. Updating Long-Term Context (CLAUDE.md)

If the "improvement" you are making changes the overall architecture or rules of the project, you must update CLAUDE.md after the fix is applied.

Example: You ask Claude to fix a slow database query. It fixes it by adding an index and changing the ORM syntax.

This ensures that the next time you /clear the context, Claude won't revert to the old, slow way of doing things.

5. Using git diff for Review

When Claude applies the modifications, you shouldn't just trust them. You can ask Claude to review its own changes using Git.

Claude will look at the diff (which is very token-efficient compared to reading whole files) and often catch its own minor mistakes before you commit them.

Summary Checklist for Iterations:

  1. Get the "OK" version working.
  2. Commit it (Create a checkpoint).
  3. Tell Claude exactly which function/line needs the bug fix, and tell it to leave the rest of the file alone.
  4. Let Claude run the code and read the errors itself.
  5. Once fixed, commit again.
  6. Type /clear to wipe the history of the bug fix from Claude's memory.
  7. Update CLAUDE.md if the fix introduced a new project rule.

ai

Back