Refactor Commits: Keep Behavior Changes Separate

Refactor Commits: Keep Behavior Changes Separate

One intent per commit: structural and behavior changes answer different review questions. Refactor-first ordering, the mechanical-flag trust contract, and pragmatic thresholds.

A commit should carry one kind of intent: either it preserves behavior while improving structure, or it changes behavior on purpose — never both. Mixed diffs force reviewers to answer two questions at once, leave bisect pointing at a commit that still has to be untangled, and inflate revert blast radius. The discipline is one intent per commit, refactor-first ordering by default, mechanical noise isolated and flagged, and an honest threshold below which the ceremony is not worth it. Other continuous-refactoring practices lean on this one, because separation is what keeps small refactors individually verifiable.

Why mixed diffs fail

Consider the diff that renames a method, tightens its validation logic, and reformats the file it lives in — one commit, one review request. The reviewer now faces two distinct questions braided together: is the new validation behavior correct, and did everything else preserve the old behavior? Neither can be answered cleanly, because every hunk might belong to either intent. The rename hunks hide among the logic hunks; the logic change hides among the formatting churn. Reviewing this diff well takes longer than reviewing its parts would have, and it is at risk of not being reviewed well — of being skimmed, approved on plausibility, and trusted to the tests.

The damage continues downstream of review. Bisect exists to answer "which commit broke this," and it can only answer at commit granularity. When the offending commit changed both structure and behavior, the bisect lands on a commit that still has to be untangled — the investigation continues inside the diff. Revert suffers the same way: rolling back a bad behavior change should not also roll back a perfectly good rename, but in a mixed commit git revert takes both together, and separating them means hand-editing the revert, so a plain revert reintroduces confusion the cleanup had already paid for.

None of this is aesthetic preference. It is the observation that bisect and revert, tools an engineering team relies on daily, operate at commit granularity, that review can too when reviewers read a pull request commit by commit, and that a commit that mixes intents degrades all of them at once.

The separation discipline

The rule is compact: one intent per commit. A commit is behavior-preserving — a refactoring in Martin Fowler's strict sense ("a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior"), verifiable by the question "do the existing tests still pass, unchanged?" — or it is behavior-changing, verifiable by "do the new and updated tests express exactly the intended change?" The two verification questions are different, and that difference is the whole argument: a reviewer handed a commit knows which single question to ask of it.

Kent Beck's Tidy First? is a book-length treatment of tidying, which the book describes as a subset of refactorings, themselves defined there as changes to structure that don't change behavior. Its vocabulary is useful: Beck distinguishes tidyings from behavioral changes, and in his newsletter post "Management: Separate Tidying" (March 15, 2022) he takes up where tidyings belong when work goes through pull requests, noting that he will argue for an alternative to that model later. The book's chapter "Separate Tidying" opens with the same pull-request assumption and sums up that tidyings "go in their own PRs, with as few tidyings per PR as possible." The book also puts the separation at the commit level: "As always, separate the tidying commit from the behavior change commit," in its chapter on explaining variables. And when tidyings and behavior changes have already become tangled together, one of the options Beck lists — at least three, none of them attractive in his words — is to untangle them "into separate PRs, or a sequence of PRs, or a sequence of commits in a single PR," which he calls more polite, though it can be a lot of work. This article applies that separation at the commit level, using tidying for a small structural change: the two are different kinds of work, reviewed differently and risked differently, and best kept in separate commits, because interleaving them is where the hidden cost lives.

Held consistently, the discipline flips review economics. Five small single-intent commits can be easier to review with confidence than one mixed diff of the same total size, because each commit answers its one question quickly: the rename is verifiably just a rename, the extraction is verifiably behavior-preserving, and the one behavior change gets the reviewer's full, undiluted attention. This is also what helps keep a continuous refactoring habit socially sustainable — in-passing improvements are easier to welcome when each arrives as a separately checkable claim.

Ordering the sequence

Separation settles what belongs together; ordering settles what comes first. The default is refactor-first: structural work lands before the behavior change it enables. This is the commit-level expression of Kent Beck's make the change easy, then make the easy change — reshape while behavior is still pinned by the existing tests, then land a feature diff small enough to read in one sitting.

A typical sequence, as the reviewer sees it:

text
1. refactor: extract discount calculation into applyDiscount()
     - behavior-preserving; existing tests pass unchanged
2. refactor: rename ambiguous `total` to `preTaxTotal`
     - mechanical rename; call sites updated, no logic touched
3. feat: apply loyalty discount tier to preTaxTotal
     - the actual change, now a small diff in one obvious place
     - new tests express the new behavior

Commits one and two each answer the did-behavior-change question in seconds. Commit three — the risky one — arrives at its smallest possible size, in code already shaped to receive it.

Refactor-after has its legitimate place too. Cleanups discovered mid-implementation — the confusing name you only noticed while changing the logic — land behind the behavior change, still in their own commits. The ordering matters less than the separation; a trailing tidy-up commit is fine, a mixed one is not.

The remaining question is packaging. Small preparatory commits ride in the same pull request with a line in the description flagging them. A preparation substantial enough to stand alone ships as its own PR and merges first — which shrinks the risky diff that follows and de-risks the plan: if the feature is descoped later, the codebase kept a structural improvement instead of holding a stale branch.

Mechanical noise

Formatting, import ordering, and whitespace churn deserve separate treatment because they are high-volume, low-information hunks for a reviewer. The discipline: isolate them in their own commits, or better, delegate them to automated tooling so they never occupy human attention at all. A hand-review of format noise is review budget spent on nothing.

Mass mechanical changes — a rename touching sixty files, a deprecated-call swap across a module — get one additional convention: the commit message names the rule. "Rename getConfig to loadConfig, all call sites, no logic changes" tells the reviewer to verify the rule once and spot-check instances, instead of reading every hunk as if it might hide judgment. Sixty files of mechanical diff reviewed as mechanical is a quick check; the same diff reviewed defensively is a long hunk-by-hunk read.

The convention carries a trust contract, and it is strict: a commit flagged as mechanical must contain zero semantic edits. The first time a "mechanical rename" quietly includes one improved condition, the convention loses its value — later mechanical commits invite the defensive hunk-by-hunk reading again, and the team pays for one smuggled edit. If a semantic fix is discovered mid-sweep, it moves to its own commit. No exceptions is what keeps the flag meaningful.

The honest costs of strict separation

Slicing takes real effort. Work does not arrive pre-separated: you notice the rename mid-logic-change, the extraction mid-bugfix, and splitting them means re-staging hunks and occasionally untangling edits within a single line. Some pairs genuinely resist clean separation. Modern staging tools make the mechanics cheaper, but deciding what belongs together is judgment no tool removes.

So the discipline deserves a pragmatic threshold rather than ritual enforcement. A one-line rename inside a small feature commit does not warrant a ceremony; flagging it in the PR description is proportionate. The separation earns its cost where the reviewer would otherwise struggle — diffs of real size, changes of real risk, sweeps of real volume. Held at that threshold, one intent per commit is not process overhead; it is a cheap way for a team to keep review, bisect, and revert meaning what they claim to mean.

Project Inquiry