Refactor Code
Refactor a Legacy Function Safely
Restructure messy code in small, verifiable steps without changing observable behaviour.
The Prompt
refactor-legacy-function-safely.md
Act as a refactoring specialist working in a codebase with limited test coverage. Language: {{language}} Code to refactor: ``` {{code}} ``` Constraints (public API, performance, deadlines): {{constraints}} Plan the refactor as a sequence of small, individually shippable steps. For each step give: - The goal of the step - The exact change - How to verify behaviour is unchanged (test, log comparison, or manual check) Then provide: 1. The final refactored code 2. What improved, measured concretely (fewer branches, clearer boundaries, removed duplication) 3. Behaviour that is intentionally preserved, including known quirks 4. Anything I should NOT refactor now, and why Never change public signatures unless I said it is allowed.
Negative prompt
Included when you copy the prompt
Do not change public function signatures unless {{constraints}} allow it. Do not mix behaviour changes (bug fixes, new features) into the refactor. Do not deliver one giant rewrite as a single step. Do not "clean up" unrelated modules in the same pass. Do not silently remove quirks that callers may depend on - call them out as preserved. Do not require a full test suite rewrite before the first safe step.
Fill in your details below - this prompt updates as you type, then copy it.
Fill in your details
0 of 3 filled. Preview updates as you type - then copy the finished prompt (negative prompt included when present).
refactor-legacy-function-safely.md
Prompt preview
Act as a refactoring specialist working in a codebase with limited test coverage. Language: {{language}} Code to refactor: ``` {{code}} ``` Constraints (public API, performance, deadlines): {{constraints}} Plan the refactor as a sequence of small, individually shippable steps. For each step give: - The goal of the step - The exact change - How to verify behaviour is unchanged (test, log comparison, or manual check) Then provide: 1. The final refactored code 2. What improved, measured concretely (fewer branches, clearer boundaries, removed duplication) 3. Behaviour that is intentionally preserved, including known quirks 4. Anything I should NOT refactor now, and why Never change public signatures unless I said it is allowed.
Examples
Example input
Language: JavaScript (Node 18) Code: function formatMoney(amount, currency) { if (currency == "USD") return "$" + (amount / 100).toFixed(2); if (currency == "EUR") return "€" + (amount / 100).toFixed(2); return amount + " " + currency; } Constraints: Public signature formatMoney(amount, currency) must stay; loose == with USD/EUR is a known quirk used by legacy callers passing string amounts; no behaviour change.
What you should get
Step 1 - Extract currency symbol map Goal: Remove duplicated branch bodies without changing comparisons. Change: const SYMBOLS = { USD: "$", EUR: "€" }; keep == checks. Verify: Snapshot outputs for (999,"USD"), (999,"EUR"), (999,"GBP"), (999, 0) if that was historically passed. Step 2 - Shared formatting helper Goal: One place for /100 and toFixed(2). Change: function asMajor(amount) { return (amount / 100).toFixed(2); } Verify: Same snapshot cases. Final code: ```js const SYMBOLS = { USD: "$", EUR: "€" }; function formatMoney(amount, currency) { const symbol = SYMBOLS[currency]; if (symbol) return symbol + (amount / 100).toFixed(2); return amount + " " + currency; } ``` Note: If SYMBOLS[currency] would break loose == for unusual keys, keep explicit == "USD" / == "EUR" to preserve quirk. Improved: 2 duplicated format expressions → 1; branches clearer. Preserved: signature; USD/EUR formatting; fallback "amount currency"; loose equality quirk if kept explicit. Do not refactor now: Localizing with Intl.NumberFormat - that changes output for some locales.
How to get a good result
Done when
Step 1 - Extract currency symbol map Goal: Remove duplicated branch bodies without changing comparisons. Change: const SYMBOLS = { USD: "$", EUR: "€" }; keep == checks. Verify: Snapshot outputs for (999,"USD"), (999,"EUR"), (999,"GBP"), (999, 0) if that was historically passed. Step 2 - Shared formatting helper Goal: One place for /100 and toFixed(2). Change: function asMajor(amount) { return (amount / 100).toFixed(2); } Verify: Same snapshot cases. Final code: ```js const SYMBOLS = { USD: "$", EUR: "€" }; function formatMoney(amount, currency) { const symbol = SYMBOLS[currency]; if (symbol) return symbol + (amount / 100).toFixed(2); return amount + " " + currency; } ``` Note: If SYMBOLS[currency] would break loose == for unusual keys, keep explicit == "USD" / == "EUR" to preserve quirk. Improved: 2 duplicated format expressions → 1; branches clearer. Preserved: signature; USD/EUR formatting; fallback "amount currency"; loose equality quirk if kept explicit. Do not refactor now: Localizing with Intl.NumberFormat - that changes output for some locales.
FAQ
- What do I fill in before I copy this prompt?
- Replace these placeholders with your details: language, code, constraints. Required fields are marked on the page. The prompt text updates as you type.
- Which AI tools is this prompt written for?
- It works best with ChatGPT, Claude, Cursor. Copy the filled prompt and paste it into the tool you already use.
- What should a good result look like?
- Step 1 - Extract currency symbol map Goal: Remove duplicated branch bodies without changing comparisons. Change: const SYMBOLS = { USD: "$", EUR: "€" }; keep == checks. Verify: Snapshot outputs for (999,"USD"), (999,"EUR"), (999,"GBP"), (999, 0) if that was historically passed. Step 2 - Shared formatting helper Goal: One place for /100 and toFixed(2). Change: function asMajor(amount) { return (amount / 100).toFixed(2); } Verify: Same snapshot cases. Final code: ```js const SYMBOLS = { USD: …
Best for
- Developers
- Tech leads
- Consultants
Related Prompts
More prompts like this — same topic and style.


