Coding & developmentRefactoringcode
Refactor code without changing its behaviour
Defines a safe refactor with invariants, characterisation tests and rollback checks.
Ready to use
Prompt
Refactor [MODULE OR FILE] to improve [MAINTAINABILITY GOAL] while preserving externally visible behaviour.
Before changing code, identify all public interfaces, side effects, ordering assumptions, data formats, error behaviour and performance-sensitive paths. Write characterisation tests for those invariants. Propose a small sequence of edits, each independently testable. Then implement the refactor, show the relevant diff, run existing and new tests, and compare output on representative inputs. Flag any behaviour that cannot be proven equivalent.
Constraints: [LANGUAGE AND FRAMEWORK], [FILES IN SCOPE], [PERFORMANCE OR COMPATIBILITY REQUIREMENTS]. Do not rename public APIs, change database schema, broaden permissions or introduce dependencies without explicit need. Leave a clear rollback path.