Single Responsibility Principle
A brief overview and example of the Single Responsibility Principle as part of a series on SOLID design principles.
coding
designPrinciples
The ‘S’ in SOLID design principles is for the Single Responsibility Principle. It is also the easiest one, in my opinion. The gist of it is that every part of the system should have only a single responsibility, including the system itself.
Take the below code
On the surface, this looks fine, but those methods’ logic is neither reusable nor easily refactored.
To fix it is simple
This way the code is reusable (it can be called from anywhere in your code) and the logic only needs to be refactored in one place.