21

Decompose Conditional


Goal

Conditions and their consequents read as named domain decisions: isInSummer(), discountFor(date), etc.

Before the refactoring
if (date < SUMMER_START || date > SUMMER_END) {
  charge = qty * winterRate + winterFee;
} else {
  charge = qty * summerRate;
}
After the refactoring
charge = isSummer(date)
  ? summerCharge(qty)
  : winterCharge(qty);
Savings

The branching logic reads top-to-bottom as a story; bugs concentrate in the named pieces.

Note

Names that aren't crisper than the underlying condition add ceremony — only extract when the named function/variable says something the condition can't.