Skip to content

Lesson 8 of 9 · 23m

Refactoring Toward Patterns When the Pressure Is Real

Introduce adapters, strategies, commands, and factories only after duplication, branching, or coupling demonstrates the need.

The right time to introduce a pattern is when the pressure is visible in the code, not when you suspect it might someday appear. This is the difference between design and speculation. Wait for the second or third example; the first instance often teaches you the wrong abstraction.

Conditionals that keep growing toward strategy

Suppose pricing logic starts small:

def price(order):
    if order.customer.kind == "enterprise":
        return enterprise_price(order)
    if order.coupon:
        return coupon_price(order)
    return standard_price(order)

That is fine. Trouble begins when every new sales motion adds another branch and branches interact. Refactor toward strategy when rules are independently testable and selection is explicit.

RULES = [
    EnterprisePricing(),
    CouponPricing(),
    StandardPricing(),
]

def price(order):
    for rule in RULES:
        if rule.applies(order):
            return rule.price(order)
    raise UnpricedOrder(order.id)

The gain is not pattern compliance. It is isolated tests, explicit ordering, and one place to reason about precedence.

Vendor details leaking toward adapter

If vendor types, error codes, and retry behavior leak across the domain, introduce an adapter at the edge. Do not build the adapter before integration because early assumptions are usually wrong. Integrate once, observe where translation pain accumulates, then tighten the boundary.

Good signs the pressure is real:

  • business logic catches vendor-specific exceptions
  • tests must construct elaborate vendor payloads to exercise domain behavior
  • replacing the vendor would require edits outside the integration module

Operational requirements growing toward command

When a user action becomes retryable, auditable, queueable, or undoable, direct method calls strain under the operational requirements.

interface Command {
  id: string;
  type: "RotateVaultKey" | "ExportUserData" | "SuspendTenant";
  payload: unknown;
  requestedBy: string;
  requestedAt: string;
}

A command object can be persisted before execution, retried after failure, logged with an audit trail, and authorized independently. Introduce it when those properties are required — not merely because CQRS is fashionable.

Construction branching spreading toward factory

When every call site repeats the same environment checks or default policy, centralize construction. Wait until the branching appears in at least two places; duplication is evidence. The first occurrence may belong inside a constructor or simple helper.

Preserve behavior while reshaping

The sequence matters: add characterization coverage, extract the seam, migrate consumers one at a time, then delete the old path. Never leave three parallel mechanisms unless migration is explicitly staged.

Field notes

  • The second implementation teaches more than the first abstraction attempt.
  • Introduce the narrowest pattern that answers the pressure; do not install the entire architectural style.
  • A successful refactor reduces the number of concepts a caller must know, even if it increases the number of types internally.

Was this lesson helpful?

Sign in to rate