Show in graph

Software → Software Architecture

Modularity

The design quality of dividing software into focused, replaceable parts with clear responsibilities and boundaries.

Motivation

Modularity solves the problem of codebases becoming impossible to understand, change, or test because everything depends on everything else.

Where it fits

Modularity belongs to software design and architecture. It connects directly to cohesion, coupling, encapsulation, and boundaries.

Code example
Bad example
Python
class OrderService:  def checkout(self, order):      # validate, price, save, email, analytics, and payment all mixed together      if not order["items"]:          raise ValueError("empty order")      total = sum(item["price"] for item in order["items"])      print(f"Charging {total}")      print("Saving order")      print("Sending email")
Good example
Python
class OrderService:  def __init__(self, validator, pricer, payment, repository, notifier):      self.validator = validator      self.pricer = pricer      self.payment = payment      self.repository = repository      self.notifier = notifier  def checkout(self, order):      self.validator.validate(order)      total = self.pricer.total(order)      self.payment.charge(total)      self.repository.save(order)      self.notifier.confirm(order)

Common mistakes

  • Splitting code into many files without clear boundaries.
  • Creating modules that still know too much about each other.
  • Optimizing for reuse before the design is understood.