01

Extract Function


Goal

Each function reads as a single named domain step — what it does, not how.

Before the refactoring
function ship(order) {
  if (!order.id) throw new Error('missing id');
  const grand = order.total * 1.1;
  email(order.user, `Total ${grand}`);
}
After the refactoring
function ship(order) {
  validate(order);
  const grand = withTax(order);
  notify(order, grand);
}
Savings

Calling code becomes a sequence of named intentions; bugs concentrate inside the now-named subroutines.

Note

Over-eager extraction can produce a maze of one-line functions; aim for extractions that earn their name with at least one decision or one transformation.