Skip to content

Slide Statements

Code reads best when things that belong together sit together. Slide Statements moves a statement up or down so that related lines become neighbours — a variable declared right where it is first used, a setup line beside the call it prepares. The behaviour does not change; only the order does. Often this is the quiet first step before an Extract Function: you slide the scattered pieces into one contiguous block, then lift the block out in a single clean move.

A variable is declared at the top of a function but not touched until thirty lines later. Two lines that clearly work as a pair are separated by unrelated code. When you try to extract a fragment, you find its ingredients spread across the function, so the extraction would need awkward parameters or would drag along lines that do not belong.

The cure is to slide the related statements together first. Once they are adjacent, the structure of the function becomes obvious and the next refactoring becomes trivial.

Two declarations sit at the top, but each is only used much later. Slide each declaration down to just above its first use, putting each value beside the work that needs it.

// Before
function summarize(order: Order): string {
const discount = order.basePrice * 0.1;
const tax = order.basePrice * 0.07;
logAccess(order.customerId);
const net = order.basePrice - discount;
recordAudit(order.id);
const gross = net + tax;
return `Net ${net}, Gross ${gross}`;
}
// After
function summarize(order: Order): string {
logAccess(order.customerId);
const discount = order.basePrice * 0.1;
const net = order.basePrice - discount;
recordAudit(order.id);
const tax = order.basePrice * 0.07;
const gross = net + tax;
return `Net ${net}, Gross ${gross}`;
}
  1. Pick the statement you want to move and the destination slot.
  2. Check the code it slides over for interference. A slide is safe only if order does not matter between them: the moving statement and every statement it passes must not read a value the other writes, and neither may have a side effect the other depends on.
  3. Move the statement to its new position.
  4. Run your tests. If anything fails, the slide crossed a real dependency — put it back and reconsider.
  5. Repeat for the next statement, sliding one at a time so a failure points at a single move.

Slide statements to put a declaration next to its first use, to group the pieces of a soon-to-be-extracted fragment, or simply to make a paired setup-and-action read together. It is a low-risk, high-clarity move and an ideal warm-up before Extract Function.

The one real hazard is a hidden dependency: an apparently independent line that secretly relies on an earlier side effect (a global flag, shared mutable state, ordering through I/O). Your tests are the guard. When in doubt, slide in tiny steps and run them after each move.

What does Slide Statements change about the code?
Why is Slide Statements often done before Extract Function?
When is sliding a statement past another safe?
What is your main safeguard against an unsafe slide?