Coding

Refactor Code Prompt Template

Improve code readability and maintainability by applying DRY, single responsibility, and clean naming principles — with a clear explanation of every change.

Last updated

The prompt

ROLE: You are a clean code practitioner and senior engineer who treats code as communication — written for the next developer to read, not just for the machine to execute. CONTEXT: A developer has working code that they want to improve. The code runs, but it's hard to read, has repeated logic, or violates the Single Responsibility Principle. The refactor should make it more maintainable without changing its external behaviour. TASK: Refactor the code below, applying clean code principles. Every change must be explained. RULES: • Do not change the external interface (function signatures, return types) unless explicitly asked — behaviour must be preserved • Apply DRY: extract any logic that appears more than once into a named function or constant • Apply Single Responsibility: if a function does more than one thing, split it • Rename any variable or function whose name doesn't clearly describe its purpose • Add error handling for any unhandled failure modes you spot • For each change: write a brief comment in the code OR a numbered explanation below CONSTRAINTS: Produce the complete refactored code, not just the changed sections. If the code is long (200+ lines), refactor in sections and note what wasn't changed. Language: [LANGUAGE]. EDITABLE VARIABLES: • [CODE] — the code to refactor • [LANGUAGE] — programming language • [CONTEXT] — what this code does and where it's used • [CONSTRAINTS] — anything that cannot change (e.g. "must stay in one file", "cannot use external libraries") OUTPUT FORMAT: ```[language] // [Refactored code with inline change annotations] ``` **Changes made:** 1. [Change description + reason] 2. [Change description + reason] ... **What I didn't change and why:** [Brief note on intentional decisions] QUALITY BAR: A new developer joining the team should be able to understand the refactored code's intent within 2 minutes of reading it, without asking anyone for context.

Replace every [BRACKETED] placeholder with your own details before running it.

How this prompt is structured

Output format

Complete refactored code with annotations + numbered change log + note on intentional non-changes

Why this template works

Requiring the AI to explain what it intentionally didn't change is as important as explaining what it did — it prevents over-engineering and shows the developer where their original code was actually fine.

Pro tips

#1

Include the test suite alongside the code — the AI will refactor more aggressively knowing tests exist as a safety net

#2

Ask for refactoring in two passes: first readability, then performance — mixing the two goals produces worse results

#3

If you disagree with a change, ask 'why did you choose X over Y?' — often reveals a tradeoff you hadn't considered

#4

Specify the team's style guide or lint rules as CONSTRAINTS — otherwise the AI will apply its own defaults which may conflict with your codebase

Need this tailored to you?

Prompt𝙸t𝙸n rewrites any prompt with role, context, task, constraints and format — free.

Try it free →

More coding templates

Code Review

Get a comprehensive AI code review covering bugs, performance issues, security vulnerabilities, best practice violations, and refactoring opportunities with specific line references.

View →

Debug an Error

Diagnose any code error with a structured breakdown: root cause analysis, step-by-step fix, and prevention strategies for the future.

View →

Explain Code Simply

Translate complex code into plain English with line-by-line explanations, real-world analogies, and edge-case analysis for any skill level.

View →

Write Unit Tests

Generate thorough unit test suites covering happy paths, edge cases, and error scenarios. Supports Jest, Pytest, and other frameworks with at least 10 test scenarios.

View →

Related reading

Refactor Legacy CodeWrite a Technical SpecCoding PromptsHow to Use AI for CodingCursorCursor Prompt Generator
Browse all prompt templates