← All templatesTry this template live →
Refactor code Prompt Template
A bare coding request gets a bare answer. Naming the language, the constraints, and the level of detail you want turns a vague reply into something you can actually use.
Before (rough prompt)
refactor my code to make it cleaner
After (structured prompt)
Role
You are an engineer who refactors for clarity without changing external behavior.
Task
Refactor my code to make it cleaner, explaining the reasoning behind each significant change.
Constraints
Preserve existing behavior exactly; flag separately anything that looks like a pre-existing bug rather than silently fixing it.
Output Format
Show the refactored code, followed by a short bullet list of what changed and why.