SlideShare a Scribd company logo
1 of 101
Download to read offline
WRITING S.O.L.I.D CODE
RAJEEV N B
RAJEEV N B
2
Developer at ThoughtWorks
Blogs at www.rajeevnb.com
@rbharshetty - Twitter
@rshetty - Github
XP
3
This is me doing XP
4
IN THE BEGINNING
5
YOUR APPLICATION WAS PERFECT
THEN SOMETHING HAPPENED
6
YOUR APPLICATION
CHANGED
7
DESIGN TO THE
RESCUE
8
DESIGN ?
9
It is all about
managing your
DEPENDENCIES
10
DEPENDENCIES
11
Client Dependent on Logger
DEPENDENCIES
12
DEPENDENCY MANAGEMENT
• Controlling interdependencies among various software
entities
• As interdependencies increase, Reusability, Flexibility
and Maintainability of your applications decrease
• Unmanaged dependencies lead to “Dependency Hell”
13
DEPENDENCY HELL
14
SUCCESSFUL DESIGN
• INCREASED COHESION
• DECREASED COUPLING
• SUPPORTS REUSE
• TESTABLE
• FLEXIBLE
15
DESIGN STAMINA
16
SOFTWARE ROTS
17
WHY DOES SOFTWARE ROT ?
• Changing requirements
• Improper Design
• No Continuos Design - Refactoring
• Limited Project Resources
• Complexity
18
ROBERT MARTIN
19
Design Principles and Design Patterns
SYMPTOMS OF ROTTING SOFTWARE
• RIGIDITY
• FRAGILITY
• IMMOBILITY
• VISCOSITY
20
RIGIDITY
21
22
RIGIDITY
• Difficult to change
• Cascading of Changes in Dependent Modules
• Impact of change cannot be predicted
• Thus, It can’t be estimated
• Time and Cost can’t be quantified
23
FRAGILITY
24
25
FRAGILITY
• Break many places when changed
• Errors appear in areas unrelated to changed areas
• Quality is unpredictable
• Development Team Credibility Loss
• Sometimes breakage increases with time
26
IMMOBILITY
27
28
IMMOBILITY
• Inability to reuse Software Components
• Lack of reusable modules
• Desirable parts of the system dependent on
undesirable parts
• Work and risk of extracting greater than cost of
writing it from scratch
29
VISCOSITY
30
31
VISCOSITY
• VISCOSITY OF DESIGN
• VISCOSITY OF ENVIRONMENT
32
VISCOSITY OF DESIGN
• Design Preserving methods are harder than Hacks
• Easy to do Wrong thing but harder to do right thing
• As time progresses, it will become increasingly difficult to
continue developing the application
33
VISCOSITY OF ENVIRONMENT
• Development Environment is slow and inefficient
• Slower feedback
Ex: Longer compile times, Slower builds, Long running tests
34
PREVENTING SOFTWARE ROT
• Refactoring (Continuos Improvement of Design)
• Testing (Unit/Integration/Contract tests)
• Code Reviews
• Documentation
• Design Principles
• Simplicity
35
OO DESIGN
PRINCIPLES
36
S.O.L.I.D
37
Single Responsibility Principle
Open/Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
SINGLE
RESPONSIBILITY
38
39
40
A software entity should
have one and only one
reason to change
BEFORE
41
AFTER
42
Why is it important to separate these two
responsibilities into separate classes ?
• Each responsibility is an axis of change
• Changes in one responsibility may affect another
• Coupling among classes leads to Fragile Design
43
SIGNS ?
44
• Description of class ( and/or )
• Rigidity and Fragility in code
BENEFITS ?
45
• Reduced Coupling
• Separation of Concerns
• Better readability
• Your code evolves cleanly (Maintainable)
OPEN/CLOSED
46
47
48
Software entities should be
closed for modification but
open for extension
OCP
49
Add functionality by adding new code, not
by modifying old code
ABSTRACTION IS THE KEY
50
• Client/Server relationship is open
• Changes in Server cause changes in the
client
ABSTRACTION IS THE KEY
51
• Client/Server relationship is closed
• Abstract Servers close clients to changes in
implementation
BEFORE
52
AFTER
53
SIGNS ?
54
● Switch cases littered all over your application
● Hunt and replace policy
BENEFITS ?
55
• Reusability
• Maintainability
• Flexible design
DESIGN BY CONTRACT
56
• Methods of class define pre-conditions and
post-conditions
• The preconditions must be met for method
to execute
• Upon completion, method guarantees that
the post conditions are met
DESIGN BY CONTRACT
57
LISKOV
SUBSTITUTION
58
59
Sub-types must be
substitutable for their
base- types
When is something
Substitutable ?
60
Requires No Less
Promises No More
61
ELLIPSE/CIRCLE
62
ELLIPSE/CIRCLE
63
● Violation of LSP
● Circle violates the post conditions
● Circle not substitutable for Ellipse
BEFORE
64
AFTER
65
SIGNS ?
66
• Explicit Checking type of a class in the code
• Derived types not substitutable for the base
types
BENEFITS ?
67
• Ability to re-use code increases
• Flexible code
INTERFACE
SEGREGATION
68
69
70
Clients should not be forced
to depend on methods it
does not use
BEFORE
71
AFTER
72
SIGNS ?
73
● Client requiring things it does not need
● Methods littered with “NotImplemented”
exceptions
BENEFITS ?
74
• Lot of small, focused interfaces
• Flexible design
DEPENDENCY
INVERSION
75
76
77
Depend upon Abstractions
not upon Concretions
DIP
78
• High Level Modules should not depend on low
level modules, Both should depend on
Abstractions
• Abstractions should not depend on details.
Details should depend on abstractions.
BEFORE
79
AFTER
80
BEFORE
81
AFTER
82
SIGNS ?
83
● High level modules are harder to reuse
● Changes in the lower level modules directly
affect the higher level modules
ADVANTAGES ?
84
• Reusable modules
• Easier to extend
• Low Coupled code
KENT BECK
85
4 Simple Design Rules
PASSES TESTS
86
87
PASSES TESTS
88
● Unit tests with Red, Green, Refactor cycles
● Tests help communicate Design
REVEALS INTENTION
89
90
REVEALS INTENTION
91
● Self Documenting code
● Meaningful names to software entities
● Clear code
NO DUPLICATION
92
93
NO DUPLICATION
94
● Once and only once
● DRY
FEWEST ELEMENTS
95
96
FEWEST ELEMENTS
97
● No Superfluous parts/elements
● YAGNI
● KISS
● Minimum number of classes/methods
98
WRAPPING UP
99
● SRP - One responsibility per class
● OCP - Extend not modify
● LSP - Derived substitutable for base
● ISP - Split into Focussed interfaces
● DIP - Depend on Abstractions
REFERENCES
100
● Design Principles and Patterns (http://
www.objectmentor.com/resources/articles/
Principles_and_Patterns.pdf)
● Design By Contract - Eiffel (https://www.eiffel.com/values/
design-by-contract/)
● Code from the talk - Java: https://github.com/rShetty/
S.O.L.I.D-Java
● Code from the talk - Ruby: https://github.com/rshetty/
S.O.L.I.D
THANK YOU

More Related Content

Similar to Writing S.O.L.I.D Code

SOLID design principles in Ruby
SOLID design principles in RubySOLID design principles in Ruby
SOLID design principles in Ruby
Anil Wadghule
 

Similar to Writing S.O.L.I.D Code (20)

android principle.pptx
android principle.pptxandroid principle.pptx
android principle.pptx
 
Art of refactoring - Code Smells and Microservices Antipatterns
Art of refactoring - Code Smells and Microservices AntipatternsArt of refactoring - Code Smells and Microservices Antipatterns
Art of refactoring - Code Smells and Microservices Antipatterns
 
SOLID design principles in Ruby
SOLID design principles in RubySOLID design principles in Ruby
SOLID design principles in Ruby
 
Is your code SOLID enough?
 Is your code SOLID enough? Is your code SOLID enough?
Is your code SOLID enough?
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Architecture Principles CodeStock
Architecture Principles CodeStock Architecture Principles CodeStock
Architecture Principles CodeStock
 
Entity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and BeyondEntity Framework: To the Unit of Work Design Pattern and Beyond
Entity Framework: To the Unit of Work Design Pattern and Beyond
 
Bojan Veljanovski - Modular Software Architecture and Design (Code Camp 2016)
Bojan Veljanovski - Modular Software Architecture and Design (Code Camp 2016)Bojan Veljanovski - Modular Software Architecture and Design (Code Camp 2016)
Bojan Veljanovski - Modular Software Architecture and Design (Code Camp 2016)
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelines
 
Refactoring.pdf
Refactoring.pdfRefactoring.pdf
Refactoring.pdf
 
Алексей Веркеенко "Symfony2 & REST API"
Алексей Веркеенко "Symfony2 & REST API" Алексей Веркеенко "Symfony2 & REST API"
Алексей Веркеенко "Symfony2 & REST API"
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
Re ppt1
Re ppt1Re ppt1
Re ppt1
 
Code qualities and practices to achieve them
Code qualities and practices to achieve themCode qualities and practices to achieve them
Code qualities and practices to achieve them
 
Component based development | what, why and how
Component based development | what, why and howComponent based development | what, why and how
Component based development | what, why and how
 
Code refactoring
Code refactoringCode refactoring
Code refactoring
 
Domain Driven Design Ruby Ways - JURNAL 05/10/2017
Domain Driven Design Ruby Ways -  JURNAL 05/10/2017Domain Driven Design Ruby Ways -  JURNAL 05/10/2017
Domain Driven Design Ruby Ways - JURNAL 05/10/2017
 
RSpec & Rails, an introduction
RSpec & Rails, an introductionRSpec & Rails, an introduction
RSpec & Rails, an introduction
 
Writing Testable Code in SharePoint
Writing Testable Code in SharePointWriting Testable Code in SharePoint
Writing Testable Code in SharePoint
 
React. Flux. Redux
React. Flux. ReduxReact. Flux. Redux
React. Flux. Redux
 

More from Rajeev Bharshetty (6)

Production Ready Microservices at Scale
Production Ready Microservices at ScaleProduction Ready Microservices at Scale
Production Ready Microservices at Scale
 
Resiliency in Distributed Systems
Resiliency in Distributed SystemsResiliency in Distributed Systems
Resiliency in Distributed Systems
 
Redux - What is this fuss about ?
Redux - What is this fuss about ?Redux - What is this fuss about ?
Redux - What is this fuss about ?
 
FunctionalGeekery-RubyConf
FunctionalGeekery-RubyConfFunctionalGeekery-RubyConf
FunctionalGeekery-RubyConf
 
SPDY
SPDY SPDY
SPDY
 
Queue
QueueQueue
Queue
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Writing S.O.L.I.D Code