In everyday life
A cook rearranges the kitchen so the pans hang by the stove. The meals should taste the same. If the cook also changes a recipe that day, nobody can tell whether a bad dish came from the move or the recipe.
One case
A developer tidies a bank app’s interest code: one 300-line functionA named piece of code that takes inputs, does one job and gives back a result, such as total(cart). becomes five short ones. In the same change, they move one rounding step from the end of the math to the middle. In some months, savers now earn 1 cent less. Nobody notices for half a year, because the change was labeled “cleanup”.
Every case
A refactor is safe only when you can show that behavior stayed the same. That takes tests that check what the code does, run before and after the change. Mix a cleanup with a real change and that proof is gone: a reviewer reading a large diffThe lines a change adds and removes, shown side by side. Reviewers read the diff to see what changed. cannot tell the moved lines from the changed ones. Bugs hide in exactly that mix.
The word
Engineers call this
Change the shape or change the behavior, never both in one step. Run the same tests before and after a refactor, and expect them to pass with no edits.