Card 09/ 09

RecapDifficulty: Intermediate1 min

Recap: Design Rot and the Five SOLID Principles

Four symptoms, two measures, five principles, and when to leave them out.

Design rot, coupling and cohesion, and the five SOLID principles are the vocabulary the rest of the subject uses.

  • Design rot is code that still works but resists change. Its four symptoms are rigidity (one change spreads), fragility (a change breaks something unrelated), immobility (a part cannot be reused) and viscosity (the hack is easier than the right change).
  • Coupling is how much a class depends on others' details; cohesion is how closely its own parts belong together. Every principle pushes coupling down and cohesion up.
  • SOLID names five principles: single responsibility, open/closed, Liskov substitution, interface segregation and dependency inversion. Martin collected them and Feathers named the acronym.
  • A class diagram shows dependencies with four arrows: inherits, implements, holds and uses.
  • Python, Java and TypeScript all declare interfaces. Java and a Python ABC are nominal, so a class must name the interface; TypeScript and a Python Protocol are structural.
  • The principles cost indirection. Apply them where change is expected, and wait for the second case before abstracting anything else.