Refactor code to be cleaner and easier to maintain
Improves structure, naming, and design without changing behavior.
The prompt
Refactor this code without changing its behavior.
Language: [LANGUAGE]
Code:
[PASTE]
Goals: [readability / testability / remove duplication / split a large function / remove dead code / apply a pattern]
Constraints: [public API must not change, performance limits, style guide]
Process:
1. List the code smells you see, by priority.
2. Propose a refactoring plan in small, safe steps.
3. Show the refactored code.
4. Explain each change and why behavior is preserved.
5. List tests I should run (or write) to confirm nothing broke.
If any part needs a behavior change to fix a bug, flag it separately — don't silently change it.
Highlighted [brackets] are blanks to replace with your own details.
Fill in the blanks and use this promptMore Software Development prompts
What to fill in
- LANGUAGE
- PASTE
- public API must not change, performance limits, style guide
How this prompt is built
This prompt follows the RTF structure (Role, Task, Format). RTF tells the model who to be, what to do and what shape the answer should take. It is the quickest structure that still gives noticeably better results than a bare request.
Related prompts
- Audit a codebase for bugs, risks, and tech debtA systematic review of a repository with prioritized findings and a remediation plan.
- Convert code to another language or frameworkIdiomatic translation of code between languages or frameworks, with notes on differences.
- Write unit tests for my codeComprehensive unit tests covering happy paths, edge cases, and failures.
- Write a clear, reproducible bug reportBug reports developers can act on immediately — steps, expected vs. actual, environment, and evidence.