Refactoring changes a system's internal structure while holding its externally observable behavior fixed. The defining commitment is a behavior-equivalence invariant: what the system produced across its contract boundary before must hold after, with everything internal free to change.
Imagine cleaning and rearranging the inside of your toy box so it's neater, but the toys you can take out and play with are exactly the same as before. Nobody playing with you notices any change — only the inside got tidier. Refactoring is fixing up the inside of something while keeping what it does on the outside exactly the same.
Rewire, Same Snacks
Refactoring means changing how something is built on the inside while keeping what it does on the outside exactly the same. Think of reorganizing the wiring and shelves inside a vending machine: you can rearrange everything inside, but it still takes the same coins and drops out the same snacks, so customers can't tell anything changed. The rule you must never break is that the outside behavior stays identical. This is different from a redesign (where you're allowed to change what it does), from replacement (where you swap the whole thing out), and from evolution (where the outside changes to fit a new environment). The whole point is to tidy and improve the insides without disturbing anyone who depends on the outside.
Behavior-Preserving Rework
Refactoring is the pattern of changing the internal structure of a system while holding its externally observable behavior fixed. The defining rule is a behavior-equivalence invariant: whatever the system produced before — outputs, obligations, the experience it delivers to a consumer — it must produce after. What's free to change is everything inside the boundary that defines 'external behavior': the parts, their arrangement, their names, the interfaces they show each other, and any performance that doesn't become externally visible. The payoff is that refactoring decouples internal renovation from external transition risk — you can reorganize the insides without renegotiating with the people who depend on the outside, because the contract they care about is held constant. This is what separates it from redesign (the contract is also up for change), replacement (the system itself is swapped out), and evolution under selection (the contract changes to fit an environment). The subtle part: the invariant being held is whatever counts as 'external behavior,' and that line is itself a design choice — naming it precisely is the first and most load-bearing act of any refactoring.
Refactoring is the structural pattern of changing the internal structure of a system while holding its externally observable behavior fixed. The defining commitment is a behavior-equivalence invariant: whatever the system produced before the change — outputs, contractual obligations, regulatory effects, the experience it delivers to a consumer — it must produce after. What is free to change is everything internal to the boundary that defines 'external behavior': the parts, their arrangement, their names, the interfaces they present to each other, and any performance characteristics that fall short of becoming externally visible. The structural payoff is that refactoring decouples internal renovation from external transition risk: a system can be reorganized without renegotiating with downstream consumers, without re-litigating its purposes, and without disrupting whoever depends on it, because the contract those parties care about is held constant. This distinguishes the move from redesign (where the contract is also up for change), from replacement (where the system itself is exchanged for another), and from evolution under selection (where the contract changes in response to an environment). A subtler structural fact governs the whole pattern: the invariant being held is whatever counts as external behavior, and that line is itself a design choice. Refactoring presupposes that some interface or contract is treated as fixed, and where that line is drawn determines what counts as a refactoring versus a breaking change. When the held interface is a public API, internal performance may drift silently and the space of permissible internal change is broad; when the interface includes timing and side effects, as in a real-time system, the permissible space narrows sharply. Naming the invariant precisely is therefore the first and most load-bearing act of any refactoring.
Forces explicit identification of what is held constant (the contract) and what is changed (the internals), separating refactoring, feature change, and replacement — three changes that carry different risk and approval profiles.
Lets a system be continuously renovated without renegotiating its purposes with everyone downstream; the complexity it manages is coordination cost, bypassed entirely when the contract is held fixed.
The held-constant boundary is itself a design choice and the first load-bearing act; the verification mechanism is what makes the invariant cheap to re-check, which is why the renovation paradox bites — the system that most needs refactoring is often the one least able to support it.
A statutory codification consolidates a sprawl of amendments into a clean restatement: section numbering and cross-references are the mutable internals, legal effect is the held-fixed contract, and a corpus of decided cases — built first and re-run against the codified text — is the case-law analogue of a test suite.
Parents (1) — more general patterns this builds on
Refactoringis a kind ofTransformation — The file: 'Refactoring is a transformation, but a BEHAVIOR-PRESERVING one — it is the specific member of the transformation family constrained by the equivalence invariant.' A clean specialization of transformation.
Refactoring is not Refinement because refactoring holds observable behavior exactly equal, whereas refinement makes it more specified or correct — strengthening a guarantee, which is a contract change.
Refactoring is not Transformation in general because a transformation may freely change outputs, whereas refactoring is the behavior-preserving special case constrained by the equivalence invariant.
Refactoring is not Replacement because replacement exchanges the system itself for another, whereas refactoring renovates the internals of the same system behind a held-fixed contract.