Page 1
Object Invariants, Immutability & You
Making Objects Do Your Bidding AmsterdamPHP 15-01-2015
Page 2
Jeroen v.d. Gulik
• CTO / Co-Founder Isset (http://isset.nl)
• Builder of Artificial Stupidity
• @n0x13
• Likes Cats … a lot
Page 3
Isset Internet Professionals• We build custom internet technology based solutions
• We helped design and build several online video platforms (Pathé Thuis, Videoland, NPO Plus)
• Currently focussing on video-transcoder.com
• We like a challenge so if you have interesting problems to solve, come and talk to me
• Still Hiring
Page 4
“Every Great Talk should start with a Great Quote”
- Ross Tuck-
Page 7
“Over time, the original intent and purpose
gets lost”- Jeroen v.d. Gulik
Page 8
“A Customer should always have an email
address”
Page 13
Testing shows the presence, not the absence
of bugs. - Joseph Stalin
Page 16
“We should enforce (business) rules when
creating objects” - Mao Zedong
Page 17
“A Customer should have a valid email address”
Page 19
Outside-in validation should be avoided
- Albert Einstein
Page 21
Objects should be responsible for their own
internal consistency - Barack Obama
Page 23
● Single Responsibility Principle
● Duplicate code
Page 26
Design by contract
Page 28
Value Object
● Small Objects
● Equality based on value
● Immutable
Page 29
Entity Object
● Usually big(-ger)
● Equality based on Identity
● Mutable
● Can change over time
Page 30
Immutability● Simplifies code
● Less cognitive load
● Guaranteed consistent
● Referencing without breakage
● Easy to test
● Caching (profit!)
Page 31
Immutable Object, Best Object
- The Hulk
Page 32
“A Customer should have a valid email address and a
password”
Page 35
Make the Implicit, Explicit
Page 39
Objects as dumb data stores
This is our industry standard
Page 43
Objects should have behaviour attached
to them- Julius Caesar
Page 47
Objects should tell a tale about its current state and all the possible
ways to change that state- Bruce Lee
Page 51
Schrödinger's cat
Page 52
UnpaidOrder PaidOrder
Page 55
“Sounds like a State Machine”- Igor Wiedler
Page 57
UnpublishedArticle PublishedArticle
Page 58
UnpublishedArticle PublishedArticleDraft Article
Page 60
Draft Article RetractedArticle
Page 62
Objects should only expose methods relevant to it's
current state - George Bush
Page 65
Objects should …
• Be responsible for their own internal state
• Have specific behaviour towards its responsibility
• Broadcast their state
• Only care about the state it’s currently in.
Page 66
Magic is bad, hmmmkay- Saddam Hussein
Page 67
Questions?Please rate my talk: https://joind.in/event/view/
3267