Nine Callers Five Flags

Three years ago someone noticed two invoice renderers looked alike and extracted a shared function. Today:

def format_invoice(order, *, tax=True, currency="USD", legacy_header=False,
                   itemize=False, redact_pii=False):
    ...   # 180 lines, roughly 40 of them an if-tree over those five flags

It has nine callers. No two of them pass the same flag combination. A tenth caller now needs a variant whose header rules conflict with what legacy_header=True does.

What do you do?

A

Replace the five booleans with a strategy object passed in by each caller, so the if-tree becomes polymorphic dispatch.

B

Inline format_invoice back into all nine call sites, delete from each copy the branches that caller never takes, then re-extract only what the survivors actually still share.

C

Add a sixth flag and one more branch. It is a two-line change and it keeps a single source of truth for invoice formatting.

D

Turn it into a base class with nine subclasses, one per caller, each overriding only the parts it changes.