Home/Templates/Refactor Code
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.

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.

Make it specific to you

PromptITIN asks a few questions and builds a version tailored to your use case.

✦ Enhance with AI

How to use this template

1

Copy the template

Click the copy button to grab the full prompt text.

2

Fill in the placeholders

Replace anything in [BRACKETS] with your specific details.

3

Paste into any AI tool

Works with ChatGPT, Claude, Gemini, Cursor, and more.

4

Or enhance with AI

Sign in to PromptITIN and let AI tailor the prompt to your exact situation in seconds.

Why this prompt 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.

Tips for best results

  • Include the test suite alongside the code — the AI will refactor more aggressively knowing tests exist as a safety net
  • Ask for refactoring in two passes: first readability, then performance — mixing the two goals produces worse results
  • If you disagree with a change, ask 'why did you choose X over Y?' — often reveals a tradeoff you hadn't considered
  • 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

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 →
← Browse all 195 templates