Waldemar Muhl Ctrl+P
single-responsibility-principle.md ×

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

p { } u p { } p { } p { } b u / u / u / l b / b / b / i l l l c i L i L i L c o c o c o c g g g l i i i a o c o c o c s i i i s d g d g d g o o o U A e D e M e s c s e s a s e t l i r i h e h l h v e t e U e a r e r s r t e U e e e e s r U e ( s r ) e ( r ) ( )

On the surface, this looks fine, but those methods’ logic is neither reusable nor easily refactored.

To fix it is simple

p { } p { } p { } p { } u p { } u p { } u p { } u p { } p { } p { } b u / b u / b u / b u U u U u M l b / l b / l b / l b s b s b a i l i l i l i l e l e l i c i L c i L c i L c i r i r i l c o c o c o c A c D c e c g c g c g c c e r l i l i l i l t l . a o c a o c a o c a o i o e o M s i s i s i s i v i t i a s d g s d g s d g s d a d i d i o o o t o l U A e U D e M M e U A i D n M U s c s s e s a a s s c o e . a s e t e l i i e t n l D i e r i h r e h l l h r i . e e l r A v e D t e e U e v A t l U ( c a r e e r r s r a c e e s t t t e l U e e e t t U t e h i e e s r e i s e r i v U t e ( U v e U ( s a s i r U s a r s ) ) t e o ( s e t ( e ; i r n U e r e ) r o ( s r ( U ( n U e ) s t s r u e h e s r i r u e ( s s r t ) u e ) h ; s r i e ) s r ) ) ;

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.

main 0 errors Waldemar Muhl