Exercise 3: Two Fixes for the Stale Comment — Possible Solution ==================================================================== FIX 1: UPDATE THE COMMENT TO MATCH THE CODE ------------------------------ def calculate_total(items): # returns the sum of item prices, discounted by the loyalty program total = sum(item['price'] for item in items) total = total * 0.9 return total FIX 2: RENAME THE FUNCTION, DROP THE COMMENT ------------------------------ def calculate_discounted_total(items): total = sum(item['price'] for item in items) total = total * 0.9 return total BOTH FIXES VERIFIED CORRECT AND EQUIVALENT ------------------------------ fix 1 (updated comment) result: 135.0 - comment now accurately says discounted fix 2 (renamed function) result: 135.0 - name itself says discounted both fixes produce the same correct behavior: True Both fixes correctly compute 135.0 for the same $150 order, and both now accurately describe what the function does - fix 1 via a corrected comment, fix 2 via a name that states the behavior directly. WHICH FIX SURVIVES A SECOND, FUTURE CHANGE BETTER ------------------------------ Fix 1 relies on a human remembering to update the comment a SECOND time if the function changes again - for example, if the discount rate later becomes tiered by customer type. Nothing about fix 1's own structure makes that more likely to happen than the original failure this chapter already verified (a real 10% discount added with the comment never updated). The comment's own accuracy depends entirely on discipline that has already been shown, in this exact function, to fail once. Fix 2's own name, calculate_discounted_total, would need to be EDITED (not just remembered) if the function stopped applying any discount at all - but as long as it keeps computing SOME kind of discounted total, the name stays accurate regardless of how the discount itself changes (a different rate, a tiered rate, a seasonal rate) - because "discounted total" is a description broad enough to remain true across many actual implementations, without needing a separate maintenance step every time the specific number changes. WHY THIS WORKS AS AN ANSWER ------------------------------ Both fixes are implemented and verified to produce identical, correct results, and the comparison of which one survives a future change better is grounded in a genuine structural difference (a name only needs updating when its own broad claim becomes false, while a comment needs updating every time ANY detail it describes changes) rather than a general preference for one style over the other.