Skip to content

Refactor tangled code into small functions

Splits a long function into well-named units without changing behaviour, and explains each step.

By Editorial team 2,300 uses 144 likes

Prompt

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.

Example output

Extracted validate_input(), build_query() and format_result()...

Why this prompt works

  1. Refactor this {{language}} function into smaller, well-named functionsStates the goal as structure, not as "improve", which is vague enough to produce a rewrite.
  2. without changing its behaviourThe key constraint. It tells the model that outputs, side effects and errors must stay the same.
  3. Keep the public signature.Protects every caller. Without this line, parameters often get renamed or reordered.
  4. 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.
  5. Point out any behaviour you were unsure about.Surfaces edge cases (error paths, mutation, ordering) you should test before merging.

Variables

Placeholders in this prompt
PlaceholderWhat to putExample
{{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.

Your prompt

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.

Variations

Short: extract one thing

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}}

Detailed: with tests first

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}}

Remove nesting

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}}

Different use: explain before changing

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}}

How to use it in each model

  • 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.

How to use this prompt

Use this prompt as a starting point and adapt the variables to your project.

  1. Fill in the variables so the prompt matches your situation.
  2. Run it in Claude and review the first result.
  3. Ask for one change at a time, such as tone, length or composition, rather than rewriting the whole prompt.

Tip: keep a copy of the version that worked so you can reuse it.