Safe Extract-Function Refactoring
Extract a block of code into a well-named function while preserving behaviour, with the data flow and side effects spelled out.
When to use it
Use it when a long function mixes several jobs and you want to pull one out. The prompt analyses what the block reads, writes and returns, picks parameters and a return shape, and lists the behaviour risks such as shared mutable state or early returns, so the extraction does not change what the code does.
When not to use it: Do not use it for blocks that depend heavily on many local variables or that are tangled with control flow across the function: plan a larger refactoring instead. It does not replace running the tests.
Required inputs
| Placeholder | What to provide | Example |
|---|---|---|
| {{source_function}} | The whole function containing the block, so the surrounding data flow is visible. | A processRefund(order, request) function of about 120 lines that validates the request, calculates the amount and sends a notification. |
| {{block_to_extract}} | Which lines or which responsibility to extract. | The lines that calculate the refundable amount, from the loop over order lines to the rounding step. |
| {{language_and_conventions}} | Language, naming and style conventions, and whether there is a linter or type checker. | TypeScript with strict mode; pure helpers live in src/domain; camelCase names. |
The prompt
Fill in each {{placeholder}} before you send it.
You are a careful engineer performing an extract-function refactoring. Behaviour must not change.
<source_function>
{{source_function}}
</source_function>
<block_to_extract>
{{block_to_extract}}
</block_to_extract>
<language_and_conventions>
{{language_and_conventions}}
</language_and_conventions>
Analyse before writing code:
1. Reads: every variable the block reads that is defined outside it.
2. Writes: every variable the block assigns or mutates that is used afterwards, including mutation of objects passed in.
3. Control flow: returns, breaks, continues, exceptions or early exits inside the block that would change meaning once extracted.
4. Side effects: I/O, logging, time, randomness or global state inside the block.
5. Decision: the function's name, parameters and return value, and whether it can be pure. If it cannot be pure, say what makes it impure.
Then provide:
- The extracted function and the modified original function.
- A short list of behaviour risks you identified and how the extraction handles each.
- Two or three tests, or checks, that would show the behaviour is unchanged, with the inputs to use.
Rules:
- Preserve behaviour exactly, including odd behaviour. Do not fix bugs, rename unrelated things or reformat code outside the change. Note any bug you spot separately under "Noticed, not changed".
- Keep the extraction minimal: do not introduce classes, new dependencies or abstractions.
- If the block cannot be extracted cleanly without changing behaviour, say so and describe the smallest preparatory step instead of forcing it.
- Do not claim the result compiles or passes tests. You have not run it. State any assumption about types or code you have not been shown.
Output format:
Analysis (reads, writes, control flow, side effects)
Decision (name, signature, purity)
Code (the extracted function, then the updated original)
Behaviour risks
Checks to run
Noticed, not changedExpected output
An analysis of what the block reads, writes and does as side effects, a decision about name and signature, the extracted and updated code, and checks to prove behaviour is unchanged. For the refund example it would produce a pure function that takes the order lines and returns the amount, note that rounding order matters and must be preserved, and list any mutation of the order object as a risk.
Analysis
Reads: order.lines, request.lineIds. Writes: refundable (used after the block). Control flow: one continue inside the loop, which stays inside the extracted function. Side effects: none.
Decision
calculateRefundableAmount(lines, lineIds): number - pure.
Behaviour risks
- The original rounds once at the end, not per line. The extracted function keeps that order.
Checks to run
- An order with two lines, one requested: compare old and new amounts.
- A request listing a line id that is not on the order: the original skips it, so the extraction must as well.
Noticed, not changed
- A requested quantity larger than the ordered quantity is not rejected.Limitations
- The assistant does not run the code, so claims about type correctness or unchanged output are unverified until you run the tests.
- It can miss shared state that is mutated in ways not visible in the function you provided.
- Its choice of name and parameters follows the code in front of it, not your wider domain language. Adjust names to match your team's terms.
- For dynamically typed languages, the analysis of reads and writes is less reliable, and the risk of missed dependencies is higher.
How to validate the output
Do these before relying on anything the model returns.
- Written by
- PromptFable Editorial
- Last updated 13 Nov 2026
- Review
- Not yet reviewed
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.