SlideShare a Scribd company logo
1 of 52
We want to do our best work.
We want our work to have meaning
We want to have fun along the way
Two major themes being Design
and Messages over objects
Design
Failures of OOD might look like failures of coding
technique but they are actually failures of perspective.
Source: Pablo Picasso - Buffalo Art
Design is not an assembly line where similarly
trained workers construct identical widgets; it’s a
studio where like-minded artists sculpt custom applications.
Design is thus an art, the art of arranging code.
How does Design affect us?
Perspective.
Unfortunately, something will change.
The customers didn’t know what they wanted.
They didn’t say what they meant.
You didn’t understand their needs.
You’ve learned how to do something better.
Even applications that are perfect in every way are not
stable.
The application was a huge success, now everyone wants
more.
Change is unavoidable. It is ubiquitous, omnipresent, and
inevitable.
Source: http://more-sky.com/
Dependency
Object-oriented design is about managing dependencies.
1 class Gear
2 attr_reader :chainring, :cog, :rim, :tire
3 def initialize(chainring, cog, rim, tire)
4 @chainring = chainring
5 @cog = cog
6 @rim = rim
7 @tire = tire
8 end
9
10 def gear_inches
11 ratio * Wheel.new(rim, tire).diameter
12 end
13 # ...
14 end
15
16 Gear.new(52, 11, 26, 1.5).gear_inches
1 class Gear
2 attr_reader :chainring, :cog, :rim, :tire
3 def initialize(chainring, cog, rim, tire)
4 @chainring = chainring
5 @cog = cog
6 @rim = rim
7 @tire = tire
8 end
9
10 def gear_inches
11 ratio * Wheel.new(rim, tire).diameter
12 end
13 # ...
14 end
15
16 Gear.new(52, 11, 26, 1.5).gear_inches
1 class Gear
2 attr_reader :chainring, :cog, :rim, :tire
3 def initialize(chainring, cog, rim, tire)
4 @chainring = chainring
5 @cog = cog
6 @rim = rim
7 @tire = tire
8 end
9
10 def gear_inches
11 ratio * Wheel.new(rim, tire).diameter
12 end
13 # ...
14 end
15
16 Gear.new(52, 11, 26, 1.5).gear_inches
1 class Gear
2 attr_reader :chainring, :cog, :rim, :tire
3 def initialize(chainring, cog, rim, tire)
4 @chainring = chainring
5 @cog = cog
6 @rim = rim
7 @tire = tire
8 end
9
10 def gear_inches
11 ratio * Wheel.new(rim, tire).diameter
12 end
13 # ...
14 end
15
16 Gear.new(52, 11, 26, 1.5).gear_inches
1 class Gear
2 attr_reader :chainring, :cog, :rim, :tire
3 def initialize(chainring, cog, rim, tire)
4 @chainring = chainring
5 @cog = cog
6 @rim = rim
7 @tire = tire
8 end
9
10 def gear_inches
11 ratio * Wheel.new(rim, tire).diameter
12 end
13 # ...
14 end
15
16 Gear.new(52, 11, 26, 1.5).gear_inches
Your goal is to model your application, using classes,
such that it does what it is supposed to do right now
and is also easy to change later.
1 class Gear
2 attr_reader :chainring, :cog, :rim, :tire
3 def initialize(chainring, cog, rim, tire)
4 @chainring = chainring
5 @cog = cog
6 @rim = rim
7 @tire = tire
8 end
9
10 def gear_inches
11 ratio * Wheel.new(rim, tire).diameter
12 end
13 # ...
14 end
15
16 Gear.new(52, 11, 26, 1.5).gear_inches
Dependency injection
1 class Gear
2 attr_reader :chainring, :cog, :wheel
3 def initialize(chainring, cog, wheel)
4 @chainring = chainring
5 @cog = cog
6 @wheel = wheel
8 end
9
10 def gear_inches
11 ratio * wheel.diameter
12 end
13 # ...
14 end
15
16 Gear.new(52, 11, Wheel.new(26, 1.5)).gear_inches
Dependency aversion
1 class Gear
2 attr_reader :chainring, :cog, :wheel
3 def initialize(chainring, cog, rim, tire)
4 @chainring = chainring
5 @cog = cog
6 @wheel = Wheel.new(rim, tire)
8 end
9
10 def gear_inches
11 ratio * wheel.diameter
12 end
13 # ...
14 end
15
16 Gear.new(52, 11, 26, 1.5).gear_inches
Order Dependency
1 class Gear
2 attr_reader :chainring, :cog, :tire
3 def initialize(args)
4 @chainring = args[:chainring]
5 @cog = args[:cog]
6 @wheel = args[:wheel]
7 end
8
9 def gear_inches
10 ratio * wheel.diameter
11 end
12 # ...
13 end
14
15 Gear.new(
16 :cog => 11,
17 :chainring => 52,
18 :wheel => Wheel.new(26, 1.5)).gear_inches
Is your code too coupled to your tests?
Tools for design
Single Responsibility, Open-Closed, Liskov
Substitution, Interface Segregation, and Dependency
Inversion.
DRY (Don’t Repeat Yourself)
Law of Demeter (LoD)
Principles
The so-called Gang of Four (Gof)
Skinny models - Service objects
Patterns
Design is more the art of preserving changeability than it
is the act of achieving perfection
Virtual World
You will never know less than you know right now.
Keep your code TRUE
Transparent The consequences of change should be obvious
in the code that is changing and in distant code that relies upon
it
Reasonable The cost of any change should be proportional to
the benefits the change achieves
Usable Existing code should be usable in new and unexpected
contexts
Exemplary The code itself should encourage those who
change it to perpetuate these qualities
Challenge - https://gitlab.com/LegendaryRob/WizSys
Example
Sandi Metz
“Sandi is the author of Practical Object-
Oriented Design in Ruby, and most recently
99 bottles. She has thirty years of experience
working on large object-oriented applications.
She’s spoken about programming, object-
oriented design and refactoring at numerous
conferences including Agile Alliance Technical
Conference, Craft Conf, Øredev, RailsConf,
and RubyConf. She believes in simple code
and straightforward explanations, and is the
proud recipient of a Ruby Hero award for her
contribution to the Ruby community. She
prefers working software, practical solutions
and lengthy bicycle trips (not necessarily in
that order). Find out more about Sandi at
sandimetz.com.
Why this book?
Design
What are design principles
What are design Patterns
When should I design?
How does one design ?
OO vs FP
Single Responsibility
What belongs to a class?
How do I organize my code?
Why does SR matter?
Code Introspection
How to write change tolerant codeData vs Behavior
When to design and when to replicate?
Dependencies
What is coupling? is it good or bad?
How to inject Dependencies
How to isolate dependencies
Seeing hidden dependencies
Which direction should we interact with them
Interfaces
Public vs Private
Responsibilities of interfaces
Intention
Context independence
Messages over objects
Developing trust
LoD
What is this? why is it important
What happens if I break the law?
How to avoid violations
Duck Typing
Understanding the quacks
Trusting ducks by choosing them wisely
Finding hidden ducks
Documentation
Sharing Code
Static vs Dynamic Typing
Inheritance
Where to use it?
Drawing up relationships
What happens when you misapply inheritance
Creating abstract classes
Template Method Pattern
Superclasses and Subclasses

More Related Content

Similar to OOD - Object orientated design

The Future is Responsive
The Future is ResponsiveThe Future is Responsive
The Future is ResponsiveZURB
 
So…What Do I Make? (Dan Mall)
So…What Do I Make? (Dan Mall)So…What Do I Make? (Dan Mall)
So…What Do I Make? (Dan Mall)Future Insights
 
Design book[1]
Design book[1]Design book[1]
Design book[1]nzende
 
Webinar: Schema Patterns and Your Storage Engine
Webinar: Schema Patterns and Your Storage EngineWebinar: Schema Patterns and Your Storage Engine
Webinar: Schema Patterns and Your Storage EngineMongoDB
 
Design margin analysis & prediction 2005
Design margin analysis & prediction 2005Design margin analysis & prediction 2005
Design margin analysis & prediction 2005Sachin Modgil
 
CM NCCU Class2
CM NCCU Class2CM NCCU Class2
CM NCCU Class2志明 陳
 
Building Applications with a Graph Database
Building Applications with a Graph DatabaseBuilding Applications with a Graph Database
Building Applications with a Graph DatabaseTobias Lindaaker
 
React Native Workshop - React Alicante
React Native Workshop - React AlicanteReact Native Workshop - React Alicante
React Native Workshop - React AlicanteIgnacio Martín
 
3D Presentation AU 2014 (abridged)
3D Presentation AU 2014 (abridged)3D Presentation AU 2014 (abridged)
3D Presentation AU 2014 (abridged)William Work
 
The State of Front-end At CrowdTwist
The State of Front-end At CrowdTwistThe State of Front-end At CrowdTwist
The State of Front-end At CrowdTwistMark Fayngersh
 
Drupal Theming with CSS Frameworks (960grid)
Drupal Theming with CSS Frameworks (960grid)Drupal Theming with CSS Frameworks (960grid)
Drupal Theming with CSS Frameworks (960grid)Ryan Cross
 
Let's Work Together
Let's Work TogetherLet's Work Together
Let's Work TogetherAquent
 
Ropossum: A Game That Generates Itself
Ropossum: A Game That Generates ItselfRopossum: A Game That Generates Itself
Ropossum: A Game That Generates ItselfMohammad Shaker
 
By Thoughtworks | Reviving the art of software design with Andy Marks and Pam...
By Thoughtworks | Reviving the art of software design with Andy Marks and Pam...By Thoughtworks | Reviving the art of software design with Andy Marks and Pam...
By Thoughtworks | Reviving the art of software design with Andy Marks and Pam...IngridBuenaventura
 
Introduction to React Native Workshop
Introduction to React Native WorkshopIntroduction to React Native Workshop
Introduction to React Native WorkshopIgnacio Martín
 
Intro To Django
Intro To DjangoIntro To Django
Intro To DjangoUdi Bauman
 

Similar to OOD - Object orientated design (20)

10 Ways To Improve Your Code
10 Ways To Improve Your Code10 Ways To Improve Your Code
10 Ways To Improve Your Code
 
The Future is Responsive
The Future is ResponsiveThe Future is Responsive
The Future is Responsive
 
So…What Do I Make? (Dan Mall)
So…What Do I Make? (Dan Mall)So…What Do I Make? (Dan Mall)
So…What Do I Make? (Dan Mall)
 
Design book[1]
Design book[1]Design book[1]
Design book[1]
 
Webinar: Schema Patterns and Your Storage Engine
Webinar: Schema Patterns and Your Storage EngineWebinar: Schema Patterns and Your Storage Engine
Webinar: Schema Patterns and Your Storage Engine
 
Design margin analysis & prediction 2005
Design margin analysis & prediction 2005Design margin analysis & prediction 2005
Design margin analysis & prediction 2005
 
CM NCCU Class2
CM NCCU Class2CM NCCU Class2
CM NCCU Class2
 
Lec18
Lec18Lec18
Lec18
 
Building Applications with a Graph Database
Building Applications with a Graph DatabaseBuilding Applications with a Graph Database
Building Applications with a Graph Database
 
React Native Workshop - React Alicante
React Native Workshop - React AlicanteReact Native Workshop - React Alicante
React Native Workshop - React Alicante
 
3D Presentation AU 2014 (abridged)
3D Presentation AU 2014 (abridged)3D Presentation AU 2014 (abridged)
3D Presentation AU 2014 (abridged)
 
The State of Front-end At CrowdTwist
The State of Front-end At CrowdTwistThe State of Front-end At CrowdTwist
The State of Front-end At CrowdTwist
 
Stem Education - Wheel and Axle
Stem Education - Wheel and AxleStem Education - Wheel and Axle
Stem Education - Wheel and Axle
 
Drupal Theming with CSS Frameworks (960grid)
Drupal Theming with CSS Frameworks (960grid)Drupal Theming with CSS Frameworks (960grid)
Drupal Theming with CSS Frameworks (960grid)
 
Let's Work Together
Let's Work TogetherLet's Work Together
Let's Work Together
 
Ropossum: A Game That Generates Itself
Ropossum: A Game That Generates ItselfRopossum: A Game That Generates Itself
Ropossum: A Game That Generates Itself
 
Ase01.ppt
Ase01.pptAse01.ppt
Ase01.ppt
 
By Thoughtworks | Reviving the art of software design with Andy Marks and Pam...
By Thoughtworks | Reviving the art of software design with Andy Marks and Pam...By Thoughtworks | Reviving the art of software design with Andy Marks and Pam...
By Thoughtworks | Reviving the art of software design with Andy Marks and Pam...
 
Introduction to React Native Workshop
Introduction to React Native WorkshopIntroduction to React Native Workshop
Introduction to React Native Workshop
 
Intro To Django
Intro To DjangoIntro To Django
Intro To Django
 

Recently uploaded

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Recently uploaded (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

OOD - Object orientated design

  • 1.
  • 2. We want to do our best work.
  • 3. We want our work to have meaning
  • 4. We want to have fun along the way
  • 5. Two major themes being Design and Messages over objects
  • 7. Failures of OOD might look like failures of coding technique but they are actually failures of perspective.
  • 8. Source: Pablo Picasso - Buffalo Art
  • 9. Design is not an assembly line where similarly trained workers construct identical widgets; it’s a studio where like-minded artists sculpt custom applications. Design is thus an art, the art of arranging code.
  • 10. How does Design affect us?
  • 11.
  • 14. The customers didn’t know what they wanted.
  • 15. They didn’t say what they meant.
  • 16. You didn’t understand their needs.
  • 17. You’ve learned how to do something better.
  • 18. Even applications that are perfect in every way are not stable.
  • 19. The application was a huge success, now everyone wants more.
  • 20. Change is unavoidable. It is ubiquitous, omnipresent, and inevitable.
  • 23. Object-oriented design is about managing dependencies.
  • 24. 1 class Gear 2 attr_reader :chainring, :cog, :rim, :tire 3 def initialize(chainring, cog, rim, tire) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel.new(rim, tire).diameter 12 end 13 # ... 14 end 15 16 Gear.new(52, 11, 26, 1.5).gear_inches
  • 25. 1 class Gear 2 attr_reader :chainring, :cog, :rim, :tire 3 def initialize(chainring, cog, rim, tire) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel.new(rim, tire).diameter 12 end 13 # ... 14 end 15 16 Gear.new(52, 11, 26, 1.5).gear_inches
  • 26. 1 class Gear 2 attr_reader :chainring, :cog, :rim, :tire 3 def initialize(chainring, cog, rim, tire) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel.new(rim, tire).diameter 12 end 13 # ... 14 end 15 16 Gear.new(52, 11, 26, 1.5).gear_inches
  • 27. 1 class Gear 2 attr_reader :chainring, :cog, :rim, :tire 3 def initialize(chainring, cog, rim, tire) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel.new(rim, tire).diameter 12 end 13 # ... 14 end 15 16 Gear.new(52, 11, 26, 1.5).gear_inches
  • 28. 1 class Gear 2 attr_reader :chainring, :cog, :rim, :tire 3 def initialize(chainring, cog, rim, tire) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel.new(rim, tire).diameter 12 end 13 # ... 14 end 15 16 Gear.new(52, 11, 26, 1.5).gear_inches
  • 29. Your goal is to model your application, using classes, such that it does what it is supposed to do right now and is also easy to change later.
  • 30. 1 class Gear 2 attr_reader :chainring, :cog, :rim, :tire 3 def initialize(chainring, cog, rim, tire) 4 @chainring = chainring 5 @cog = cog 6 @rim = rim 7 @tire = tire 8 end 9 10 def gear_inches 11 ratio * Wheel.new(rim, tire).diameter 12 end 13 # ... 14 end 15 16 Gear.new(52, 11, 26, 1.5).gear_inches
  • 32. 1 class Gear 2 attr_reader :chainring, :cog, :wheel 3 def initialize(chainring, cog, wheel) 4 @chainring = chainring 5 @cog = cog 6 @wheel = wheel 8 end 9 10 def gear_inches 11 ratio * wheel.diameter 12 end 13 # ... 14 end 15 16 Gear.new(52, 11, Wheel.new(26, 1.5)).gear_inches
  • 34. 1 class Gear 2 attr_reader :chainring, :cog, :wheel 3 def initialize(chainring, cog, rim, tire) 4 @chainring = chainring 5 @cog = cog 6 @wheel = Wheel.new(rim, tire) 8 end 9 10 def gear_inches 11 ratio * wheel.diameter 12 end 13 # ... 14 end 15 16 Gear.new(52, 11, 26, 1.5).gear_inches
  • 36. 1 class Gear 2 attr_reader :chainring, :cog, :tire 3 def initialize(args) 4 @chainring = args[:chainring] 5 @cog = args[:cog] 6 @wheel = args[:wheel] 7 end 8 9 def gear_inches 10 ratio * wheel.diameter 11 end 12 # ... 13 end 14 15 Gear.new( 16 :cog => 11, 17 :chainring => 52, 18 :wheel => Wheel.new(26, 1.5)).gear_inches
  • 37. Is your code too coupled to your tests?
  • 39. Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion. DRY (Don’t Repeat Yourself) Law of Demeter (LoD) Principles
  • 40. The so-called Gang of Four (Gof) Skinny models - Service objects Patterns
  • 41. Design is more the art of preserving changeability than it is the act of achieving perfection
  • 43. You will never know less than you know right now.
  • 45. Transparent The consequences of change should be obvious in the code that is changing and in distant code that relies upon it Reasonable The cost of any change should be proportional to the benefits the change achieves Usable Existing code should be usable in new and unexpected contexts Exemplary The code itself should encourage those who change it to perpetuate these qualities
  • 48.
  • 49.
  • 50. Sandi Metz “Sandi is the author of Practical Object- Oriented Design in Ruby, and most recently 99 bottles. She has thirty years of experience working on large object-oriented applications. She’s spoken about programming, object- oriented design and refactoring at numerous conferences including Agile Alliance Technical Conference, Craft Conf, Øredev, RailsConf, and RubyConf. She believes in simple code and straightforward explanations, and is the proud recipient of a Ruby Hero award for her contribution to the Ruby community. She prefers working software, practical solutions and lengthy bicycle trips (not necessarily in that order). Find out more about Sandi at sandimetz.com.
  • 52. Design What are design principles What are design Patterns When should I design? How does one design ? OO vs FP Single Responsibility What belongs to a class? How do I organize my code? Why does SR matter? Code Introspection How to write change tolerant codeData vs Behavior When to design and when to replicate? Dependencies What is coupling? is it good or bad? How to inject Dependencies How to isolate dependencies Seeing hidden dependencies Which direction should we interact with them Interfaces Public vs Private Responsibilities of interfaces Intention Context independence Messages over objects Developing trust LoD What is this? why is it important What happens if I break the law? How to avoid violations Duck Typing Understanding the quacks Trusting ducks by choosing them wisely Finding hidden ducks Documentation Sharing Code Static vs Dynamic Typing Inheritance Where to use it? Drawing up relationships What happens when you misapply inheritance Creating abstract classes Template Method Pattern Superclasses and Subclasses

Editor's Notes

  1. Lets look at design
  2. Our internal calculators are always running, comparing total amount accomplished to overall effort expended. When the cost of doing work exceeds its value, our efforts feel wasted.
  3. Keep that in mind.. it’s about perspective. 30years of experience within the industry boiled to this one realization.
  4. This is some art I saw in 2006 at the Standard bank exhibition of Pablo Picasso and his works, He went out to show the different styles of art that something that looks simple can be complex within it’s own right.
  5. Art is a funny thing, because it’s also all about perspective
  6. Close your eyes: Imagine writing a new application. Imagine that this application comes equipped with a complete and correct set of requirements. And if you will, imagine one more thing: once written, this application need never change.
  7. How would such a challenge make you feel, at first you may be excited. Would you want to write the fastest code ever? would it need to look nice or make sense? What sort of constraints do we eliminate because we know the app will never change? So what are these “constraints” really?
  8. It always dose. What sort of constraints are necessary for change? But really the question is can something that seems to be holding us back be really whats holding us up.
  9. Knowing this, what do we do? we need to know our enemy!
  10. Object-oriented applications are made up of parts that interact to produce the behavior of the whole. The parts are objects; interactions are embodied in the messages that pass between them. Getting the right message to the correct target object requires that the sender of the message know things about the receiver. This knowledge creates dependencies between the two and these dependencies stand in the way of change.
  11. This code looks simple enough to follow, but we have created many dependancies. • The name of another class. Gear expects a class named Wheel to exist. • The name of a message that it intends to send to someone other than self. Gear expects a Wheel instance to respond to diameter. • The arguments that a message requires. Gear knows that Wheel.new requires a rim and a tire. • The order of those arguments. Gear knows the first argument to Wheel.new should be rim, the second, tire.
  12. 1. The name of another class. Gear expects a class named Wheel to exist.
  13. 2. The name of a message that it intends to send to someone other than self. Gear expects a Wheel instance to respond to diameter.
  14. 3. The arguments that a message requires. Gear knows that Wheel.new requires a rim and a tire.
  15. 4. The order of those arguments. Gear knows the first argument to Wheel.new should be rim, the second, tire.
  16. Anyone can arrange code to make it work right now. Today’s application can be beat into submission by sheer force of will. It’s a standing target at a known range. It is at your mercy. Creating an easy-to-change application, however, is a different matter. Your application needs to work right now just once; it must be easy to change forever. This quality of easy changeability reveals the craft of programming. Achieving it takes knowledge, skill, and a bit of artistic creativity.
  17. These dependencies couple Gear to Wheel. Alternatively, you could say that each coupling creates a dependency. The more Gear knows about Wheel, the more tightly coupled they are. The more tightly coupled two objects are, the more they behave like a single entity. If you make a change to Wheel you may find it necessary to make a change to Gear. If you want to reuse Gear, Wheel comes along for the ride. When you test Gear, you’ll be testing Wheel too.
  18. We have now taken the dependency and injected it into the object via the params. This is good because now Gear can take any object that has a `gear_inches` method and it will work. Free duck typing!
  19. Sometimes doing a direct dependency injection can be tough because of how coupled the code has become. In these instances doing something like this, is second best. It exposes the dependancies right within the initialize block The intent is to explicitly expose the dependency while reducing its reach into your class.
  20. Managing the order dependency is one that is rarely thought of. But as systems become more complex using this design becomes more important. Not only will it be the guide post of your design of the code to come it will make testing a dream because you will isolate the problems much faster.
  21. If making a change to your model breaks all the spec’s within the model or even other spec’s outside of the scope of what you are working on, this will indicate that your models are too closely coupled and that your tests have now become a dependency themselves.
  22. In a class-based OO language like Ruby, methods are defined in classes. The classes you create will affect how you think about your application forever. They define a virtual world, one that constrains the imagination of everyone downstream. You are constructing a box that may be difficult to think outside of.
  23. If your application succeeds many of the decisions you make today will need to be changed later. When that day comes, your ability to successfully make those changes will be determined by your application’s design.
  24. Going with my mindful theme last year, these principles are now my new guide posts in making decisions when writing any code. Now with this in mind Sandi Metz takes years of experience and shows us how to apply these principles into almost every facet of our current work environment (Since we work with OO languages). So Is this book worth it? Yes, yes and yes again.
  25. Each subtopic here could be a full blown talk at a conference. But the book has two main theme’s: