Upload
eyal-golan
View
369
Download
1
Tags:
Embed Size (px)
Citation preview
About Me
2
Senior Java developer and agile practitioner.Practicing TDD, clean code. Software craftsmanship advocate.
Tech lead / Scrum master @ eBayTeam lead @ StartApp, doing, among other things:● Manages the continuous integration and deployment of the system.● Leading the coding practices.
Engineering team lead @ AppLift (Berlin)
Agenda
1. SOLID
2. SRP overview
3. Why SRP?
4. Recognizing SRP violation
5. Develop for SRP
6. Summarize
3
•Robert C. Martin – Uncle Bob•http://en.wikipedia.org/wiki/SOLID_(object-
oriented_design)5
Single responsibility principleOpen / closed principleLiskov substitution principleInterface segregation principleDependency inversion principle
SOLID
•Open/closed principle•Open for extension, but closed for
modification•Alistair Cockburn: “…Identify points of predicted
variation and create a stable interface around them…”
7
SOLID
•Liskov substitution principle•Replace objects with instances of their
subtypes without altering the correctness of that program
8
Rectangle
Square
SOLID
•Interface segregation principle•Many client-specific interfaces are better
than one general-purpose interface
9
SOLID•Dependency inversion principle•Abstractions should not depend on details•Don’t depend on anything concrete
•Work with interfaces
10
Single Responsibility Principle
•Wikipedia• …the single responsibility principle states that every class
should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility…
•Clean Code•A class or module should have one, and only
one, reason to change
12
Recognizing By Structure
•Too many dependencies (fields / parameters)
23
ClassDependency 1
Dependency 2
Dependency 3
Dependency 4
Dependency 5
Dependency 6
Recognizing By Structure
•Description / name needs: “AND”
•Generic name: “EmployeeManager”
25
void calculateAndSend(…)
Recognizing By Behavior
•Complicated test
28
when(…).then(…); when(…).then(…);
when(…).then(…);
when(…).then(…);when(…).then(…);
when(…).then(…);
Recognizing By Behavior
•Change here, break there
•Test may be broken elsewhere
•The “shotgun effect”
29
Develop for SRP
•Awareness•The state or ability to perceive, to feel, or to
be conscious of events, objects, or sensory patterns
32
Develop for SRP
•Use other principles•High cohesion•Decrease coupling• Interfaces•Real encapsulation
•Law of Demeter
35
Develop for SRP
36
Keep it simple, stupid!
Keep it simple and short!
Keep it simple, short and specific!
Conclusion
•OOD
•Clean code
•Better practice
41
Do 1 thingA class should have one reason to change!
SRP
Resources• http://butunclebob.com/ArticleS.UncleBob.Principles
OfOod• Uncle Bob about SRP
• http://www.codinghorror.com/blog/2007/03/curlys-law-do-one-thing.html• Coding Horror
• https://docs.google.com/file/d/0ByOwmqah_nuGNHEtcU5OekdDMkk/• PDF about SRP
• http://eyalgo.com/2014/02/01/the-single-responsibility-principle/• My blog post about SRP
42