In Python, a developer refactors this checkout function:
def checkout(items, coupon): total = 0 for item in items: total += item.price if coupon: total -= 5 tax = total * 0.13 return total + tax
into this version:
def apply_coupon(total, coupon): if coupon: total -= 5 def checkout(items, coupon): total = 0 for item in items: total += item.price apply_coupon(total, coupon) tax = total * 0.13 return total + tax
Every existing test calls checkout with coupon=False, and all of them pass. Consider a customer whose item prices sum to 40.00 dollars and who uses a coupon. Ignoring floating-point rounding, which statement is accurate?
It is not a refactoring: with a coupon the new version returns 45.20 rather than 39.55, because the subtraction inside apply_coupon never changes the caller's total.
It is a valid refactoring: both versions return 39.55, because apply_coupon receives total and subtracts the 5.00 discount from it.
It is not a refactoring: with a coupon the new version raises an error, because total is not defined inside the extracted apply_coupon function.
It is a valid refactoring: every existing test still passes, and a passing suite shows that observable behaviour has been preserved for all inputs.
Unlock full course notes by signing up
Renaming a public method safely
A shared library exposes a public method calc() that several other teams' services call. You want to rename it to calculate_invoice_total() while preserving callers' return values, exceptions and side effects. All callers are known, and delegation is assumed to preserve this behaviour.
Follow the library's required rollout policy:
Confirm that the existing tests pass before editing code.
Complexity before and after Extract Method
A cinema prices tickets with the function below. Count each if and elif as one decision point; else counts as none.
A developer applies Extract Method to the age-based pricing:
Give the cyclomatic complexity MMM of the original ticket_price, of the new base_price and of the refactored ticket_price, each as a whole number. Then give the price in dollars, to two decimal places, that the refactored code returns for ticket_price(70, "Tue", True).
Moving the rounding boundary
A store applies a 10% discount to item prices recorded as nonnegative integer cents. A developer proposes the following Python change as a refactoring:
For these nonnegative values, // 10 divides by ten and discards any fractional part. Fill in both functions' returned totals for each basket. Enter exact integers in cents; do not apply any additional rounding.
Removing a temporary changes object sharing
A maintenance team proposes removing a temporary variable from a Python function that creates task queues. Task durations are stored as integer minutes. The proposed change is intended to preserve existing behavior.
The following client code runs once with each version, starting with fresh queues each time:
Calculate morning_total and backup_total for the original version, then calculate both totals for the proposed version. Enter all four results in minutes, to zero decimal places.