SOLID principles on practice
Startup -> Legacy
Spending time diagramm
Extending
Not breaking something else
Easier to read
- Single Responsibility - There should never be more than one reason for a class to change
- Open/Closed - Software entities should be open for extension, but closed for modification
- Liskov Substitution - Functions that use references to base classes must be able to use objects of derived classes without knowing it.
- Interface Segregation - Many client-specific interfaces are better than one general-purpose interface.
- Dependency Inversion - Depend upon abstractions, not concretions.
SOLID principles are principles - not rules
Avoid overfragmenting your code for sake Single Responsibility or other SOLID.
Don't try to achieve SOLID, use SOLID to achieve maintainability.
-
Single Responsibility - There should never be more than one reason for a class to change
Only one reason to change != class that have only one responsibility
Make things (classes, functions, etc.) responsible for fulfilling one type of role.
i.e. Refactor code responsibilities into separate classes. -
Open/Closed - Software entities should be open for extension, but closed for modification
Be able to add new functionality to existing code easily without modifying existing code.
So you are never break the core of your system.
i.e. Separate similar code into classes. Highlighting abstractions. These can define what subclasses will require and strengthen Principle 1. by separating code duties. -
Liskov Substitution - Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
When a class inherits from another class, the program shouldn't break and you shouldn't need to hack anything to use the subclass.
i.e. Define constructor arguments to keep inheritance flexible. -
Interface Segregation - Many client-specific interfaces are better than one general-purpose interface.
Make interfaces (parent abstract classes) more specific, rather than generic.
i.e. Create more interfaces (classes) if needed and/or provide objects to constructors. -
Dependency Inversion - Depend upon abstractions, not concretions.
Make classes depend on abstract classes rather than non-abstract classes.
i.e. Make classes inherit from abstract classes.