OOPWhat? Why? on Rails?
earliest evidence of double-entry bookkeeping       is the Farolfi ledger of 1299-1300.
The earliest evidence of full double-entry bookkeepingis the Farolfi ledger of 1299-1300.  Why am I talking about this?
The earliest evidence of  full double-entry bookkeepingis the Farolfi ledger of 1299-1300.   Why am I talking about this?....
Barbara Liskov    ACM Turing Award lecture (2009) 11:50http://www.infoq.com/presentations/liskov-power-of-abstraction
The entire system should be broken up into                  “Partitions”   Each has access to some hidden state          E...
Why doesnt she get to the point?  Shes talking about Objects!
Why doesnt she get to the point?       Shes talking about Objects!Object Oriented Programming didnt exist...
Who invented OOP?Alan Kay is credited with coining the term   Led research to develop SmallTalk                   => 1972 ...
Who invented OOP?It developed later as the dominant methodology  when programming languages supporting   the techniques be...
I thought of objects        being like biological cellsand/or individual computers on a network,only able to communicate w...
Design is *all* about DependenciesIf you refer to something       ---> you depend on itWhen the things you depend on chang...
A----               B-----               C -----> X               D-----/               E ---/●   if x changes everybody h...
/--> A                     /---> B                Z ------> C                     ---> D                      --> E●   if ...
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 decisionObject Oriented code tells objects to do things
ActiveRecord / Objects?  Procedural gets info, then makes decisionObject Oriented code tells objects to do things       Wh...
S.O.L.I.DS:   SRP: Single Responsibility PrincipleO:   OCP: Open/Closed PrincipleL:   LSP:   Liskov Substitution Principle...
Single Responsibility      An object should have only a          single responsibility One and ONLY ONE reason to changeIf...
Jim Weirich talk on SOLIDJim asked: AR mixes domain + persistence -- is it ok?
Jim Weirich talk on SOLIDJim asked: AR mixes domain + persistence -- is it ok?Response:●   NO! As design gets complex and ...
Jim Weirich talk on SOLIDJim asked: AR mixes domain + persistence -- is it ok?Response:●   NO! As design gets complex and ...
The DHH Factor...later on twitter@paulc: ... What are your thoughts onseparating logic + persistence?
The DHH Factor...later on twitter@paulc: ... What are your thoughts onseparating logic + persistence?@dhh: Fuck. That. Shi...
#find and the infinite protocol         If ActiveRecord models        Are your Domain Models  You are exposing an infinite...
Interface Segregation Principle         Code should depend on     as NARROW protocol as possibleThe protocol is the messag...
#find and the infinite protocol       If ActiveRecord models      Are your Domain ModelsYou are exposing an infinite proto...
Single ResponsibilityBuild models without referencing ActiveRecord?Why?●   Faster tests●   Separate concerns for complex s...
Single ResponsibilityBuild models without referencing ActiveRecord?How?●   FigLeaf approach?    –   The gem “hides” all AR...
Single ResponsibilityBuild models without referencing ActiveRecord?How?●   Domain Object approach?    –   You keep AR mode...
...thoughts “There is no problem in computer science           that cannot be solved   by adding another layer of indirect...
...thoughts@dhh I hate the disconnect between design patterns/abstract ideas and implementation Your basic question should...
...thoughts@avdi You absolutely MUST not give up the fun you found when first coming to ruby when working on larger systems
...your thoughts?
OO and Rails...
OO and Rails...
OO and Rails...
Upcoming SlideShare
Loading in …5
×

OO and Rails...

752 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
752
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

OO and Rails...

  1. 1. OOPWhat? Why? on Rails?
  2. 2. earliest evidence of double-entry bookkeeping is the Farolfi ledger of 1299-1300.
  3. 3. The earliest evidence of full double-entry bookkeepingis the Farolfi ledger of 1299-1300. Why am I talking about this?
  4. 4. The earliest evidence of full double-entry bookkeepingis the Farolfi ledger of 1299-1300. Why am I talking about this?...because we are amazingly luckythat our idea creators are still alive!
  5. 5. Barbara Liskov ACM Turing Award lecture (2009) 11:50http://www.infoq.com/presentations/liskov-power-of-abstraction
  6. 6. The entire system should be broken up into “Partitions” Each has access to some hidden state Each provides operationsOnly interact by calling each others operations
  7. 7. Why doesnt she get to the point? Shes talking about Objects!
  8. 8. Why doesnt she get to the point? Shes talking about Objects!Object Oriented Programming didnt exist...
  9. 9. Who invented OOP?Alan Kay is credited with coining the term Led research to develop SmallTalk => 1972 (40ish years ago)
  10. 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. 11. I thought of objects being like biological cellsand/or individual computers on a network,only able to communicate with messages(so messaging came at the very beginning) – Alan Kay
  12. 12. Design is *all* about DependenciesIf you refer to something ---> you depend on itWhen the things you depend on change ---> you must change -- Sandi Metz, "Solid Design" GoRuCo 2009
  13. 13. A---- B----- C -----> X D-----/ E ---/● if x changes everybody has to change!● x better not change! It should be stable
  14. 14. /--> A /---> B Z ------> C ---> D --> E● if z changes no body cares!● if c changes, only z may have to● z is isolated!
  15. 15. J → K → L● if L changes, K may have to● Maybe J will too?
  16. 16. OO Design in Rails?
  17. 17. ActiveRecord / Objects? Rather than asking for data,tell a class to do something for you
  18. 18. ActiveRecord / Objects?Procedural gets info, then makes decision
  19. 19. ActiveRecord / Objects? Procedural gets info, then makes decisionObject Oriented code tells objects to do things
  20. 20. ActiveRecord / Objects? Procedural gets info, then makes decisionObject Oriented code tells objects to do things What do you do in Rails code?
  21. 21. S.O.L.I.DS: SRP: Single Responsibility PrincipleO: OCP: Open/Closed PrincipleL: LSP: Liskov Substitution PrincipleI: ISP: Interface Segregation PrincipleD: DIP: Dependency Inversion Principle
  22. 22. Single Responsibility An object should have only a single responsibility One and ONLY ONE reason to changeIf you have to use the word “AND” or “OR” to describe your class....
  23. 23. Jim Weirich talk on SOLIDJim asked: AR mixes domain + persistence -- is it ok?
  24. 24. Jim Weirich talk on SOLIDJim asked: AR mixes domain + persistence -- is it ok?Response:● NO! As design gets complex and interactions between objects get complex, it confuses things.● NO! Its harder to test.
  25. 25. Jim Weirich talk on SOLIDJim asked: AR mixes domain + persistence -- is it ok?Response:● NO! As design gets complex and interactions between objects get complex, it confuses things.● NO! Its harder to test.Jim said: Maybe it IS OK in your case...!● ITS PRINCIPLES NOT RULES! up to you!
  26. 26. The DHH Factor...later on twitter@paulc: ... What are your thoughts onseparating logic + persistence?
  27. 27. The DHH Factor...later on twitter@paulc: ... What are your thoughts onseparating logic + persistence?@dhh: Fuck. That. Shit. Same complete wank."Rails is not your application".If youre building a web app, of course it is.
  28. 28. #find and the infinite protocol If ActiveRecord models Are your Domain Models You are exposing an infinite protocol!.where(:category => “Blah”).order(:name)
  29. 29. Interface Segregation Principle Code should depend on as NARROW protocol as possibleThe protocol is the messages between objects Not WIDE interfaces! Its just encapsulation, right?
  30. 30. #find and the infinite protocol If ActiveRecord models Are your Domain ModelsYou are exposing an infinite protocol! .find(:first)
  31. 31. Single ResponsibilityBuild 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. ITS A THIRD PLAIN RUBY OBJECT!
  32. 32. Single ResponsibilityBuild models without referencing ActiveRecord?How?● FigLeaf approach? – The gem “hides” all AR methods (so you cant access them outside your model) – You expose the functionality you mean – Its domain driven
  33. 33. Single ResponsibilityBuild 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
  34. 34. ...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”
  35. 35. ...thoughts@dhh I hate the disconnect between design patterns/abstract ideas and implementation Your basic question should be “Is the code better?”
  36. 36. ...thoughts@avdi You absolutely MUST not give up the fun you found when first coming to ruby when working on larger systems
  37. 37. ...your thoughts?

×