Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Single Responsibility Principle - misunderstood...

Single Responsibility Principle - misunderstood and underestimated

"Singe Responsibility Principle - a class should have only one reason to change". This definition has already been formulated in two versions by Robert C. Martin aka Uncle Bob. During reviews and coaching of teams I have observed that this architecture principle is underestimated as much as misunderstood since 2003.

The result is such classes as "UserService" with 5kLOC code.
This talk explains a UseCase and then the application / refactoring with the Single Responsibility Principle. Furthermore the new term "Single Ownership Principle" is introduced.
(Languages german or english)

Avatar for Nodirbek Usmanov

Nodirbek Usmanov

October 12, 2023
Tweet

Other Decks in Technology

Transcript

  1. SINGLE RESPONSIBILITY PRINCIPLE “A class should have only one reason

    to change.” Robert C. Martin,“Agile Software Development: Principles, Patterns, and Practices”, 2002 Nodirbek Usmanov | Befico GmbH
  2. Nodirbek Usmanov | Befico GmbH IS ONE OF THE SIMPLEST

    OF THE PRINCIPLES, AND ONE OF THE HARDEST TO GET
  3. SINGLE RESPONSIBILITY PRINCIPLE (SRP) “In context of SRP, we define

    a responsibility to be a reason for change.” Robert C. Martin,“Agile Software Development: Principles, Patterns, and Practices”, 2002 Nodirbek Usmanov | Befico GmbH
  4. SINGLE RESPONSIBILITY PRINCIPLE “A CLASS SHOULD HAVE ONLY ONE REASON

    TO CHANGE.” - ROBERT C. MARTIN Nodirbek Usmanov | Befico GmbH
  5. CONCLUSION – VIOLATES SRP Nodirbek Usmanov | Befico GmbH •

    The class knows about • business rules • databases • reports and formatting • Responsibilities > 1 • Each of these responsibilities will cause the employee class to use other classes in the system.
  6. RESPONSIBILITY Nodirbek Usmanov | Befico GmbH Let us add a

    new function to the class. How many responsibilities does this class have? + findById - Finds a student by id
  7. RESPONSIBILITY Nodirbek Usmanov | Befico GmbH How many responsibilities does

    this class have? 3 same family as save, both database functions
  8. CONCLUSION – VIOLATES SRP Nodirbek Usmanov | Befico GmbH •

    The class knows about • business rules • databases • reports and formatting • Responsibilities > 1 • Each of these responsibilities will cause the student class to use other classes in the system.
  9. RESPONSIBILITY – SOURCE OF CHANGE • The SRP is about

    users • Who are the users, who add changes to that method? Nodirbek Usmanov | Befico GmbH
  10. RESPONSIBILITY WHO ARE THE PEOPLE, WHO ARE THE SOURCES OF

    CHANGE FOR THE STUDENT CLASS? Nodirbek Usmanov | Befico GmbH
  11. SINGLE RESPONSIBILITY PRINCIPLE “A CLASS SHOULD HAVE ONLY ONE REASON

    TO CHANGE.” - ROBERT C. MARTIN Nodirbek Usmanov | Befico GmbH Focuses the left side – classes!
  12. SINGLE RESPONSIBILITY PRINCIPLE RESPONSIBILITY IS A FAMILY OF FUNCTIONS THAT

    SERVES ONE PARTICULAR ACTOR Nodirbek Usmanov | Befico GmbH Focuses the right side – actors!
  13. Nodirbek Usmanov | Befico GmbH SINGLE RESPONSIBILITY PRINCIPLE “A class

    should have only one reason to change.” Robert C. Martin, 2002 SINGLE OWNERSHIP PRINCIPLE “A class should serve only one owner, who defines the requirements to create or change it.” Nodirbek Usmanov, 2023 REPHRASE
  14. Nodirbek Usmanov | Befico GmbH SINGLE RESPONSIBILITY PRINCIPLE “A class

    should have only one reason to change.” Robert C. Martin, 2002 SINGLE OWNERSHIP PRINCIPLE “A class should serve only one owner, who defines a family of functions (responsibility).” Nodirbek Usmanov, 2023 REPHRASE 2
  15. TAKE AWAY • A responsibility is a family of functions

    that serves one particular actor • Increase the cohesion between things that change for same reasons • Decrease the coupling between those things that change change for different reasons Nodirbek Usmanov | Befico GmbH
  16. LINKS • The Single Responsibility Principle (08 May 2014), by

    Robert C. Martin (Uncle Bob), https://blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html • Agile Software Development: Principles, Patterns, and Practices, 2002, by Robert C. Martin (Uncle Bob) Nodirbek Usmanov | Befico GmbH
  17. THANK YOU Nodirbek Usmanov | Befico GmbH Reviewer Ulugbek Usmanov

    CGI Consulting Deutschland Reviewer Marcus Schroeder flint & sparks GmbH, Berlin Contact me https://linkedin.com/in/nodirbek-usmanov [email protected]