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.


Python
def ticket_price(age, day, member):
    if age < 0:
        raise ValueError("age must be non-negative")
    if age < 12:
        price = 8.00
    elif age >= 65:
        price = 9.00
    else:
        price = 14.00
    if day == "Tue":
        price = price - 3.00
    if member:
        price = price * 0.9
    return round(price, 2)


A developer applies Extract Method to the age-based pricing:


Python
def base_price(age):
    if age < 12:
        return 8.00
    elif age >= 65:
        return 9.00
    return 14.00

def ticket_price(age, day, member):
    if age < 0:
        raise ValueError("age must be non-negative")
    price = base_price(age)
    if day == "Tue":
        price = price - 3.00
    if member:
        price = price * 0.9
    return round(price, 2)


Give the cyclomatic complexity MM 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).

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Refactoring Questions: