Skip to content
AI Skill Index

Plan a refactor before touching anything

Turns a vague urge to clean something up into an ordered, revertible sequence.

Author: Editorial
Sign in to save
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

  1. Move the 5 email methods to notifications.py as functions. OrderService keeps methods of the same name that call them.
  2. Move the 10 persistence methods to repository.py as a class that holds the db handle. OrderService builds one and delegates to it.
  3. Move the 7 stock methods to stock.py. They need the repository, so this comes after step 2.
  4. Pass _cart_cache to the 9 pricing methods as an argument, so they stop reading it from self. Nothing moves yet.
  5. Move the pricing methods to pricing.py.
  6. Delete the delegating methods that nothing outside the class calls. place_order(), cancel_order() and refund() 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.

ShareXLinkedIn

3 of the 7 other prompts filed under Coding.

Coding tools and skills

3 of the 9 tools and 3 of the 7 skills filed under Coding, the same category as this prompt.

Tool

GitHub Copilot

Code completion and chat inside your editor.

Freemium
Tool

Replit

Browser IDE with an agent that builds and deploys a working app.

Freemium
Tool

Sourcegraph Cody

Code assistant that answers using a whole-organisation index.

Freemium
Skill

Accessibility reviewer

Reviews a component against WCAG failures that automated tools miss.

Claude Code, Cursor, Any agent
Skill

API endpoint reviewer

Checks a handler for the auth, validation and error gaps that recur.

Claude Code, Cursor, Any agent
Skill

Test writer

Writes tests that fail for the right reason and refuse to test the framework.

Claude Code, Cursor, Any agent

Coding guides

3 of the 6 guides filed under Coding, the same category as this prompt.

Cookies

We use only the cookies this site needs to work. Optional analytics helps us see which content is worth writing more of — nothing loads until you say yes.

Read the privacy policy