OO and Rails...
-
Upload
adzdavies -
Category
Technology
-
view
533 -
download
1
Transcript of OO and Rails...
![Page 1: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/1.jpg)
OOPOOPWhat? Why?What? Why?
on Rails?on Rails?
![Page 2: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/2.jpg)
\\
earliest evidence of double-entry bookkeeping earliest evidence of double-entry bookkeeping is the Farolfi ledger of 1299-1300.is the Farolfi ledger of 1299-1300.
![Page 3: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/3.jpg)
The earliest evidence of
full double-entry bookkeeping
is the Farolfi ledger of 1299-1300.
Why am I talking about this?
![Page 4: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/4.jpg)
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!
![Page 5: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/5.jpg)
Barbara Liskov
ACM Turing Award lecture (2009) 11:50http://www.infoq.com/presentations/liskov-power-of-abstraction
![Page 6: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/6.jpg)
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
![Page 7: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/7.jpg)
Why doesn't she get to the point?
She's talking about 'Objects'!
![Page 8: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/8.jpg)
Why doesn't she get to the point?
She's talking about 'Objects'!
Object Oriented Programming didn't exist...
![Page 9: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/9.jpg)
Who invented OOP?
Alan Kay is credited with coining the term
Led research to develop SmallTalk
=> 1972 (40ish years ago)
![Page 10: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/10.jpg)
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
![Page 11: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/11.jpg)
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
![Page 12: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/12.jpg)
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
![Page 13: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/13.jpg)
![Page 14: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/14.jpg)
![Page 15: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/15.jpg)
A----\
B-----\
C -----> X
D-----/
E ---/
● if x changes everybody has to change!● x better not change! It should be stable
![Page 16: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/16.jpg)
/--> A
/---> B
Z ------> C
\---> D
\--> E
● if z changes no body cares!● if c changes, only z may have to● z is isolated!
![Page 17: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/17.jpg)
J K L→ →
● if L changes, K may have to● Maybe J will too?
![Page 18: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/18.jpg)
OO Design in Rails?
![Page 19: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/19.jpg)
ActiveRecord / Objects?
Rather than asking for data,
tell a class to do something for you
![Page 20: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/20.jpg)
ActiveRecord / Objects?
Procedural gets info, then makes decision
![Page 21: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/21.jpg)
ActiveRecord / Objects?
Procedural gets info, then makes decision
Object Oriented code tells objects to do things
![Page 22: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/22.jpg)
ActiveRecord / Objects?
Procedural gets info, then makes decision
Object Oriented code tells objects to do things
What do you do in Rails code?
![Page 23: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/23.jpg)
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
![Page 24: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/24.jpg)
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....
![Page 25: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/25.jpg)
Jim Weirich talk on SOLID
Jim asked: AR mixes domain + persistence -- is it ok?
![Page 26: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/26.jpg)
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.
![Page 27: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/27.jpg)
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!
![Page 28: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/28.jpg)
The DHH Factor
...later on twitter
@paulc: ... What are your thoughts on separating logic + persistence?
![Page 29: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/29.jpg)
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.
![Page 30: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/30.jpg)
#find and the infinite protocol
If ActiveRecord models
Are your Domain Models
You are exposing an infinite protocol!
.where(:category => “Blah”).order(:name)
![Page 31: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/31.jpg)
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?
![Page 32: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/32.jpg)
#find and the infinite protocol
If ActiveRecord models
Are your Domain Models
You are exposing an infinite protocol!
.find(:first)
![Page 33: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/33.jpg)
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 objectsYou often find business rules. IT'S A THIRD PLAIN RUBY OBJECT!
![Page 34: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/34.jpg)
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
![Page 35: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/35.jpg)
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 logicthat accesses the AR model
– Everybody else accesses the Domain object
![Page 36: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/36.jpg)
...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”
![Page 37: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/37.jpg)
![Page 38: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/38.jpg)
...thoughts
@dhh
I hate the disconnect between design patterns/abstract ideas and implementation
Your basic question should be “Is the code better?”
![Page 39: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/39.jpg)
...thoughts
@avdi
You absolutely MUST not give up the fun you found when first coming to ruby when working on larger systems
![Page 40: OO and Rails...](https://reader033.fdocuments.us/reader033/viewer/2022060119/558e99371a28ab9c708b46b5/html5/thumbnails/40.jpg)
...your thoughts?