SlideShare una empresa de Scribd logo
1 de 22
Object Oriented
Design In Ruby
Presented By- Swapnil Abnave

Kiprosh, 5 December 2013
CHANGE
Fact: your application is going to
change. How will your application
handle that change?
Your App may be o

o

o

o

Rigid: Making a change somewhere will
break something somewhere else.
Fragile: You can’t predict where that break
will be.

Immobile: It’s hard to change/re-use your
code.
Viscous: It’s easier to do the wrong thing

than to fix things.
Design is all about
dependencies
• If you refer to something, you depend

on it.
• When the things you depend on

change, you must change.
SOLID
●

Single Responsibility

●

Open Closed

●

Liskov Substitution

●

Interface Segregation

●

Dependency Inversion
Single Responsibility

There should never be more than
one reason for a class to change
Open/Closed

A module should be open for
extension but closed for
modification
Dependency Inversion

Depend upon abstractions.
Do not depend upon concretions
IGNORABLE RULES
SOLID principles we can ignore in
ruby:
●

Interface Segregation

●

Liskov Substitution

WHY ?
Interface Segregation
●

Really only a problem for staticallytyped, compiled languages.

“Because we’re in Ruby, we don’t have
this problem! Win!”
●

“Dynamic languages obey this rule
in the most extreme way possible:
duck typing.”
Interface Segregation
• What is it ? - “Many client

specific interfaces are better
than one general purpose
interface”
Liskov Substitution
When you design, don’t break the
contract of the superclass in the
subclass.
DESIGN MIGHT SAVE YOU
To avoid dependencies, your design
should be:
●

Loosely coupled – D – Inject

●

Highly cohesive – SRP

●

Easily composable – Can be changed

●

Context independent – Can be
rearranged
Resistance
Resistance is a resource => Listen to what
the pain is telling you.
Listen to what the code smells are telling
you.
●

●

Embrace the friction.

●

Fix the problem.
If testing seems hard – examine your
design.
Your Checkpoint for Design
When you get to the refactor stage of
red-green-refactor, ask yourself …
●

Is it DRY?

●

Does it have just one responsibility?

●

●

Does everything change at the same
rate?
Does it depend on things that change
"Triangle of Responsibility"
Refactoring
●

●

●

Refactor
Extract - Pull functionality out
where necessary
Inject - Inject that new
dependency into place from which
it was extracted
Act like an idiot
●

●

●

What if I don't know where I want
this refactoring to take me?
That's OK. In fact, that's typical.
"Refactor, not because you know
the abstraction, but because you
want to find it."
Act like an idiot
●

●

"You don't have to know where
you're going to successfully
refactor."

When you see someone's code
and think it's beautiful and you
wonder how they thought of
it, they didn't. They evolved it to
that point.
Dependency Injection
When injecting dependencies into a
class, do so only via arguments to
the
#initialize method
def
intialize(downloader=FTPDownloader.ne
w)
@downloader = downloader
Argument Order
Dependency
When you need to inject a few
dependencies, you can use an options
hash to remove the dependency on the
order of the arguments
def intialize(opts)
@env = opts[:env] || Rails.env

filename = opts[:filename]
end
Dependency is unavoidable
How to assess: "Does each object
depend on things that change less
than it does?”
●

Line up the objects from left to right
Left = lower likelihood of change

Right = higher likelihood of change
●

Only depend on things on your left
Conclude to begin with design
TDD is not enough
DRY is not enough

Design because you expect your
application to succeed(and to
change in the future to come)

Más contenido relacionado

Similar a Object Oriented Design in Ruby

Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopMohammad Shawahneh
 
Evgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shapeEvgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shapeAnna Shymchenko
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#Aditya Kumar Rajan
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo designConfiz
 
Developing solid applications
Developing solid applicationsDeveloping solid applications
Developing solid applicationsNilesh Bangar
 
Design for Testability
Design for Testability Design for Testability
Design for Testability Pawel Kalbrun
 
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting TalkJuly 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talkottawaruby
 
Becoming a better developer by using the SOLID design principles
Becoming a better developer by using the SOLID design principlesBecoming a better developer by using the SOLID design principles
Becoming a better developer by using the SOLID design principlesKaterina Trajchevska
 
Ofir Dagan - (Don’t) Blame it on React Native - Codemotion Rome 2019
Ofir Dagan - (Don’t) Blame it on React Native - Codemotion Rome 2019Ofir Dagan - (Don’t) Blame it on React Native - Codemotion Rome 2019
Ofir Dagan - (Don’t) Blame it on React Native - Codemotion Rome 2019Codemotion
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patternsPrabhakar Sharma
 
S.O.L.I.D: Principles of OOP and Agile design
S.O.L.I.D: Principles of OOP and Agile design S.O.L.I.D: Principles of OOP and Agile design
S.O.L.I.D: Principles of OOP and Agile design ADEDAMOLA ADEDAPO
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean ArchitectureRoc Boronat
 

Similar a Object Oriented Design in Ruby (20)

Design Principles
Design PrinciplesDesign Principles
Design Principles
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
 
Evgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shapeEvgeniy Khyst - why does software design matter and how to keep it in good shape
Evgeniy Khyst - why does software design matter and how to keep it in good shape
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
Developing solid applications
Developing solid applicationsDeveloping solid applications
Developing solid applications
 
Design for Testability
Design for Testability Design for Testability
Design for Testability
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting TalkJuly 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
July 2012 Ruby Tuesday - Lana Lodge - Refactoring Lighting Talk
 
Becoming a better developer by using the SOLID design principles
Becoming a better developer by using the SOLID design principlesBecoming a better developer by using the SOLID design principles
Becoming a better developer by using the SOLID design principles
 
Ofir Dagan - (Don’t) Blame it on React Native - Codemotion Rome 2019
Ofir Dagan - (Don’t) Blame it on React Native - Codemotion Rome 2019Ofir Dagan - (Don’t) Blame it on React Native - Codemotion Rome 2019
Ofir Dagan - (Don’t) Blame it on React Native - Codemotion Rome 2019
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
S.O.L.I.D: Principles of OOP and Agile design
S.O.L.I.D: Principles of OOP and Agile design S.O.L.I.D: Principles of OOP and Agile design
S.O.L.I.D: Principles of OOP and Agile design
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Buddy navigator
Buddy navigatorBuddy navigator
Buddy navigator
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 
Solid
SolidSolid
Solid
 

Último

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, ...apidays
 
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...Martijn de Jong
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
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 CVKhem
 
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 DevelopmentsTrustArc
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
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.pdfsudhanshuwaghmare1
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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 educationjfdjdjcjdnsjd
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
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...apidays
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
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...apidays
 

Último (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, ...
 
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...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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
 
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
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
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
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
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...
 

Object Oriented Design in Ruby

  • 1. Object Oriented Design In Ruby Presented By- Swapnil Abnave Kiprosh, 5 December 2013
  • 2. CHANGE Fact: your application is going to change. How will your application handle that change?
  • 3. Your App may be o o o o Rigid: Making a change somewhere will break something somewhere else. Fragile: You can’t predict where that break will be. Immobile: It’s hard to change/re-use your code. Viscous: It’s easier to do the wrong thing than to fix things.
  • 4. Design is all about dependencies • If you refer to something, you depend on it. • When the things you depend on change, you must change.
  • 5. SOLID ● Single Responsibility ● Open Closed ● Liskov Substitution ● Interface Segregation ● Dependency Inversion
  • 6. Single Responsibility There should never be more than one reason for a class to change
  • 7. Open/Closed A module should be open for extension but closed for modification
  • 8. Dependency Inversion Depend upon abstractions. Do not depend upon concretions
  • 9. IGNORABLE RULES SOLID principles we can ignore in ruby: ● Interface Segregation ● Liskov Substitution WHY ?
  • 10. Interface Segregation ● Really only a problem for staticallytyped, compiled languages. “Because we’re in Ruby, we don’t have this problem! Win!” ● “Dynamic languages obey this rule in the most extreme way possible: duck typing.”
  • 11. Interface Segregation • What is it ? - “Many client specific interfaces are better than one general purpose interface”
  • 12. Liskov Substitution When you design, don’t break the contract of the superclass in the subclass.
  • 13. DESIGN MIGHT SAVE YOU To avoid dependencies, your design should be: ● Loosely coupled – D – Inject ● Highly cohesive – SRP ● Easily composable – Can be changed ● Context independent – Can be rearranged
  • 14. Resistance Resistance is a resource => Listen to what the pain is telling you. Listen to what the code smells are telling you. ● ● Embrace the friction. ● Fix the problem. If testing seems hard – examine your design.
  • 15. Your Checkpoint for Design When you get to the refactor stage of red-green-refactor, ask yourself … ● Is it DRY? ● Does it have just one responsibility? ● ● Does everything change at the same rate? Does it depend on things that change
  • 16. "Triangle of Responsibility" Refactoring ● ● ● Refactor Extract - Pull functionality out where necessary Inject - Inject that new dependency into place from which it was extracted
  • 17. Act like an idiot ● ● ● What if I don't know where I want this refactoring to take me? That's OK. In fact, that's typical. "Refactor, not because you know the abstraction, but because you want to find it."
  • 18. Act like an idiot ● ● "You don't have to know where you're going to successfully refactor." When you see someone's code and think it's beautiful and you wonder how they thought of it, they didn't. They evolved it to that point.
  • 19. Dependency Injection When injecting dependencies into a class, do so only via arguments to the #initialize method def intialize(downloader=FTPDownloader.ne w) @downloader = downloader
  • 20. Argument Order Dependency When you need to inject a few dependencies, you can use an options hash to remove the dependency on the order of the arguments def intialize(opts) @env = opts[:env] || Rails.env filename = opts[:filename] end
  • 21. Dependency is unavoidable How to assess: "Does each object depend on things that change less than it does?” ● Line up the objects from left to right Left = lower likelihood of change Right = higher likelihood of change ● Only depend on things on your left
  • 22. Conclude to begin with design TDD is not enough DRY is not enough Design because you expect your application to succeed(and to change in the future to come)