Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Object Invariants,
Immutability & You
Making Objects Do Your Bidding
NijmegenPHP 15-04-2015
Jeroen v.d. Gulik
• CTO / Co-Founder Isset
(http://isset.nl)
• Builder of Artificial Stupidity
• @n0x13
• Likes Cats … a lot
Isset Internet Professionals
• We build custom internet technology based solutions
• We helped design and build several on...
“Every Great Talk should
start with a Great Quote”
Ross Tuck
-House M.D.
Telephone Game
“Over time, the original
intent and purpose gets
lost”
- Jeroen v.d. Gulik
“A Customer should
always have an email
address”
Testing shows the presence,
not the absence of bugs.
- Kanye West
“We should enforce (business)
rules when creating objects”
- Justin Bieber
“A Customer should have
a valid email address”
Outside-in validation
should be avoided

- Albert Einstein
Objects should be responsible
for their own internal consistency
- Barack Obama
Best Solution ?
● Single Responsibility Principle
● Duplicate code
Design by contract
Value Object
● Small Objects
● Equality based on value
● Immutable
Example Value Objects
• Money
• Currency
• Time
• DateRange / Period
Money Value Object
https://github.com/mathiasverraes/money
Entity Object
● Usually big(-ger)
● Equality based on Identity
● Mutable
● Can change over time
Immutability
● Simplifies code
● Less cognitive load
● Guaranteed consistent
● Referencing without side effects
● Easy to t...
Immutable Object,
Best Object
- The Hulk
“A Customer should have
a valid email address
and a password”
Make the Implicit,
Explicit !
Assertion Library
https://github.com/beberlei/assert
“As an Author I want to be able to Create
an Article. Before Publishing it, I want to
have a Draft. I also would like to b...
Objects as dumb data stores
a.k.a.
Anemic Domain Model

This is our industry standard
Published ?
Objects should have
behaviour attached 

to them
- Lex Luther
Objects should tell a tale about its
current state and all the possible
ways to change that state
- Bruce Lee
Schrödinger's cat
UnpaidOrder PaidOrder
“Sounds like a State Machine”
- Igor Wiedler
Immutability
but of a different kind
UnpublishedArticle PublishedArticle
UnpublishedArticle PublishedArticle
Draft Article
Draft Article RetractedArticle
Objects should only expose
methods relevant to it's current
state
- George Bush
Objects should …
• Be responsible for their own internal consistency
• Have specific behaviour towards its
responsibility
•...
But there is still a piece
of the puzzle missing…
As a Publisher I want to
know which Article has been
retracted more than X times
As an Editor I want to see
all the changes that have
been made
Store all the Changes
(Δ delta’s),that have
happened
EventSourcing!
But that is a talk on its own
Magic is bad, hmmmkay
- Saddam Hussein
Questions?
https://joind.in/event/nijmegenphp-201504
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Upcoming SlideShare
Loading in …5
×

2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

623 views

Published on

Code tells a story, but is it the actual story the original author wanted to tell? A lot of time the original story gets lost, as new functionality gets bolted on, or key parts are extracted, leaving behind an incomplete picture. These repurposed objects might pass all the unit tests but have no actual meaning in the context of the original story. We will take a look at how we can more easily communicate the purpose of code to future developers, by introducing contracts and immutability, and using explicit context bound language, to give meaning to the code and keep the story whole.

Published in: Software
  • Be the first to comment

  • Be the first to like this

2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

  1. 1. Object Invariants, Immutability & You Making Objects Do Your Bidding NijmegenPHP 15-04-2015
  2. 2. Jeroen v.d. Gulik • CTO / Co-Founder Isset (http://isset.nl) • Builder of Artificial Stupidity • @n0x13 • Likes Cats … a lot
  3. 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/NLZiet) • 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
  4. 4. “Every Great Talk should start with a Great Quote” Ross Tuck
  5. 5. -House M.D.
  6. 6. Telephone Game
  7. 7. “Over time, the original intent and purpose gets lost” - Jeroen v.d. Gulik
  8. 8. “A Customer should always have an email address”
  9. 9. Testing shows the presence, not the absence of bugs. - Kanye West
  10. 10. “We should enforce (business) rules when creating objects” - Justin Bieber
  11. 11. “A Customer should have a valid email address”
  12. 12. Outside-in validation should be avoided
 - Albert Einstein
  13. 13. Objects should be responsible for their own internal consistency - Barack Obama
  14. 14. Best Solution ? ● Single Responsibility Principle ● Duplicate code
  15. 15. Design by contract
  16. 16. Value Object ● Small Objects ● Equality based on value ● Immutable
  17. 17. Example Value Objects • Money • Currency • Time • DateRange / Period
  18. 18. Money Value Object https://github.com/mathiasverraes/money
  19. 19. Entity Object ● Usually big(-ger) ● Equality based on Identity ● Mutable ● Can change over time
  20. 20. Immutability ● Simplifies code ● Less cognitive load ● Guaranteed consistent ● Referencing without side effects ● Easy to test ● Caching (profit!)
  21. 21. Immutable Object, Best Object - The Hulk
  22. 22. “A Customer should have a valid email address and a password”
  23. 23. Make the Implicit, Explicit !
  24. 24. Assertion Library https://github.com/beberlei/assert
  25. 25. “As an Author I want to be able to Create an Article. Before Publishing it, I want to have a Draft. I also would like to be able to Retract an Article with a Reason”
  26. 26. Objects as dumb data stores a.k.a. Anemic Domain Model
 This is our industry standard
  27. 27. Published ?
  28. 28. Objects should have behaviour attached 
 to them - Lex Luther
  29. 29. Objects should tell a tale about its current state and all the possible ways to change that state - Bruce Lee
  30. 30. Schrödinger's cat
  31. 31. UnpaidOrder PaidOrder
  32. 32. “Sounds like a State Machine” - Igor Wiedler
  33. 33. Immutability but of a different kind
  34. 34. UnpublishedArticle PublishedArticle
  35. 35. UnpublishedArticle PublishedArticle Draft Article
  36. 36. Draft Article RetractedArticle
  37. 37. Objects should only expose methods relevant to it's current state - George Bush
  38. 38. Objects should … • Be responsible for their own internal consistency • Have specific behaviour towards its responsibility • Broadcast their state • Only care about the state it’s currently in.
  39. 39. But there is still a piece of the puzzle missing…
  40. 40. As a Publisher I want to know which Article has been retracted more than X times
  41. 41. As an Editor I want to see all the changes that have been made
  42. 42. Store all the Changes (Δ delta’s),that have happened
  43. 43. EventSourcing! But that is a talk on its own
  44. 44. Magic is bad, hmmmkay - Saddam Hussein
  45. 45. Questions? https://joind.in/event/nijmegenphp-201504

×