SlideShare a Scribd company logo
1 of 40
Download to read offline
OOP
What? Why?
 on Rails?
earliest evidence of double-entry bookkeeping
       is the Farolfi ledger of 1299-1300.
The earliest evidence of
 full double-entry bookkeeping
is the Farolfi ledger of 1299-1300.


  Why am I talking about this?
The earliest evidence of
  full double-entry bookkeeping
is the Farolfi ledger of 1299-1300.


   Why am I talking about this?


...because we are amazingly lucky
that our idea creators are still alive!
Barbara Liskov

    ACM Turing Award lecture (2009) 11:50
http://www.infoq.com/presentations/liskov-power-of-abstraction
The entire system should be broken up into
                  “Partitions”
   Each has access to some hidden state
          Each provides operations
Only interact by calling each others operations
Why doesn't she get to the point?
  She's talking about 'Objects'!
Why doesn't she get to the point?
       She's talking about 'Objects'!


Object Oriented Programming didn't exist...
Who invented OOP?
Alan Kay is credited with coining the term
   Led research to develop SmallTalk
                   => 1972 (40ish years ago)
Who invented OOP?
It developed later as the dominant methodology
  when programming languages supporting
   the techniques became widely available
               (C++, Java, Delphi)
              => early-mid 1990s (20ish years ago)
   "Actually I made up the term "object-oriented",
   and I can tell you I did not have C++ in mind."
                                            -- Alan Kay
I thought of objects
        being like biological cells
and/or individual computers on a network,
only able to communicate with messages
(so messaging came at the very beginning)
                                      – Alan Kay
Design is *all* about Dependencies
If you refer to something
       ---> you depend on it
When the things you depend on change
       ---> you must change
     -- Sandi Metz, "Solid Design" GoRuCo 2009
A----
               B-----
               C -----> X
               D-----/
               E ---/


●   if x changes everybody has to change!
●   x better not change! It should be stable
/--> A
                     /---> B
                Z ------> C
                     ---> D
                      --> E

●   if z changes no body cares!
●   if c changes, only z may have to
●   z is isolated!
J → K → L



●   if L changes, K may have to
●   Maybe J will too?
OO Design in Rails?
ActiveRecord / Objects?
   Rather than asking for data,
tell a class to do something for you
ActiveRecord / Objects?
Procedural gets info, then makes decision
ActiveRecord / Objects?
  Procedural gets info, then makes decision
Object Oriented code tells objects to do things
ActiveRecord / Objects?
  Procedural gets info, then makes decision
Object Oriented code tells objects to do things


       What do you do in Rails code?
S.O.L.I.D
S:   SRP: Single Responsibility Principle
O:   OCP: Open/Closed Principle
L:   LSP:   Liskov Substitution Principle
I:   ISP:   Interface Segregation Principle
D:   DIP:   Dependency Inversion Principle
Single Responsibility
      An object should have only a
          single responsibility
 One and ONLY ONE reason to change


If you have to use the word “AND” or “OR”
         to describe your class....
Jim Weirich talk on SOLID
Jim asked: AR mixes domain + persistence
 -- is it ok?
Jim Weirich talk on SOLID
Jim asked: AR mixes domain + persistence
 -- is it ok?
Response:
●   NO! As design gets complex and interactions
    between objects get complex, it confuses
    things.
●   NO! It's harder to test.
Jim Weirich talk on SOLID
Jim asked: AR mixes domain + persistence
 -- is it ok?
Response:
●   NO! As design gets complex and interactions
    between objects get complex, it confuses things.
●   NO! It's harder to test.
Jim said: Maybe it IS OK in your case...!
●   IT'S PRINCIPLES NOT RULES! up to you!
The DHH Factor
...later on twitter


@paulc: ... What are your thoughts on
separating logic + persistence?
The DHH Factor
...later on twitter


@paulc: ... What are your thoughts on
separating logic + persistence?
@dhh: Fuck. That. Shit. Same complete wank.
"Rails is not your application".
If you're building a web app, of course it is.
#find and the infinite protocol
         If ActiveRecord models
        Are your Domain Models


  You are exposing an infinite protocol!
.where(:category => “Blah”).order(:name)
Interface Segregation Principle
         Code should depend on
     as NARROW protocol as possible


The protocol is the messages between objects


            Not WIDE interfaces!
        It's just encapsulation, right?
#find and the infinite protocol
       If ActiveRecord models
      Are your Domain Models


You are exposing an infinite protocol!
             .find(:first)
Single Responsibility
Build models without referencing ActiveRecord?
Why?
●   Faster tests
●   Separate concerns for complex situations
●   Expose a clean, domain-oriented interface
●   Logic unrelated to persistence:
    At the intersection of persistable objects
    You often find business rules.
    IT'S A THIRD PLAIN RUBY OBJECT!
Single Responsibility
Build models without referencing ActiveRecord?
How?
●   FigLeaf approach?
    –   The gem “hides” all AR methods
        (so you can't access them outside your model)
    –   You expose the functionality you mean
    –   It's domain driven
Single Responsibility
Build models without referencing ActiveRecord?
How?
●   Domain Object approach?
    –   You keep AR models as thin as possible
    –   Add a separate object for Domain logic
        that accesses the AR model
    –   Everybody else accesses the Domain object
...thoughts
 “There is no problem in computer science
           that cannot be solved
   by adding another layer of indirection,
except having too many layers of indirection”
...thoughts
@dhh
 I hate the disconnect between design
 patterns/abstract ideas and implementation
 Your basic question should be “Is the code
 better?”
...thoughts
@avdi
 You absolutely MUST not give up
 the fun you found when first coming to ruby
 when working on larger systems
...your thoughts?

More Related Content

Similar to OO and Rails...

Intro to openFrameworks
Intro to openFrameworksIntro to openFrameworks
Intro to openFrameworksKyle McDonald
 
Method Swizzling with Objective-C
Method Swizzling with Objective-CMethod Swizzling with Objective-C
Method Swizzling with Objective-CAdamFallon4
 
C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem Claudson Oliveira
 
Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)muhammadmubinmacadad2
 
Dependency Injection: Why is awesome and why should I care?
Dependency Injection: Why is awesome and why should I care?Dependency Injection: Why is awesome and why should I care?
Dependency Injection: Why is awesome and why should I care?devObjective
 
A Lightning Introduction To Clouds & HLT - Human Language Technology Conference
A Lightning Introduction To Clouds & HLT - Human Language Technology ConferenceA Lightning Introduction To Clouds & HLT - Human Language Technology Conference
A Lightning Introduction To Clouds & HLT - Human Language Technology ConferenceBasis Technology
 
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...David Beazley (Dabeaz LLC)
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallJohn Mulhall
 
Ruby object model
Ruby object modelRuby object model
Ruby object modelmbeizer
 
Ruby on Rails - An overview
Ruby on Rails -  An overviewRuby on Rails -  An overview
Ruby on Rails - An overviewThomas Asikis
 
Intro to oop.pptx
Intro to oop.pptxIntro to oop.pptx
Intro to oop.pptxUmerUmer25
 
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"JSFestUA
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)Jorge López-Lago
 
Brief introduction to Object Oriented Analysis and Design
Brief introduction to Object Oriented Analysis and DesignBrief introduction to Object Oriented Analysis and Design
Brief introduction to Object Oriented Analysis and DesignAmrullah Zunzunia
 
Tech breakfast 18
Tech breakfast 18Tech breakfast 18
Tech breakfast 18James Leone
 
Documentation for developers
Documentation for developersDocumentation for developers
Documentation for developersMichael Marotta
 

Similar to OO and Rails... (20)

Intro to openFrameworks
Intro to openFrameworksIntro to openFrameworks
Intro to openFrameworks
 
Method Swizzling with Objective-C
Method Swizzling with Objective-CMethod Swizzling with Objective-C
Method Swizzling with Objective-C
 
C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem C# .NET - Um overview da linguagem
C# .NET - Um overview da linguagem
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)
 
The CQRS diet
The CQRS dietThe CQRS diet
The CQRS diet
 
Dependency Injection: Why is awesome and why should I care?
Dependency Injection: Why is awesome and why should I care?Dependency Injection: Why is awesome and why should I care?
Dependency Injection: Why is awesome and why should I care?
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
A Lightning Introduction To Clouds & HLT - Human Language Technology Conference
A Lightning Introduction To Clouds & HLT - Human Language Technology ConferenceA Lightning Introduction To Clouds & HLT - Human Language Technology Conference
A Lightning Introduction To Clouds & HLT - Human Language Technology Conference
 
Look beyond PHP
Look beyond PHPLook beyond PHP
Look beyond PHP
 
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
Why Extension Programmers Should Stop Worrying About Parsing and Start Thinki...
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John Mulhall
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Ruby on Rails - An overview
Ruby on Rails -  An overviewRuby on Rails -  An overview
Ruby on Rails - An overview
 
Intro to oop.pptx
Intro to oop.pptxIntro to oop.pptx
Intro to oop.pptx
 
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
JS Fest 2019/Autumn. Alexandre Gomes. Embrace the "react fatigue"
 
Open event (show&tell april 2016)
Open event (show&tell april 2016)Open event (show&tell april 2016)
Open event (show&tell april 2016)
 
Brief introduction to Object Oriented Analysis and Design
Brief introduction to Object Oriented Analysis and DesignBrief introduction to Object Oriented Analysis and Design
Brief introduction to Object Oriented Analysis and Design
 
Tech breakfast 18
Tech breakfast 18Tech breakfast 18
Tech breakfast 18
 
Documentation for developers
Documentation for developersDocumentation for developers
Documentation for developers
 

Recently uploaded

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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 AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
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 2024The Digital Insurer
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

OO and Rails...

  • 2. earliest evidence of double-entry bookkeeping is the Farolfi ledger of 1299-1300.
  • 3. The earliest evidence of full double-entry bookkeeping is the Farolfi ledger of 1299-1300. Why am I talking about this?
  • 4. The earliest evidence of full double-entry bookkeeping is the Farolfi ledger of 1299-1300. Why am I talking about this? ...because we are amazingly lucky that our idea creators are still alive!
  • 5. Barbara Liskov ACM Turing Award lecture (2009) 11:50 http://www.infoq.com/presentations/liskov-power-of-abstraction
  • 6. The entire system should be broken up into “Partitions” Each has access to some hidden state Each provides operations Only interact by calling each others operations
  • 7. Why doesn't she get to the point? She's talking about 'Objects'!
  • 8. Why doesn't she get to the point? She's talking about 'Objects'! Object Oriented Programming didn't exist...
  • 9. Who invented OOP? Alan Kay is credited with coining the term Led research to develop SmallTalk => 1972 (40ish years ago)
  • 10. Who invented OOP? It developed later as the dominant methodology when programming languages supporting the techniques became widely available (C++, Java, Delphi) => early-mid 1990s (20ish years ago) "Actually I made up the term "object-oriented", and I can tell you I did not have C++ in mind." -- Alan Kay
  • 11. I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages (so messaging came at the very beginning) – Alan Kay
  • 12. Design is *all* about Dependencies If you refer to something ---> you depend on it When the things you depend on change ---> you must change -- Sandi Metz, "Solid Design" GoRuCo 2009
  • 13.
  • 14.
  • 15. A---- B----- C -----> X D-----/ E ---/ ● if x changes everybody has to change! ● x better not change! It should be stable
  • 16. /--> A /---> B Z ------> C ---> D --> E ● if z changes no body cares! ● if c changes, only z may have to ● z is isolated!
  • 17. J → K → L ● if L changes, K may have to ● Maybe J will too?
  • 18. OO Design in Rails?
  • 19. ActiveRecord / Objects? Rather than asking for data, tell a class to do something for you
  • 20. ActiveRecord / Objects? Procedural gets info, then makes decision
  • 21. ActiveRecord / Objects? Procedural gets info, then makes decision Object Oriented code tells objects to do things
  • 22. ActiveRecord / Objects? Procedural gets info, then makes decision Object Oriented code tells objects to do things What do you do in Rails code?
  • 23. S.O.L.I.D S: SRP: Single Responsibility Principle O: OCP: Open/Closed Principle L: LSP: Liskov Substitution Principle I: ISP: Interface Segregation Principle D: DIP: Dependency Inversion Principle
  • 24. Single Responsibility An object should have only a single responsibility One and ONLY ONE reason to change If you have to use the word “AND” or “OR” to describe your class....
  • 25. Jim Weirich talk on SOLID Jim asked: AR mixes domain + persistence -- is it ok?
  • 26. Jim Weirich talk on SOLID Jim asked: AR mixes domain + persistence -- is it ok? Response: ● NO! As design gets complex and interactions between objects get complex, it confuses things. ● NO! It's harder to test.
  • 27. Jim Weirich talk on SOLID Jim asked: AR mixes domain + persistence -- is it ok? Response: ● NO! As design gets complex and interactions between objects get complex, it confuses things. ● NO! It's harder to test. Jim said: Maybe it IS OK in your case...! ● IT'S PRINCIPLES NOT RULES! up to you!
  • 28. The DHH Factor ...later on twitter @paulc: ... What are your thoughts on separating logic + persistence?
  • 29. The DHH Factor ...later on twitter @paulc: ... What are your thoughts on separating logic + persistence? @dhh: Fuck. That. Shit. Same complete wank. "Rails is not your application". If you're building a web app, of course it is.
  • 30. #find and the infinite protocol If ActiveRecord models Are your Domain Models You are exposing an infinite protocol! .where(:category => “Blah”).order(:name)
  • 31. Interface Segregation Principle Code should depend on as NARROW protocol as possible The protocol is the messages between objects Not WIDE interfaces! It's just encapsulation, right?
  • 32. #find and the infinite protocol If ActiveRecord models Are your Domain Models You are exposing an infinite protocol! .find(:first)
  • 33. Single Responsibility Build models without referencing ActiveRecord? Why? ● Faster tests ● Separate concerns for complex situations ● Expose a clean, domain-oriented interface ● Logic unrelated to persistence: At the intersection of persistable objects You often find business rules. IT'S A THIRD PLAIN RUBY OBJECT!
  • 34. Single Responsibility Build models without referencing ActiveRecord? How? ● FigLeaf approach? – The gem “hides” all AR methods (so you can't access them outside your model) – You expose the functionality you mean – It's domain driven
  • 35. Single Responsibility Build models without referencing ActiveRecord? How? ● Domain Object approach? – You keep AR models as thin as possible – Add a separate object for Domain logic that accesses the AR model – Everybody else accesses the Domain object
  • 36. ...thoughts “There is no problem in computer science that cannot be solved by adding another layer of indirection, except having too many layers of indirection”
  • 37.
  • 38. ...thoughts @dhh I hate the disconnect between design patterns/abstract ideas and implementation Your basic question should be “Is the code better?”
  • 39. ...thoughts @avdi You absolutely MUST not give up the fun you found when first coming to ruby when working on larger systems