Back to Course

Spot the Tax · Card 6 of 20

One class, one reason to change

Why a class that handles payment, inventory, email, analytics, and audit logs becomes the file no one wants to touch.

The code

What will this cost you in six months?

class OrderProcessor
  def process(order)
    return false unless valid?(order)

    charge_card(order)
    update_inventory(order)
    send_confirmation_email(order)
    notify_warehouse(order)
    update_analytics(order)
    log_to_audit(order)

    true
  end
  # ... 200 lines of private methods
end

The problem

This class has six different reasons it might need to change in the future, all rolled into one file. Whenever the payment provider changes, you have to touch this class. Whenever the inventory system changes, same thing. The same goes for emails, the warehouse API, the analytics events, and the audit log format. Every team in the company eventually ends up working in this file at some point, and every change to one piece carries a real risk of accidentally breaking something else.

Take a moment. Before revealing, think about how you'd split this class. What would each new piece be responsible for, and what does OrderProcessor end up doing?