Plan a step-by-step refactor of legacy code with characterization tests first, shippable intermediate states, added type safety and a test checkpoint after each step.
Help me refactor the legacy code below safely. Produce a sequence of small steps, each leaving the code working and shippable, with a test checkpoint after every step. Do not rewrite everything at once. ## Inputs Legacy code: ``` [LEGACY_CODE] ``` Language, version and runtime: [LANGUAGE] How the code is called and deployed: [USAGE] Existing tests and how to run them: [TESTS] Goals (default: improve readability, add type safety, extract reusable functions, apply SOLID principles where they reduce coupling): [GOALS] Constraints (release cadence, behaviour that must not change, files that cannot move): [CONSTRAINTS] ## Instructions 1. Identify the riskiest areas: hidden global state, side effects mixed with logic, implicit type conversions, missing error handling. 2. Step 0 is always a safety net: characterization tests that capture current outputs, including surprising behaviour, before any change. 3. Order steps so each is small, reversible and independently releasable: rename and extract pure functions first, then isolate side effects behind interfaces, then introduce types, then restructure modules. 4. Apply SOLID principles only where they remove a concrete coupling or change risk in this code. 5. Mark any step that changes behaviour and require explicit sign-off for it. ## Return 1. **Step-by-step refactoring plan**: for each step, the goal, the change, the risk and how to roll it back. 2. **Intermediate states**: the code after each significant step. 3. **Test suggestions at each step**: which tests to add or run, and what must still pass. 4. **Final refactored code**. 5. Follow-up work that was intentionally left out. ## Acceptance checks - Characterization tests come before the first code change. - Each intermediate state is runnable. - Behaviour changes are isolated in labeled steps. - No claim that tests passed unless they were run.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Legacy code (JavaScript, ES5, Node 22 runtime):
var TAX = 0.2; var lastTotal;
function process(o, cb) {
var t = 0;
for (var i = 0; i < o.items.length; i++) { t += o.items[i].price * o.items[i].qty; }
if (o.coupon == "SAVE10") t = t - 10;
lastTotal = t + t * TAX;
db.save(o.id, lastTotal, function (err) { if (err) console.log(err); cb(lastTotal); });
}
Usage: called by three Express routes; deployed weekly
Tests: none
Goals: readability, TypeScript types, extract reusable functions, remove global state
Constraints: the callback signature must keep working until all routes migrateA plan whose step 0 adds characterization tests (including a coupon making the subtotal negative and the error path where cb still receives a total after a failed save); step 1 extracts a pure calculateTotal(items, coupon, taxRate); step 2 removes the lastTotal global by returning values; step 3 wraps db.save in a promise-returning function while keeping a callback adapter; step 4 adds TypeScript or JSDoc types; and a labeled behaviour-change step for surfacing save errors instead of only logging them, requiring sign-off. Intermediate code is shown after steps 1 and 3. Illustrative expectation, not a recorded model run.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
Models