SOLID Principles
- S - Single Responsibility Principle (reading)
- “A class or function should only have one reason to change”
- “Every class should have only one responsibility”
- O - Open Closed Principle (reading)
- “Software entities should be open for extension, but closed for modification”
- Write your code in a way such that when you need to add new functionality, it shouldn’t require changing existing code
- L - Liskov Substitution Principle
- “To build software systems from interchangeable parts, those parts must adhere to a contract that allows those parts to be substituted one for another.”
- In essence, use interfaces and abstract classes
- See also design by contract
- I - Interface Segregation Principle
- "Prevent classes from relying on things that they dont need.”
- D - Dependency Inversion Principle
- “Depend on abstractions, not concretions”
Benefits
- Write code that’s testable
- Write code that’s easy to understand
- Write code where things are where they’re expecting to be
- Write code where classes narrowly do what they were intended to do
- Write code that can be adjusted and extended quickly
- Write code that can be adjusted and extended quickly without producing bugs
- Write code that separates the policy (rules) from the details (implementation)
- Write code that allows for implementations to be swapped out (e.g. swapping out Email APIs, ORMs, web server frameworks)
Resources
- https://khalilstemmler.com/articles/solid-principles/solid-typescript/
- https://en.wikipedia.org/wiki/SOLID
Last update:
August 11, 2023
Created: June 3, 2023
Created: June 3, 2023