Cursor Refactoring Prompt for Developers
The code ends up easier to read and change while behaving exactly the same for every input, with each change explained. This prompt is tuned for developers working in Cursor: it carries your role context, and the steps below cover what changes in Cursor. Check that every function, method and package the AI uses actually exists in your installed versions, and run the tests before you commit.
Refactor the code below. The behavior must stay exactly the same. ## Code Language: [e.g. Python 3.12] [paste the code] ## What bothers me about it [e.g. deep nesting, a 200-line function, duplicated logic, unclear names] ## Constraints - Keep the public function names and signatures unchanged: [yes or no] - Do not add new dependencies. - Match the existing style: [e.g. type hints, no classes] ## Instructions - Make one kind of improvement at a time (naming, extraction, simplifying conditionals) so each is easy to review. - Do not change error messages, return values or side effects. - If you find a bug, point it out separately. Do not silently fix it. ## Output format 1. The refactored code in full 2. A numbered list of each change and why it helps 3. Any input where you are not 100% sure the behavior is identical About my work - Language, framework and versions: [e.g. TypeScript 5, Next.js 15, Node 20] - Relevant files or functions: [e.g. lib/billing.ts, the createInvoice function] - Conventions to follow: [e.g. no classes, Zod for validation, Vitest for tests] Context - Files involved: [list the files, or reference them in the chat] - How I build and test: [e.g. npm test]
How to use this prompt
- 1
Open Cursor
Open the chat panel in Cursor and paste the prompt as a new message.
- 2
Give it context
Use @ to attach the files or folders the prompt is about, so Cursor reads them instead of guessing.
- 3
Review and iterate
Review each proposed diff before you accept it, then reply in the same chat to adjust.
- 4
Save what you repeat
Put rules you repeat (stack, style, test command) in a project rules file so every chat starts with them.
Working in Cursor
- •Cursor works on files in your open project, so name the files and functions you mean.
- •For a small, local change, select the code and use the inline edit command instead of the chat panel.
- •Long chats drift. Start a new chat for a new task and re-attach only the files it needs.
Worked example
What you give it
Language: Python 3.12
What bothers me: too much nesting, and it is hard to see the free shipping rule.
Keep signature: yes. No new dependencies.
def shipping_cost(order):
if order is not None:
if order.country == "US":
if order.total >= 50:
return 0
else:
return 5
else:
return 15
else:
raise ValueError("order required")What a good answer looks like (shortened)
1. Refactored code:
def shipping_cost(order):
if order is None:
raise ValueError("order required")
if order.country != "US":
return 15
return 0 if order.total >= 50 else 5
2. Changes:
1. Moved the None check to the top as a guard clause, so the error case exits first.
2. Inverted the country check, which removes a level of nesting.
3. Collapsed the final if/else into a conditional expression.
3. Behavior check: identical for all inputs. Same exception, same message, same return values.Tips for better results
- •Paste the actual error output and the exact versions you run. Most wrong answers come from the AI assuming a different version of a library or framework.
- •Be specific in "What bothers me about it." Naming one smell, like duplicated validation, gets a focused refactor instead of a rewrite.
- •Cursor works on files in your open project, so name the files and functions you mean.
- •Avoid this: Letting the AI rename public functions or change return types during a refactor, which breaks callers you did not paste.
Check before you use it: Check that every function, method and package the AI uses actually exists in your installed versions, and run the tests before you commit.
Get the free prompt pack
Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.
Variations
With tests first
Add this line to Instructions: Before refactoring, write tests that pin down the current behavior, then show they still pass.
Step by step
Change Output format to: Show each change as a separate before and after snippet so I can apply them one at a time.
Extract a function
Replace What bothers me with: Pull the logic for [specific task] into its own well-named function.
Save it as a Retext snippet
Retext is a free text expander for Mac. Save this prompt once, then type ;refactor in any app (ChatGPT, Claude, your editor, a browser tab) and the full prompt is pasted in.
- Click Add to Retext. The prompt installs as a snippet with the trigger ;refactor.
- Change the trigger in Retext if you prefer a different one.
- Type the trigger wherever you want the prompt, then fill in the brackets.
Frequently asked questions
How do developers keep AI-generated code consistent with their codebase?
Include one or two existing files as examples of your style and state your conventions in the prompt. Ask for changes as small diffs to existing code rather than whole new files.
How do I know an AI refactor did not change behavior?
Run your existing tests before and after. If there are none, ask the AI to write characterization tests against the original code first, then run them against the refactored version.
Should I paste code into Cursor or reference the file?
Reference the file with @. Cursor reads the current version from your project, which avoids pasting stale code and keeps the prompt short.
How do I reuse this prompt without copying it every time?
Click Add to Retext to save it as a snippet. Then type ;refactor in any Mac app, including your AI tool, and the full prompt is pasted in.
Related Templates
Get the free prompt pack
Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.