DEV Community

Rocky Warren
Rocky Warren

Posted on • Originally published at rocky.dev on

SOLID Principles to Code By

Uncle Bob's book Agile Software Development, Principles, Patterns, and Practices calls out 5 principles that aid greatly in Test-Driven Development and make programs easier to maintain and extend (definitions complements of Wikipedia):

  1. Single Responsibility (SRP) - Every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.
  2. Open-Closed (OCP) - Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
  3. Liskov Substitution (LSP) - If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may be substituted for objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).
  4. Interface-Segregation (ISP) - No client should be forced to depend on methods it does not use.
  5. Dependency-Inversion (DIP) - High-level modules should not depend on low-level modules. Both should depend on abstractions. Additionally, abstractions should not depend upon details. Details should depend upon abstractions.
    • No variable should hold reference to concrete class
    • No class should derive from concrete class
    • No method should override an implemented method of any of its base classes

Another that isn't mentioned but that I have found helpful is the Law of Demeter (LoD) - An object A can call a method of an object B, but A cannot "reach through" B to access another object C to call a method. Otherwise, A implicitly requires greater knowledge of B's internal structure.

Top comments (0)