Agent Upstart

Cool things people are building with agents — and practical help getting the most out of them.

Playbooks · Coding agents

Refactor a messy function with an agent, keeping behaviour

A solo builder cleans up one tangled function with Claude Code, with passing tests proving the behaviour did not change.

Audience: Solo builder · Stack: A git repo where the function already has tests, or where you can add them first · Tool: Claude Code (terminal CLI)

Before you start

Steps

  1. Run the function's tests and record that they pass. This is the baseline the whole refactor is judged against.
  2. Start Claude Code with claude in the project directory. Type /plan to enter plan mode and describe the refactor as a constraint, not a style preference: same inputs, same outputs, same edge cases, clearer structure. Name the specific smells to remove — for example, a nested condition, a duplicated block, or a misleading name — and nothing else.
  3. Read the plan. Reject any part that adds behaviour, changes an output, or “fixes” something while refactoring. A refactor that also fixes a bug is two changes, and the tests cannot tell you which one broke what.
  4. Let the agent refactor in small steps, running the function's tests after each step. If a step fails a test, stop and revert that step before continuing; do not let the agent adjust the test to fit the new code.
  5. When the tests pass, read the new function in full, without the diff, as a reader would. If you still cannot follow it, the refactor is not done — say what is still unclear and ask for one more pass on that part only.
  6. Run the wider suite for the area around the function, not just its own tests. A refactor can break callers even when the function's own tests pass.
  7. Commit with a message that says refactor only, and note the test command and its result in the commit message or pull request, so a reviewer can repeat the check.

Worked example

A solo builder has a function that prices an order: forty lines, three nested conditions, and a discount block copied twice. The function's tests pass. In plan mode they brief: “Same inputs, same outputs, same edge cases. Remove the duplicated discount block and flatten the nesting. Change no prices, no rounding, and no error messages.” The agent extracts the discount into one helper and flattens one condition; the tests pass after each step. Reading the result fresh, the builder finds one variable name still misleading, asks for that rename alone, re-runs the tests and the surrounding suite, and commits a refactor-only change. The function is shorter, the prices are identical, and the tests are the evidence.

What can go wrong

Limits

Time and cost: NOT_MEASURED. This page gives no time or cost figure because the tool docs fetched for it state none that applies to this task.

Verification

Checked 2026-10-23 against the author's own docs. The workflow on this page was not run end to end; every command, flag, and capability claim was checked against an author doc or repo fetched that day.

Cut from this page, with reason:

If a step here does not match what your installed tool shows, trust the tool's own docs for your version and stop before approving a change you cannot read.

Back to: Coding playbooks hub · Skills index