A change to payroll should be easy to describe in a review. If the first half of the diff rearranges nested conditions and the second half changes who gets paid, the reviewer has two puzzles to solve at once. A small tidying gives each puzzle its own turn. First make today’s rules legible. Then change the rule.
Kent Beck calls these small structural edits “tidyings” in Tidy First?. The question in the title matters. If moving things around makes the change in front of you easier to understand, do it. If the change is already obvious, there may be nothing to earn by tidying first.
Put the exceptions where you can see them
Imagine a payroll function with separate rates for a former employee, someone on leave, and someone working their usual hours. A guard clause can finish an exceptional case early and leave the ordinary calculation at the bottom.
function getPayAmount(employee) {
if (employee.hasLeft) return departurePay(employee);
if (employee.isOnLeave) return leavePay(employee);
return regularPay(employee);
}
The order carries a business rule. If both flags can be true, departure pay wins. Keep an example with both flags set when flattening the original nesting. Apply the same care when making two branches look alike. In JavaScript, “missing” and “zero” can lead to different outcomes even when both values fail a truthiness check.
Give the reader the missing noun
Suppose a delivery fee disappears when
subtotalCents >= 5000. A name such as
freeDeliveryThresholdCents gives the number both a purpose and
a unit. An explaining variable such as
qualifiesForFreeDelivery gives the expression a role in the
decision. Choose the name that answers the question the next reader would
otherwise have to reconstruct.
The same idea works at a function boundary. If a formatter reads a currency from a global settings object, pass the currency as an explicit parameter. Inspect every caller first. Reading a setting once at startup and reading it at checkout can produce different results when someone changes their preferences.
Arrange the file in reading order
Read a function from the top with one real input in mind. A blank line between validation, calculation, and persistence may be enough to make its stages visible. Move a declaration beside its first useful value when doing so preserves evaluation order. Put an entry point near the top of a module and closely related helpers together so that following one operation takes fewer jumps.
Some files need the opposite of another extraction. If three tiny functions keep handing the same half-finished value to one another, pull the steps into one place. Once the sequence is visible, extract a helper only when you can name the work it completes. A narrow interface often emerges from seeing which values the caller really needs.
Leave evidence for the review
Remove dead code after checking call sites, registrations, and any configuration that might refer to it. Keep a comment when it explains a surprising constraint, such as why rounding happens per line item. A comment that repeats the arithmetic adds another sentence to maintain.
Run the same examples before and after the edit, including an ordinary input and one at the boundary. In the delivery example, try 4,999, 5,000, and 5,001 cents. A tidy diff should let someone verify that all three answers stayed the same before reviewing a new price or policy.