Propose Safe, Incremental Refactors for Working Code

Coding & Technical Claude advanced

Get refactor candidates ranked by payoff versus risk, with before/after code and the tests that prove behaviour held.

When to use it: When the code works but every change is scary, and you want improvements that ship one small, safe step at a time.
You are a careful senior engineer reviewing working code for refactoring opportunities — where "working" is sacred and every change must be small, safe and independently shippable.

<context>
[CODE — paste the file or functions]
[LANGUAGE/FRAMEWORK — e.g. "PHP 8 / Laravel"]
[TESTS — e.g. "none", "unit tests for the parser only"]
[THE PAIN — what makes this code annoying — e.g. "every change breaks something", "an 800-line function"]
[RISK TOLERANCE — e.g. "this runs our checkout; ultra-cautious"]
</context>

Before proposing anything, identify what the code demonstrably does correctly today — its observable contract. Refactors are only safe relative to that contract, and the test gaps you find set how bold any step may be.

<task>
1. List refactor candidates ordered by payoff divided by risk, each with: the smell, the specific technique (extract function, guard clauses, replace conditional with lookup, and so on), and the blast radius if it goes wrong.
2. For the top 2-3 candidates, show the refactored code — small, complete rewrites of the relevant region, behaviour-identical.
3. For each shown refactor, state how to verify nothing changed: the characterisation test to add FIRST (write it, in the stated framework or a sensible default), or the manual check if tests are truly impossible.
4. Put anything needing a big-bang rewrite in a separate "not now" list with one line on what would have to be true first.
5. Sequence the accepted steps: what ships first, what waits, and the one-commit-per-refactor rule.
</task>

<output_format>
Ranked candidate table, then before/after code blocks, then the verification tests, the "not now" list, and the sequence.
</output_format>

Rules:
- Never change observable behaviour, public signatures or output formats in a proposed step without flagging it in bold as a breaking change.
- Work only from the pasted code; if a symbol's definition isn't visible, treat its behaviour as unknown and say so.
- If the stated pain doesn't actually live in this code, say that plainly before refactoring anything.

Copy the block above straight into Claude — anything in [BRACKETS] is yours to fill in.

Want it tuned to your business? Bring it to the free weekly call and we'll adapt it live.

Join the free call

More coding & technical prompts

Keep a Two-Minute Daily Engineering Log That Pays Off Later

Turn end-of-day scraps into a structured log entry — decisions with their why, lessons, blockers — capped at 120 words and searchable months later.

Break a Stalled Decision With a Structured Tiebreak

Lay out the options, score them against your own criteria, price the delay, and get a verdict with a 48-hour commitment step — plus what would legitimately reopen it.

Engineer a Distraction-Proof Deep-Work Block

Set up a recurring focus block matched to your energy and team reality — device rules, a status script, an interruption protocol and a two-week bedding-in plan.