Skip to main content

Model Artifact: Refactor PR Writeup

Scenario

A Semester 3 learner refactors checkout pricing code that had duplicated discount rules across controller and invoice paths.

Completed learner-quality example

PR title

Extract pricing policy object and preserve checkout behavior

Problem

Discount eligibility was implemented in three places. Two call sites rounded before tax and one rounded after tax, so small carts could produce inconsistent totals.

Scope

This PR performs a behavior-preserving refactor except for one documented bug fix: all checkout paths now round only at the final money boundary. No API endpoints, database columns, or user-visible discount names change.

Changes

  • Introduced PricingPolicy with explicit inputs: customer tier, coupon, line items, and tax region.
  • Replaced controller and invoice duplication with calls to the policy.
  • Added golden tests for five existing receipts before changing implementation.
  • Added one regression test for the rounding inconsistency.

Review guide

Review commit by commit:

  1. Golden tests that capture current behavior.
  2. New policy object with duplicated logic moved, not redesigned.
  3. Call-site replacement.
  4. Rounding regression fix.

Risk and rollback

Risk is concentrated in total calculation. Rollback is safe by reverting the PR because schema and external contracts are unchanged. The deployment plan compares ten production-like receipts in staging before release.

Evidence

  • Unit tests cover standard, coupon, tier, and combined-discount cases.
  • Golden receipt snapshots make accidental text or total drift visible.
  • Manual staging check compared invoice PDF total, checkout total, and order history total.

How to read this example

  • Passing: Explains why the refactor exists and how behavior is protected.
  • Strong: Tells reviewers the intended reading order and separates refactor scope from the bug fix.
  • Portfolio-worthy: Demonstrates risk management, rollback thinking, and evidence across automated and manual checks.