SingleResponsabilityPrinciple, easy example

A class should have one responability

But then what is a responsability. I have seen examples of code which had a lot of responsabilities. And when the developer was asked what do you think this class is responsible for the answer was: he responsible for a almost everything. Which can be one thing if you have a debat about it. But according uncle bobUncle Bob, software design a responsability is a reason to change the class. So a class should only have one reason to change. The most used example for explaining the one reason to change is validation. Before you start some actions on a ojbect this object must be validated. For instance you do something with a Customer class, like sending email or something, before you do that you need to validate the customer. The validation of that object is done most of the time in the same class but in that case it's violating the SingleResponsabilityPrinciple.

Single responsability principle advantages

  • Easier to maintain
  • Easier to extend
  • Easier to test
  • Reduce complexity
  • Increase reusability
  • Reduced coupling

Code smells for single responsibility are:

  • Class descriptions wich contain AND
  • Method names which doesnt match class name




Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!