Split a Long Function Without Changing Its Behavior

2 Views
0 Uses

Prompt

ChatGPT
Claude
Grok
OpenRouter
You are a senior {{LANGUAGE}} engineer. This function is too long to understand or test. Break it up while keeping its observable behavior exactly the same.

The function and any helpers it uses: {{LONG_FUNCTION}}
How it's called and what callers rely on: {{CALLERS}}
Style constraints (max function length, naming conventions, typing): {{STYLE}}

Do this:
1. Read it and list the distinct responsibilities it currently has, with the line ranges for each.
2. Propose new functions: a name, its parameters, its return value, and the lines it takes over. Prefer pure functions that take inputs and return outputs over ones that mutate shared state.
3. Write the refactored code: the original function becomes a short, readable sequence of calls.
4. Keep the public signature, return values, exceptions raised, and side-effect order identical. Call out anything you couldn't keep identical.
5. Show a before/after comparison of which parts are now unit-testable on their own.
6. Suggest three tests for the newly extracted pieces.

Don't rename public functions, change error messages, or "improve" logic along the way. If you see a bug, report it under "Noticed, not changed" with the line.

Customize this prompt

Fill in the variables below to personalize this prompt:

ChatGPT
Claude
Grok
OpenRouter

Model Settings

Recommended Models

ChatGPT Claude Gemini

Temperature

0.7

Max Tokens

2000

Additional Notes

Best for: Taming a 200-line function so it can be read and tested

About this prompt

“Split a Long Function Without Changing Its Behavior” is a free prompt for ChatGPT in the Coding & Software category, 188 words long, added to The Prompt Index on October 28, 2026 by The Prompt Index. Copy it with the button above, paste it into ChatGPT or any other AI chat assistant, and send it.

It has 4 placeholders to fill in first: {{LANGUAGE}}, {{LONG_FUNCTION}}, {{CALLERS}}, {{STYLE}}. Use the “Customize this prompt” fields to personalise it before you copy.

Recommended models: ChatGPT, Claude, Gemini.

0 upvotes, sent to an AI tool 0 times, viewed 2 times.

Browse all Coding & Software prompts, the AI prompt database or use the AI prompt finder to search every prompt.