The latest in AI, every dayAI News

Code · July 15, 2026

Refactor a function without breaking its behavior

Paste a slow or messy function and get a clean version, explained change by change, that does exactly the same thing.

We all have that function that works but is scary to touch. This prompt cleans it up, explains every change, and confirms the behavior stays the same, so you refactor with confidence instead of crossed fingers.

For: ClaudeChatGPTClaude Code
Full prompt
Act as a senior software engineer specialized in refactoring, who prioritizes clarity and
performance without changing the code's behavior.

I'm going to paste a function I want to improve. Your job is to refactor it and explain to me
exactly what you changed and why.

Original function: [PASTE THE FULL FUNCTION HERE]
Language: [E.G. JavaScript, Python, TypeScript]
What worries me: [E.G. it's slow, it's hard to read, it has repeated code, it's hard to test]
Constraints, if any: [E.G. I can't change the function signature, it must keep using this library]

Give me:
1. The complete refactored function, ready to copy and paste.
2. A list of the changes, one by one: what you changed and why (performance, readability,
   less duplication, better error handling, etc.).
3. Explicit confirmation that the behavior (inputs and outputs) is EXACTLY the same as
   before, or a clear warning if something changes.
4. If you spot a real bug in the original version, flag it separately: don't fix it silently,
   tell me what you found and why it's a bug.
5. If something about the context isn't clear (e.g. what data types it receives, where this
   function is used), tell me instead of assuming.

Rules: don't rewrite the function into a different style or library unless I ask for it.
Don't add new functionality that wasn't in the original. If you're not sure a change
preserves the behavior, say so explicitly instead of asserting it.
Short link: wandabuilds.ai/p/wcCD

Come back tomorrow for another, or see all prompts.