Refactor Without Behaviour Change
By l i n h · Published · Updated
Plans a refactor in small safe steps and names the tests that must pass before and after, so behaviour is proven unchanged rather than assumed.
Prompt template
## Refactor Without Behaviour Change Plan a refactor that changes structure but not behaviour. - Code to refactor: [textarea: Paste the code or describe the module] - Why it needs to change: [textarea: Duplication or unclear names or hard to test] - Language and test framework: [text: Python with pytest] - Existing tests: [textarea: What is covered today and what is not] - Risk tolerance: [dropdown: Very cautious, Balanced, Move fast] - Size of each step: [number: 1, 20] Start by writing down the behaviour that must stay identical: inputs, outputs, side effects, errors and timing the callers rely on. Then check the existing tests against that list and name the gaps. Before any code moves, specify characterisation tests that pin the current behaviour, including odd cases, and say they must pass on the untouched code first. Break the refactor into steps of the stated size, each one a single named move such as extract function, rename, or inline, with the test run that follows it. Order steps so the code compiles and the tests pass after every one. Finish with a final comparison check and a list of anything that could not be proven, such as concurrency or performance.