SlideShare a Scribd company logo
1 of 91
Cameron Presley
@pcameronpresley
Cameron@TheSoftwareMentor.com
Establishing a SOLID Foundation
An Intro to Software Design
Slides can be found at:
http://blog.TheSoftwareMentor.com/Presentations/#SOLID
@pcameronpresley 2
@pcameronpresley 3
@pcameronpresley
Mnemonic for a set of principles
that allows us to write easier to
maintain code
Concepts aren’t new (late 80’s)
Introduced by Robert C. Martin in
Agile Principles, Patterns, and
Practices
4
@pcameronpresley
Single Responsibility
Open/Closed
Liskov Substitution
Interface Segregation
Dependency Inversion
5
@pcameronpresley
Doing
Too Much!
6
Bad
Abstractions
@pcameronpresley 7
@pcameronpresley 8
@pcameronpresley 9
@pcameronpresley 10
@pcameronpresley 11
@pcameronpresley 12
@pcameronpresley 13
@pcameronpresley 14
@pcameronpresley 15
@pcameronpresley 16
@pcameronpresley 17
@pcameronpresley
A class should have
one reason to change
18
@pcameronpresley 19
@pcameronpresley
Classes can be measured
in hundreds (or thousands)
of LOCs
20
@pcameronpresley
27 public methods
21
@pcameronpresley
Single public method with
54 private methods
22
@pcameronpresley 23
@pcameronpresley 24
@pcameronpresley 25
1. Break large methods
down to smaller methods
2. Move private methods to
new classes
3. Instantiate/use new
classes
@pcameronpresley 26
@pcameronpresley
Classes should depend upon
abstractions
Classes shouldn’t be responsible
for creating their own
dependencies
27
@pcameronpresley 28
@pcameronpresley
Only way to change dependencies is to
change the class
Impossible to write unit tests that cross
system boundaries
Provides another way to determine if a class
is doing too much (32 dependencies)
29
@pcameronpresley 30
@pcameronpresley 31
@pcameronpresley 32
@pcameronpresley
Two Goals
33
1. Make sure the dependency can be passed
2. Make sure the dependency has an
abstraction
@pcameronpresley
First Steps
34
1. If the class is static, make it not static
2. If the class doesn’t have an abstraction,
define one
@pcameronpresley
Final Step
35
1. Inject the dependency using
constructor injection or
method injection
@pcameronpresley 36
@pcameronpresley
Problem with Static Classes
37
Can’t pass them as parameters
Can’t implement an interface or base class
@pcameronpresley
Removing Static Cling
38
Removing the static attribute is ideal, but not
always possible
Using the Wrapper pattern, we can define
another class that is not static to wrap around
the static and then pass that around
@pcameronpresley 39
@pcameronpresley 40
@pcameronpresley
Passing Dependencies
41
Constructor vs Method Injection
“Will this dependency need to change
during the object’s lifetime?”
@pcameronpresley 42
@pcameronpresley 43
@pcameronpresley 44
@pcameronpresley 45
@pcameronpresley
80% of issues come from
these two principles
Remaining principles cover
the additional 20%
46
@pcameronpresley 47
@pcameronpresley 48
@pcameronpresley 49
@pcameronpresley
Classes should be
open for extension,
closed for modification
50
@pcameronpresley
Add new functionality by
adding new classes, not
modifying existing ones
51
@pcameronpresley 52
@pcameronpresley
Modifying a class to add functionality can break
something downstream
OCP forces us to add functionality by creating
new classes and then using an abstraction
Safer to create a new class and inject the
behavior
53
@pcameronpresley
Modifying a class to add functionality can break
something downstream
OCP forces us to add functionality by creating
new classes and then using an abstraction
Safer to create a new class and inject the
behavior
54
@pcameronpresley
Modifying a class to add functionality can break
something downstream
OCP forces us to add functionality by creating
new classes and then using an abstraction
Safer to create a new class and inject the
behavior
55
@pcameronpresley 56
@pcameronpresley 57
1. Refactor to Factory Pattern
2. Original class becomes the
Factory
3. Update clients to use the
Factory instead
@pcameronpresley 58
@pcameronpresley 59
@pcameronpresley 60
@pcameronpresley 61
@pcameronpresley 62
@pcameronpresley
Adding a New Calculator
63
1. Create the class
2. Implement IShippingCalculator interface
3. Implement business rules
4. Update the factory to return the correct
calculator
@pcameronpresley 64
@pcameronpresley 65
@pcameronpresley
New Requirement!
66
@pcameronpresley 67
@pcameronpresley 68
@pcameronpresley 69
@pcameronpresley 70
@pcameronpresley
Type casting to perform logic is a bad abstraction
“Leaky Abstractions”
Can’t lean on the compiler for error finding
71
@pcameronpresley 72
@pcameronpresley
Not all Books are created equally
What if we changed our abstraction?
73
@pcameronpresley 74
@pcameronpresley 75
@pcameronpresley
If two classes are derived from the same
abstraction, then they should be
interchangeable
Should not depend upon the class of the
abstraction to do work
76
@pcameronpresley 77
@pcameronpresley 78
@pcameronpresley 79
@pcameronpresley 80
@pcameronpresley 81
@pcameronpresley
Can find out what “can” be done, not what
“should” be done
Header vs Role Interface
Shape abstractions based on roles
82
@pcameronpresley 83
@pcameronpresley 84
1. One role to track inventory when an order is
made
2. Define an interface with required methods
3. Update InventoryManager so that it implements
the new interface
4. Update client code to use the new interface
@pcameronpresley 85
@pcameronpresley 86
@pcameronpresley 87
@pcameronpresley
@pcameronpresley 89
• What is SOLID?
• Why should our code be SOLID?
• What do the principles stand for?
• How to spot issues? How to fix them?
• Doing Too Much and Bad Abstractions
@pcameronpresley 90
@pcameronpresley
Agile Principles, Patterns, and Practices by Robert C. Martin
Clean Code: A Handbook of Agile Software Craftsmanship by Robert
C. Martin
Working Effectively With Legacy Code by Michael Feathers
Refactoring by Martin Fowler
SOLID Principles of Object Oriented Design by Steve Smith
Role Interfaces by Martin Fowler
https://blog.TheSoftwareMentor.com/presentations/#SOLID
91

More Related Content

What's hot

What's hot (7)

Cje demo
Cje demoCje demo
Cje demo
 
Dot all 2019 | Testing with Craft | Giel Tettelar
Dot all 2019 | Testing with Craft | Giel TettelarDot all 2019 | Testing with Craft | Giel Tettelar
Dot all 2019 | Testing with Craft | Giel Tettelar
 
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015][XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
 
Is Xp still extreme?
Is Xp still extreme?Is Xp still extreme?
Is Xp still extreme?
 
Pair programming demystified
Pair programming demystifiedPair programming demystified
Pair programming demystified
 
OSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be MissingOSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be Missing
 
Product! - The road to production deployment
Product! - The road to production deploymentProduct! - The road to production deployment
Product! - The road to production deployment
 

Similar to Establishing a SOLID Foundation

The Unicorn Project and The Five Ideals (older: see notes for newer version)
The Unicorn Project and The Five Ideals (older: see notes for newer version)The Unicorn Project and The Five Ideals (older: see notes for newer version)
The Unicorn Project and The Five Ideals (older: see notes for newer version)
Gene Kim
 
My Three Ex’s: A Data Science Approach for Applied Machine Learning
My Three Ex’s: A Data Science Approach for Applied Machine LearningMy Three Ex’s: A Data Science Approach for Applied Machine Learning
My Three Ex’s: A Data Science Approach for Applied Machine Learning
Daniel Tunkelang
 

Similar to Establishing a SOLID Foundation (20)

Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHP
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
 
Programming for a better world
Programming for a better worldProgramming for a better world
Programming for a better world
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Mvc
MvcMvc
Mvc
 
Deeplearning for industries | Data to Production
Deeplearning for industries | Data to ProductionDeeplearning for industries | Data to Production
Deeplearning for industries | Data to Production
 
Oop principles
Oop principlesOop principles
Oop principles
 
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
Rails Conf 2014 Concerns, Decorators, Presenters, Service-objects, Helpers, H...
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Quickly Testing Legacy Code - ACCU York April 2019
Quickly Testing Legacy Code - ACCU York April 2019Quickly Testing Legacy Code - ACCU York April 2019
Quickly Testing Legacy Code - ACCU York April 2019
 
Limiting WIP: Doing Less to Do More
Limiting WIP: Doing Less to Do MoreLimiting WIP: Doing Less to Do More
Limiting WIP: Doing Less to Do More
 
Semantic Merge - No fear refactoring
Semantic Merge - No fear refactoringSemantic Merge - No fear refactoring
Semantic Merge - No fear refactoring
 
The Unicorn Project and The Five Ideals (older: see notes for newer version)
The Unicorn Project and The Five Ideals (older: see notes for newer version)The Unicorn Project and The Five Ideals (older: see notes for newer version)
The Unicorn Project and The Five Ideals (older: see notes for newer version)
 
My Three Ex’s: A Data Science Approach for Applied Machine Learning
My Three Ex’s: A Data Science Approach for Applied Machine LearningMy Three Ex’s: A Data Science Approach for Applied Machine Learning
My Three Ex’s: A Data Science Approach for Applied Machine Learning
 
Commonly used design patterns
Commonly used design patternsCommonly used design patterns
Commonly used design patterns
 
Design Patterns- Course for students .pptx
Design Patterns- Course for students  .pptxDesign Patterns- Course for students  .pptx
Design Patterns- Course for students .pptx
 
Master class in modern Java
Master class in modern JavaMaster class in modern Java
Master class in modern Java
 
React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.React hooks Episode #1: An introduction.
React hooks Episode #1: An introduction.
 
Testing GraphQL in Your JavaScript Application: From Zero to Hundred Percent
Testing GraphQL in Your JavaScript Application: From Zero to Hundred PercentTesting GraphQL in Your JavaScript Application: From Zero to Hundred Percent
Testing GraphQL in Your JavaScript Application: From Zero to Hundred Percent
 
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016 Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016
Solving Cross-Cutting Concerns in PHP - DutchPHP Conference 2016
 

More from Cameron Presley

More from Cameron Presley (6)

Taking a Gamble with Functional Domain Modeling
Taking a Gamble with Functional Domain ModelingTaking a Gamble with Functional Domain Modeling
Taking a Gamble with Functional Domain Modeling
 
Functional Programming Through Construction : First Principles
Functional Programming Through Construction : First PrinciplesFunctional Programming Through Construction : First Principles
Functional Programming Through Construction : First Principles
 
How to Have Code Reviews That Developers Actually Want
How to Have Code Reviews That Developers Actually WantHow to Have Code Reviews That Developers Actually Want
How to Have Code Reviews That Developers Actually Want
 
Indy Code - Taking a Gamble With F#: Implementing Blackjack
Indy Code - Taking a Gamble With F#: Implementing BlackjackIndy Code - Taking a Gamble With F#: Implementing Blackjack
Indy Code - Taking a Gamble With F#: Implementing Blackjack
 
How Functional Programming Made Me A Better Developer
How Functional Programming Made Me A Better DeveloperHow Functional Programming Made Me A Better Developer
How Functional Programming Made Me A Better Developer
 
Establishing a SOLID Foundation - An Intro to Software Design
Establishing a SOLID Foundation - An Intro to Software DesignEstablishing a SOLID Foundation - An Intro to Software Design
Establishing a SOLID Foundation - An Intro to Software Design
 

Recently uploaded

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Recently uploaded (20)

ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
Intro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджераIntro in Product Management - Коротко про професію продакт менеджера
Intro in Product Management - Коротко про професію продакт менеджера
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Buy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptxBuy Epson EcoTank L3210 Colour Printer Online.pptx
Buy Epson EcoTank L3210 Colour Printer Online.pptx
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024Top 10 Symfony Development Companies 2024
Top 10 Symfony Development Companies 2024
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 

Establishing a SOLID Foundation

Editor's Notes

  1. What is SOLID? Why Should Our Code Be SOLID? Exploring the SOLID principles Additional Resources
  2. What is SOLID?
  3. Why is this a good idea?
  4. Spend most of your time reading?
  5. Or writing?
  6. Optimize for Time-To-Context (i.e. how long does it take to read code before understanding what it does?)
  7. Who maintains the code once you leave?
  8. Optimize for Time-To-Context (i.e. how long does it take to read code before understanding what it does?)
  9. Extract to methods!
  10. Extract to classes!
  11. How Do We Spot Errors?
  12. How do we fix this?
  13. Optimize for Time-To-Context (i.e. how long does it take to read code before understanding what it does?)
  14. How do we fix this?
  15. Optimize for Time-To-Context (i.e. how long does it take to read code before understanding what it does?)
  16. How do we fix this?
  17. Optimize for Time-To-Context (i.e. how long does it take to read code before understanding what it does?)
  18. How do we fix this?
  19. Optimize for Time-To-Context (i.e. how long does it take to read code before understanding what it does?)
  20. How do we fix this?
  21. Wrapping Things Up