When a Reminder Change Reaches the Receipt Renderer
You are reviewing a payment-reminder change. The copy looks reasonable. Then the diff adds another boolean argument to a helper that also generates payment receipts. Receipt callers must now pass a value that means, effectively, “keep doing what you already did.”
That is the moment to inspect the boundary:
- Consolidate a genuinely shared rule when every caller must follow the same decision.
- Tolerate similar structure when receipt and reminder requirements change independently.
- Reconsider the helper when callers must understand each other’s exceptions to preserve their own behavior.
DRY, or Don’t Repeat Yourself, becomes expensive when reviewers treat deleted lines as the result. A shorter diff can leave a larger testing obligation. A common function can turn a reminder edit into a receipt review.
Judge the extraction by what changes together, which behaviors need testing, and who owns the decision. The line count can wait.
Shared Billing Knowledge Carries Different Obligations From Shared Markup
Should receipt and reminder emails share code because both display an invoice amount?
Start with what each message means. A receipt confirms a completed payment. A reminder requests action concerning an outstanding invoice. They share billing vocabulary, but their triggers and permitted actions differ.
Repeated knowledge creates a synchronization obligation. If several modules independently decide whether an invoice remains payable, a policy change requires every implementation to agree. Leaving competing versions scattered through templates invites inconsistent decisions.
Repeated syntax creates a different problem. Similar HTML around an amount may simply reflect similar presentation. Changing the reminder’s introduction need not change the receipt’s confirmation text.
Ask a precise question: should this requirement change necessarily affect every caller? A payable-invoice rule can justify a shared policy function. Matching paragraph tags offer much weaker evidence.
Mechanical formatting also deserves its own boundary. A function such as format_money(amount_cents, currency, locale) can accept explicit values and return display text without knowing which workflow called it. Its callers share formatting behavior while retaining control over their messages.
Explicit Email Functions Versus a Renderer Full of Switches
Consider this hypothetical Python example. The excerpts return plain text to expose the control flow; HTML escaping, delivery, and framework setup sit outside the comparison. Assume format_money performs only monetary formatting.
Receipt and reminder inputs stay explicit
def build_receipt_email(amount_cents, currency, locale, paid_at):
amount = format_money(amount_cents, currency, locale)
return f"Payment received: {amount}\nPaid at: {paid_at}"
def build_reminder_email(amount_cents, currency, locale, payment_url):
amount = format_money(amount_cents, currency, locale)
return f"Payment due: {amount}\nPay: {payment_url}"
These functions repeat an assignment and some string construction. Their interfaces also describe meaningful events: the receipt needs a payment timestamp; the reminder needs a payment destination. Neither caller configures the other workflow.
This version already uses a narrow shared primitive. Keeping the formatting implementation local would produce more duplication without changing the separation between workflows.
The shared renderer accepts workflow decisions
def render_billing_email(
amount_cents, currency, locale, *,
is_reminder, include_payment_link, show_paid_at,
payment_url=None, paid_at=None,
):
amount = format_money(amount_cents, currency, locale)
heading = "Payment due" if is_reminder else "Payment received"
lines = [f"{heading}: {amount}"]
if show_paid_at:
lines.append(f"Paid at: {paid_at}")
if include_payment_link:
lines.append(f"Pay: {payment_url}")
return "\n".join(lines)
The shared implementation has a legitimate advantage: common presentation changes have a single implementation point. Formatting stays consistent. If every billing message must acquire the same wrapper, the renderer offers an obvious place to apply it.
The cost appears in its accepted states. A caller can request a paid timestamp and an active payment link together. It can select reminder wording while omitting the payment destination. Each additional switch expands the interface’s vocabulary beyond the domain inputs.
Flags Expose Coupling
A boolean parameter deserves scrutiny when it makes a receipt caller configure reminder behavior. Renaming the boolean to an enum leaves the underlying boundary question intact.
Test the Abstraction Against the Next Billing Change
Write down a plausible receipt requirement and a plausible reminder requirement before approving the extraction. Trace each through the proposed interface. This exposes coordination costs that a tidy function body can hide.
| Change or responsibility | Local duplication | Shared workflow helper | Separate workflows with a narrow primitive |
|---|---|---|---|
| Reminder-only copy | Edit and test the reminder function. | Edit a branch; inspect receipt behavior for unintended effects. | Edit and test the reminder function. |
| Common amount formatting | Update each local formatter and check consistency. | Update the shared formatting path and verify callers. | Update the formatting primitive and check its contract and integrations. |
| Payment-policy change | Repeated policy decisions require coordinated edits. | Centralization helps only if the helper owns the actual policy. | Keep the rule in a shared policy function, separate from formatting. |
| Test scope | Workflow tests stay local; repeated rules need consistency checks. | Cover meaningful flag combinations and affected caller behavior. | Test workflow behavior separately from the primitive’s contract. |
| Modification ownership | Workflow owners control local edits and coordinate repeated rules. | Shared decisions require agreement across affected callers. | Workflow owners control composition; primitive owners control its contract. |
A shared helper concentrates edits. It can also expand the set of callers that need review. Putting both workflows in the same file does not remove that obligation.
Conversely, explicit workflow functions can live in the same module and deploy together. Splitting a function does not require a new library, repository, or service. There is enough infrastructure already.
Let a Failed-Payment Notice Test the Repeated Pattern
When similar functions first appear, extracting their common shape feels productive. Waiting lets actual requirements expose which parts remain stable.
The rule of three provides that waiting heuristic: a third occurrence prompts inspection, not automatic extraction. Apply it to a failed-payment notice arriving alongside the receipt and reminder.
- Receipt: a completed payment triggers the message; the inputs include the paid amount and timestamp; the message confirms completion.
- Reminder: an outstanding invoice triggers the request; the inputs include the amount due and payment destination; the message asks for payment.
- Failed-payment notice: a declined payment attempt triggers the message; the inputs include relevant attempt details; the permitted response may involve updating a payment method before retrying.
Compare inputs, trigger, and permitted action before comparing string templates. A shared amount formatter still fits. A single workflow renderer now needs to explain confirmation, collection, and recovery through its controls.
Use this waiting heuristic for uncertain structure; consolidate an established shared payable-invoice rule at its first reuse. Waiting for another copy of known policy merely creates synchronization work.
Once the workflow boundaries are clear, examine smaller repeated pieces. A shared presentation wrapper can earn extraction when callers require the same presentation contract. Keep domain decisions out of that wrapper so its interface remains about presentation.
Separate the Entry Points While Preserving Existing Emails
Start with the callers. Before moving code, record the argument choices each caller actually uses and the behavior those choices produce.
For the example renderer, the receipt selects confirmation wording, displays the paid timestamp, and omits the payment link. The reminder selects payment-request wording, displays the payment link, and omits the paid timestamp. Capture those meaningful scenarios directly.
Lock down observable billing behavior
- Map caller configurations. Identify each call site, its workflow, the values it passes, and any conditions controlling payment actions.
- Add characterization tests. Assert the existing receipt and reminder outputs, including amounts, timestamps, and the presence or absence of payment links.
- Introduce explicit entry points. Create
build_receipt_emailandbuild_reminder_email. Initially, they can delegate to the existing renderer with the established arguments. - Move callers behind those entry points. Remove workflow-flag decisions from calling code while preserving the tested behavior.
- Separate the implementations. Move the relevant branches into each workflow function, retain justified primitives, and delete the unused flag-driven renderer.
Test meaningful domain scenarios. Mechanically exercising every boolean combination can turn accidental interface states into apparent requirements. If payment actions depend on invoice state, capture the actual eligibility conditions in the behavior tests.
Keep Copy Changes Separate
Preserve existing text and behavior during the split. Put new reminder wording, broad renaming, and unrelated cleanup in subsequent changes so reviewers can identify what caused a regression.
The split is complete when callers use explicit workflow entry points, the old helper is gone, and workflow-selecting booleans have disappeared from the new signatures. The resulting functions may contain similar lines. That is an acceptable outcome when those lines serve independently changing requirements.
Make the Next DRY Review Explain Who Changes Together
A useful extraction proposal should answer concrete review questions:
- What exact rule does this helper share?
- Which requirements should change every caller?
- Which caller-specific exceptions enter the interface?
- Who owns the shared decision when callers disagree?
Duplication trades repeated edits for workflow independence. Sharing trades centralized implementation for coordinated change. Choose that trade deliberately, with the receipt and reminder requirements visible.
Before approving the next DRY extraction, write one plausible enhancement for each caller in the review and trace which signatures, branches, and tests each enhancement would change.
Your Thoughts
Share your thoughts.
Join the Discussion