WriteMyAIPromptFree, no sign-up

AI prompt for refactoring code

This is a ready-made AI prompt for refactoring code, written for Claude and free to copy. Refactoring means changing structure without changing behaviour, and models routinely do both while describing only the first. The behaviour-identical constraint has to be stated explicitly or improvements smuggle in bug fixes. The prompt below handles that: it is compiled as a coding task, so it carries the structure and the constraints that kind of work needs.

The prompt

Written for Claude. Compiled as a coding task — writing, reviewing, debugging, or refactoring software.

<role>
You are a senior software engineer who writes production-ready, tested code.
</role>

<context>
What it does: {{purpose}}. Why I want it refactored: {{motivation}}. Constraints I cannot change: {{fixed_constraints}}. The code: {{code}}.
</context>

<task>
Refactor this {{language}} code.
</task>

<constraints>
- Behaviour must be identical. If a change alters behaviour, say so instead of making it.
- Explain each change and what it improves.
- Do not introduce dependencies that are not already present.
- Return complete runnable code, not fragments.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.
</constraints>

<output_format>
Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.
</output_format>

<approach>
Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.
</approach>

<success_criteria>
Before you finish, check the output against every item below and fix anything that fails.

- Existing tests would pass unchanged against the refactored code.
</success_criteria>

<before_you_start>
If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.
</before_you_start>

Now, refactor this {{language}} code.

Optional: start the reply with ``` to force the output straight into shape.

Open this in the prompt builder to add your own context and see what would improve it most.

What makes refactoring code hard to prompt for

Refactoring means changing structure without changing behaviour, and models routinely do both while describing only the first. The behaviour-identical constraint has to be stated explicitly or improvements smuggle in bug fixes.

Why this prompt works

Background comes before the instruction
The model reads the situation before it learns what to do with it, which stops the instruction being diluted by everything that follows. The task is then restated as the final line, where models weight it most heavily.
Rules a coding task assumes but nobody writes down
Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections. Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle. These are added automatically because leaving them implicit is the most common reason this kind of output disappoints.
Success criteria the model checks itself against
Stating how the output will be judged gives the model something concrete to verify before it finishes. Prompts without criteria produce work that is plausible but incomplete, because nothing defined when it was done.
Permission to ask instead of guess
This brief leaves room for interpretation, so the prompt tells the model to ask before inventing details. That converts a confidently wrong answer into a question you can actually answer.
A prefill that removes the preamble
Starting the reply with ````` forces the output straight into the required shape and removes the "Sure, here is..." opener entirely.

What to replace

PlaceholderWhat to put there
{{purpose}}Your purpose.
{{motivation}}Your motivation.
{{fixed_constraints}}Your fixed constraints.
{{code}}Your code.
{{language}}Your language.

Check the output before you use it

  • Run the existing tests. A refactor that changes behaviour is a rewrite.
  • Check no new dependency appeared.
  • Look for "improvements" that are actually behaviour changes the model did not flag.

The same prompt for other models

Identical content, packaged the way each model reads most reliably.

ChatGPT

## Role

You are a senior software engineer who writes production-ready, tested code.

## Context

What it does: {{purpose}}. Why I want it refactored: {{motivation}}. Constraints I cannot change: {{fixed_constraints}}. The code: {{code}}.

## Task

Refactor this {{language}} code.

## Constraints

- Behaviour must be identical. If a change alters behaviour, say so instead of making it.
- Explain each change and what it improves.
- Do not introduce dependencies that are not already present.
- Return complete runnable code, not fragments.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.

## Output format

Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.

## Approach

Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.

## Success criteria

Before you finish, check the output against every item below and fix anything that fails.

- Existing tests would pass unchanged against the refactored code.

## Before you start

If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.

Now, refactor this {{language}} code.

Gemini

**Task**

Refactor this {{language}} code.

**Role**

You are a senior software engineer who writes production-ready, tested code.

**Context**

What it does: {{purpose}}. Why I want it refactored: {{motivation}}. Constraints I cannot change: {{fixed_constraints}}. The code: {{code}}.

**Constraints**

- Behaviour must be identical. If a change alters behaviour, say so instead of making it.
- Explain each change and what it improves.
- Do not introduce dependencies that are not already present.
- Return complete runnable code, not fragments.
- Return complete, runnable code. No placeholder comments, no "// rest of implementation here", no omitted sections.
- Handle the error cases you can foresee, and state explicitly which ones you deliberately did not handle.
- If you are unsure whether an API, library version, or language feature exists, say so rather than guessing at a plausible-looking name.

**Output format**

Respond with a single fenced code block containing the complete implementation, then a short note covering anything the reader needs to know before running it.

**Approach**

Work through the problem step by step before giving your answer. Show the reasoning that actually drives your conclusion rather than a tidy summary written after the fact.

**Success criteria**

Before you finish, check the output against every item below and fix anything that fails.

- Existing tests would pass unchanged against the refactored code.

**Before you start**

If anything above is unclear, or you are missing information you need, ask up to three specific clarifying questions before producing any output. Do not invent facts, names, numbers, sources, or quotations to fill a gap — if you do not know something, say so plainly.

Now, refactor this {{language}} code.

Common questions

Is it safe to let AI refactor code?
With tests, yes. Without tests, you have no way of knowing whether behaviour changed, and behaviour changing silently is the exact risk. Write the characterisation tests first if none exist.
Why did the refactor change what my code does?
Because models treat obvious bugs as things to fix while restructuring, unless told not to. Requiring identical behaviour and asking for any proposed behaviour change to be reported separately keeps the two apart.
Which AI model is best for refactoring code?
All of them handle this; what changes is the packaging. This page shows the same prompt written for Claude, ChatGPT, Gemini. Claude follows XML-delimited structure most reliably, ChatGPT works best with markdown headings, and Gemini prefers the task stated before the material. The content of the prompt is identical in each.
Can I change this prompt for my own situation?
Yes, and you should. Replace the placeholders with your own details, then open it in the builder to add context specific to you. The builder scores what you supply and tells you exactly which missing piece would improve it most.
Why does this prompt include rules I did not ask for?
Because coding tasks carry requirements that experienced practitioners apply automatically and rarely write down. The compiler adds them so the output does not fail on something obvious. Every added rule is listed on the how it works page.

Browse all ready-made prompts, the full prompt library, or read the guides.