SlideShare a Scribd company logo
Single Responsibility Principle
By Eyal Golan
About Me
2
Senior Java developer and agile practitioner.
Practicing TDD, clean code. Software craftsmanship
advocate.
Tech lead / Scrum master @ eBay
Team 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
4
• Robert C. Martin – Uncle Bob
• http://en.wikipedia.org/wiki/SOLID_(object-
oriented_design)
5
Single responsibility principle
Open / closed principle
Liskov substitution principle
Interface segregation principle
Dependency inversion principle
SOLID
• Single responsibility principle
• A class should have only a single
responsibility
6
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
11
Single
Responsibility
Principle
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
Which Components?
• Methods
• Classes
• Packages
• Modules
• System
13
14
Why SRP ?
Why SRP?
• Organize the code
15
George A. Miller
Why SRP?
• A place for everything and
everything in its place
16
Why SRP?
• Less fragile code
• Low coupling
• High cohesion
17
Why SRP?
• Easier code changes (Refactoring)
18
Why SRP?
• Easier naming
• The smaller and more focused class, it will
be easier to name
19
Why SRP?
• Maintainability
• Testability and easier debugging
20
21
Recognizing
SRP
Violation
Recognizing By Structure
• Class / method is too long
22
Class:
LOC > 250
Bad
Recognizing By Structure
• Too many dependencies (fields /
parameters)
23
Class
Dependency 1
Dependency 2
Dependency 3 Dependency 4
Dependency 5
Dependency 6
Recognizing By Structure
• Low cohesion
24
Recognizing By Structure
• Description / name needs: “AND”
• Generic name: “EmployeeManager”
25
void calculateAndSend(…)
Recognizing By Structure
• A method with many levels
26
if
while
Recognizing By Behavior
• Class needs to be changed for more
than one behavioral change
27
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
Recognizing By Behavior
• Unable to encapsulate the module
30
31
Develop For
SRP
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
• Testable code
• TDD
33
Test
CodeRefactor
Develop for SRP
• Code quality metrics
• Coverage
• SONAR
34
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!
Develop for SRP
• Naming
• Think about it
• Role play your entities
• Longer and more focused name
37
Develop for SRP
• Extract method
• Extract class
38
Develop for SRP
• Refactor mercilessly
• Use design patterns
• Keep modularization clear
39
Example
40
Precise name
(method, class)
Short class,
35 lines
High cohesion
Conclusion
• OOD
• Clean code
• Better practice
41
Do 1 thing
A 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_nuGNH
EtcU5OekdDMkk/
• PDF about SRP
• http://eyalgo.com/2014/02/01/the-single-
responsibility-principle/
• My blog post about SRP
42
Simple, Isn’t It?
43
Q & A
44
45
Thank You
!
Eyal Golan
egolan74@gmail.com
Connect
@eyalgo_egolan
http://eyalgo.com/

More Related Content

PDF
Git
Mayank Patel
 
PPTX
Gitflow - Branching and Merging Flow for Git
Maulik Shah
 
PPTX
Writing php extensions in golang
do_aki
 
PPTX
Single Responsibility Principle
Eyal Golan
 
PPTX
Git - Basic Crash Course
Nilay Binjola
 
PDF
REST APIs with Spring
Joshua Long
 
PDF
Gitlab - Creating C++ applications with Gitlab CI
Uilian Ries
 
PDF
Git training v10
Skander Hamza
 
Gitflow - Branching and Merging Flow for Git
Maulik Shah
 
Writing php extensions in golang
do_aki
 
Single Responsibility Principle
Eyal Golan
 
Git - Basic Crash Course
Nilay Binjola
 
REST APIs with Spring
Joshua Long
 
Gitlab - Creating C++ applications with Gitlab CI
Uilian Ries
 
Git training v10
Skander Hamza
 

What's hot (20)

PDF
Intro to Git and GitHub
Panagiotis Papadopoulos
 
PPTX
Adobe AEM core components
Lokesh BS
 
PPTX
Intro to git and git hub
Venkat Malladi
 
PDF
Intro to WordPress Plugin Development
Brad Williams
 
PPTX
The Single Responsibility Principle
Lars-Erik Kindblad
 
PDF
Git and git flow
Fran García
 
PPTX
Reactjs
Mallikarjuna G D
 
PDF
InjectionIII의 Hot Reload를 이용하여 앱 개발을 좀 더 편하게 하기.pdf
정민 안
 
PPT
IIS-Settings
Mindfire Solutions
 
PPTX
Mono Repo
Zacky Pickholz
 
PDF
Content Security Policy
Austin Gil
 
PPT
Git workflows presentation
Mack Hardy
 
PDF
Git workflows
Sergiu Savva
 
PPT
hibernate with JPA
Mohammad Faizan
 
PDF
Git and Github slides.pdf
Tilton2
 
PPTX
reactJS
Syam Santhosh
 
PPT
React native
Mohammed El Rafie Tarabay
 
PPTX
Git One Day Training Notes
glen_a_smith
 
PDF
Architecting iOS Project
Massimo Oliviero
 
Intro to Git and GitHub
Panagiotis Papadopoulos
 
Adobe AEM core components
Lokesh BS
 
Intro to git and git hub
Venkat Malladi
 
Intro to WordPress Plugin Development
Brad Williams
 
The Single Responsibility Principle
Lars-Erik Kindblad
 
Git and git flow
Fran García
 
InjectionIII의 Hot Reload를 이용하여 앱 개발을 좀 더 편하게 하기.pdf
정민 안
 
IIS-Settings
Mindfire Solutions
 
Mono Repo
Zacky Pickholz
 
Content Security Policy
Austin Gil
 
Git workflows presentation
Mack Hardy
 
Git workflows
Sergiu Savva
 
hibernate with JPA
Mohammad Faizan
 
Git and Github slides.pdf
Tilton2
 
reactJS
Syam Santhosh
 
Git One Day Training Notes
glen_a_smith
 
Architecting iOS Project
Massimo Oliviero
 
Ad

Similar to Single Responsibility Principle @ Clean Code Alliance Meetup (20)

KEY
"SOLID" Object Oriented Design Principles
Serhiy Oplakanets
 
PPTX
TDD and the Legacy Code Black Hole
Noam Kfir
 
PPTX
SOLID_Principles_Explained_Presentation.pptx
SahanaRV2
 
PPTX
Solid as OOP abstraction
Pawel Klimczyk
 
PPT
principles of object oriented class design
Neetu Mishra
 
PPTX
Improving Software Quality Using Object Oriented Design Principles
Dr. Syed Hassan Amin
 
PPTX
Birth of a developer
Piyush Rahate
 
PPTX
Architectural changes in Orion
Tomas Dermisek
 
PPTX
Clean code
Simon Sönnby
 
PPTX
Improving the Design of Existing Software
Steven Smith
 
PDF
Solid principles
Kumaresh Chandra Baruri
 
PDF
2024 DAPUG Conference Arnaud Bouchez From Classes to Interfaces
Arnaud Bouchez
 
PPTX
Software Design Principles (SOLID)
ASIMYILDIZ
 
PPTX
Introduction to SOLID Principles
Dave Cook
 
KEY
Best practices for writing good automated tests
Felipe Lima
 
PDF
S.O.L.I.D xp
XP Conference India
 
PDF
Writing S.O.L.I.D Code
Rajeev Bharshetty
 
PDF
Object-oriented design principles
Xiaoyan Chen
 
PPTX
Unit test in JavaScript
bmarchal
 
PPTX
Improving the Quality of Existing Software
Steven Smith
 
"SOLID" Object Oriented Design Principles
Serhiy Oplakanets
 
TDD and the Legacy Code Black Hole
Noam Kfir
 
SOLID_Principles_Explained_Presentation.pptx
SahanaRV2
 
Solid as OOP abstraction
Pawel Klimczyk
 
principles of object oriented class design
Neetu Mishra
 
Improving Software Quality Using Object Oriented Design Principles
Dr. Syed Hassan Amin
 
Birth of a developer
Piyush Rahate
 
Architectural changes in Orion
Tomas Dermisek
 
Clean code
Simon Sönnby
 
Improving the Design of Existing Software
Steven Smith
 
Solid principles
Kumaresh Chandra Baruri
 
2024 DAPUG Conference Arnaud Bouchez From Classes to Interfaces
Arnaud Bouchez
 
Software Design Principles (SOLID)
ASIMYILDIZ
 
Introduction to SOLID Principles
Dave Cook
 
Best practices for writing good automated tests
Felipe Lima
 
S.O.L.I.D xp
XP Conference India
 
Writing S.O.L.I.D Code
Rajeev Bharshetty
 
Object-oriented design principles
Xiaoyan Chen
 
Unit test in JavaScript
bmarchal
 
Improving the Quality of Existing Software
Steven Smith
 
Ad

Recently uploaded (20)

PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
WatchTraderHub - Watch Dealer software with inventory management and multi-ch...
WatchDealer Pavel
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Exploring AI Agents in Process Industries
amoreira6
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Presentation about variables and constant.pptx
kr2589474
 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
 

Single Responsibility Principle @ Clean Code Alliance Meetup