Refactor repeated subtotal logic
Both functions contain the same quantity check and product calculation. Refactor invoice_total to reuse subtotal.
def subtotal(price, quantity):
if quantity > 0:
return price * quantity
return 0
def invoice_total(price, quantity, shipping):
if quantity > 0:
return price * quantity + shipping
return shipping
Keep the existing rule that a nonpositive quantity contributes zero to the subtotal, then add shipping exactly once.
Which refactor removes the duplicated calculation and preserves the current behavior?
Why it matters: Keeping one source of truth for a calculation makes later fixes consistent.