Plan a refactor before touching anything
Turns a vague urge to clean something up into an ordered, revertible sequence.
I want to restructure this and I do not want to break it. Current shape: ``` [PASTE CODE OR DESCRIBE STRUCTURE] ``` What I want instead: [TARGET STRUCTURE] What must keep working throughout: [PUBLIC API / TESTS / CALLERS] Give me: 1. **An ordered sequence of steps**, each of which leaves the code working and tested. If a step cannot be made safe on its own, say so rather than hiding it inside a bigger one. 2. **The riskiest step**, and what to verify immediately after it. 3. **What to do first that is not refactoring** — usually a missing test that pins current behaviour. 4. **The case for not doing this at all**, honestly argued. Do not produce the final code. I want the plan.
Highlighted parts are placeholders. Replace them before you send.
Worked example
The input is invented for this page. The output shows what the prompt asks for with that input.
Example input (invented)
Current shape
orders/service.py, 1,400 lines, one class OrderService with 31 methods:
pricing 9 methods apply_discounts, compute_tax, compute_total ...
stock 7 methods reserve, release, check_availability ...
email 5 methods send_confirmation, send_refund_notice ...
persistence 10 methods save, load, mark_paid ...
Every method reads self.db and self.config.
The pricing methods also read self._cart_cache.
tests/test_orders.py: 44 tests, all of them through OrderService.place_order().
What I want instead: Four modules (pricing, stock, notifications, repository), with OrderService left as a thin class that calls them.
What must keep working throughout: place_order(), cancel_order() and refund() keep their signatures. The checkout API and the nightly reconciliation job both import OrderService. The 44 tests stay green after every step.
Example output
1. Steps, each leaving the 44 tests green
- Move the 5 email methods to
notifications.pyas functions. OrderService keeps methods of the same name that call them. - Move the 10 persistence methods to
repository.pyas a class that holds the db handle. OrderService builds one and delegates to it. - Move the 7 stock methods to
stock.py. They need the repository, so this comes after step 2. - Pass
_cart_cacheto the 9 pricing methods as an argument, so they stop reading it fromself. Nothing moves yet. - Move the pricing methods to
pricing.py. - Delete the delegating methods that nothing outside the class calls.
place_order(),cancel_order()andrefund()stay.
Step 4 cannot be folded into step 5. Do both in one commit and a failing test will not say which change broke it.
2. The riskiest step is step 4. The cache is shared state. A pricing method that used to see a cart updated earlier in the same call may now get a stale copy. Straight after it, place an order with a discount code added mid-checkout and compare the total with the old one.
3. First, and not refactoring. All 44 tests go through place_order(). Add tests that pin compute_total() for a cart with a discount, a cart with two tax rates and an empty cart, and one that pins what refund() writes and sends. Until they exist, nothing checks steps 4 and 5 directly.
4. The case for leaving it. Two callers import one class, before and after. If the file is rarely edited, 1,400 lines cost you scrolling and little else. The split pays only when several people change it in the same weeks and collide. Otherwise write the tests from point 3 and stop.
More Coding prompts
3 of the 7 other prompts filed under Coding.
Review a diff like a senior engineer
Looks for defects and consequences, not style opinions a linter already covers.
Trace a performance problem to its cause
Separates the slow thing from the thing you noticed.
Turn a README into something someone can follow
Rewrites project docs around what a newcomer actually gets stuck on.
Coding tools and skills
3 of the 9 tools and 3 of the 7 skills filed under Coding, the same category as this prompt.
GitHub Copilot
Code completion and chat inside your editor.
Replit
Browser IDE with an agent that builds and deploys a working app.
Sourcegraph Cody
Code assistant that answers using a whole-organisation index.
Accessibility reviewer
Reviews a component against WCAG failures that automated tools miss.
API endpoint reviewer
Checks a handler for the auth, validation and error gaps that recur.
Test writer
Writes tests that fail for the right reason and refuse to test the framework.
Coding guides
3 of the 6 guides filed under Coding, the same category as this prompt.
Claude Code alternatives: agentic coding tools compared
What Claude Code needs from you today, which agents replace it in the terminal, in an editor, on your own models or in the cloud, and which of them read your SKILL.md files.
GitHub Copilot alternatives: AI coding assistants compared
What Copilot Free covers today, which tools on this index replace it in your editor, in the terminal, on your own models or across a large codebase, and the catch of each.
Cursor alternatives: AI code editors and agents compared
What Cursor's plans include today, which tools on this index replace it as an editor, an extension, a terminal agent or a browser builder, and the trade-off of each.