Back to guide/Developer Tools

Code Refactoring

Explicitly stating that external behavior must be preserved prevents breaking changes. Requiring a change log with rationale makes the refactoring reviewable and educational.

ai-coding-assistant-promptslanguagerefactoring_goaloriginal_code
Edit View
Prompt
Refactor the following {{language}} code to improve {{refactoring_goal}}.\n\nOriginal code:\n{{original_code}}\n\nConstraints:\n- Maintain the exact same external behavior (inputs and outputs must not change)\n- Do not add new dependencies unless absolutely necessary\n- Follow {{style_guide}} conventions\n\nProvide:\n1. The refactored code\n2. A bullet list of every change made and why\n3. Before/after comparison of any metrics that changed (lines of code, cyclomatic complexity, number of parameters)\n4. Any risks or trade-offs introduced by the refactoring

Variables to customize

{{language}}{{refactoring_goal}}{{original_code}}{{style_guide}}

Why this prompt works

Explicitly stating that external behavior must be preserved prevents breaking changes. Requiring a change log with rationale makes the refactoring reviewable and educational.

Save this prompt to your library

Organize, version, and access your best prompts across ChatGPT, Claude, and Cursor.