# SOLID - Software Design Principles
Principles
Note that these are Principles not Rules.
Can sometimes lead to overengineered code.
Resources:
- Single Responsibility Principle
- Open/Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inversion
Others
- KISS - Keep it simple stupid
- Premature Optimizations is not needed
- Minimalism - You aren't gonna need it
- Docs - Never trust the documents
- DRY - Don't repeat yourself
- Agile develepment - Get continous feedback from client
# Single Responsibility Principle
- Single Responsibility for class/methods
- Only 1 reason to change class/methods
- Dataflow - Data should flow parent to child.
- Bad ex - 2 parents modifying child could be bad.
|
|
# Open/Closed Principle
- Like loose coupling, black-box.
- Software entities (classes, functions, modules, etc) is open for extension but closed for modification
|
|
# Liskov Substitution Principle
Let
q(x)
be a property provable about objects ofx
of typeT
. Thenq(y)
should be provable for objectsy
of typeS
(WhereS
is a subtype ofT
.)
- Use inheritance only if Child class can be a Substitution for parent class
- Ex:
- A
Square
is aRectangle
but not vice versa. - If
Square
is a subtype ofRectangle
thenq(Rectangle) == q(square)
but it won't since both have different requirements. - So we should use
Shape
- A
|
|
# Interface Segregation Principle
- Divide big interface into multiple small interfaces (which are more specific).
- If a class is not using interface then class should not be forced to Implement that interface.
- JavaScript don;t have interfaces. So it means interactions in js.
- We can use Facade Pattern (A Facade is an object that provides a simplified interface to big code.).
|
|
# Dependency Inversion
- Depend on abstractions(interfaces) instead of concrete classes
- But javascript has not abstractions.
|
|