For the quickest useful change.
In this {{language}} function, extract the most tangled block into a well-named helper. Keep the public signature and behaviour. Show only the changed code and one sentence on why.
{{code}}
Splits a long function into well-named units without changing behaviour, and explains each step.
Refactor this {{language}} function into smaller, well-named functions without changing its behaviour. Keep the public signature.
{{code}}
Show the refactored code, then list each change and the reason. Point out any behaviour you were unsure about.
Extracted validate_input(), build_query() and format_result()...
Refactor this {{language}} function into smaller, well-named functionsStates the goal as structure, not as "improve", which is vague enough to produce a rewrite.without changing its behaviourThe key constraint. It tells the model that outputs, side effects and errors must stay the same.Keep the public signature.Protects every caller. Without this line, parameters often get renamed or reordered.Show the refactored code, then list each change and the reason.The list makes the diff reviewable: you can accept or reject each change on its reason.Point out any behaviour you were unsure about.Surfaces edge cases (error paths, mutation, ordering) you should test before merging.| Placeholder | What to put | Example |
|---|---|---|
{{language}} |
The language and version, e.g. Python 3.12 | Python |
{{code}} |
One function or small module. Include its tests if you have them | paste your function here |
Fill in the fields below and the prompt updates as you type. Leave a field empty to keep its placeholder.
Refactor this {{language}} function into smaller, well-named functions without changing its behaviour. Keep the public signature.
{{code}}
Show the refactored code, then list each change and the reason. Point out any behaviour you were unsure about.
For the quickest useful change.
In this {{language}} function, extract the most tangled block into a well-named helper. Keep the public signature and behaviour. Show only the changed code and one sentence on why.
{{code}}
Safest when you have no tests yet.
Before refactoring this {{language}} function, write characterization tests that capture its current behaviour, including edge cases and error paths. Then refactor into smaller, well-named functions without changing behaviour or the public signature, and confirm which tests cover each new function.
{{code}}
For deeply nested conditionals.
Reduce the nesting in this {{language}} function using early returns and guard clauses. Keep behaviour and the public signature identical. List each condition you moved and confirm the order of checks is unchanged.
{{code}}
For legacy code you do not yet understand.
Do not change this {{language}} code yet. Explain what each block does, list hidden assumptions and side effects, and propose a refactoring plan in steps small enough to review one at a time.
{{code}}
Works as written. Paste the callers or interfaces under the function as context, and say they must not change.
Paste one function at a time. Run your tests after each change; if you have none, use the "tests first" variation.
Works as written. Ask it to keep your existing naming style by pasting two or three nearby functions as examples.
Use this prompt as a starting point and adapt the variables to your project.
Tip: keep a copy of the version that worked so you can reuse it.