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

83
Object Invariants, Immutability & You Making Objects Do Your Bidding NijmegenPHP 15-04-2015

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

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

Object Invariants, Immutability & You

Making Objects Do Your Bidding NijmegenPHP 15-04-2015

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

Jeroen v.d. Gulik

• CTO / Co-Founder Isset (http://isset.nl)

• Builder of Artificial Stupidity

• @n0x13

• Likes Cats … a lot

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

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

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

“Every Great Talk should start with a Great Quote”

Ross Tuck

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

-House M.D.

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

Telephone Game

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

“Over time, the original intent and purpose gets

lost”- Jeroen v.d. Gulik

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

“A Customer should always have an email

address”

Page 9: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 10: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 11: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 12: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 13: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Testing shows the presence, not the absence of bugs.

- Kanye West

Page 14: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 15: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 16: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

“We should enforce (business) rules when creating objects”

- Justin Bieber

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

“A Customer should have a valid email address”

Page 18: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 19: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Outside-in validation should be avoided

- Albert Einstein

Page 20: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 21: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Objects should be responsible for their own internal consistency

- Barack Obama

Page 22: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 23: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Best Solution ?

● Single Responsibility Principle

● Duplicate code

Page 24: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 25: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 26: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Design by contract

Page 27: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 28: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Value Object

● Small Objects

● Equality based on value

● Immutable

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

Example Value Objects

• Money

• Currency

• Time

• DateRange / Period

Page 30: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 31: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Money Value Objecthttps://github.com/mathiasverraes/money

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

Entity Object

● Usually big(-ger)

● Equality based on Identity

● Mutable

● Can change over time

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

Immutability● Simplifies code

● Less cognitive load

● Guaranteed consistent

● Referencing without side effects

● Easy to test

● Caching (profit!)

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

Immutable Object, Best Object

- The Hulk

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

“A Customer should have a valid email address

and a password”

Page 36: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 37: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 38: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 39: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 40: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Make the Implicit, Explicit !

Page 41: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 42: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 43: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 44: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Assertion Libraryhttps://github.com/beberlei/assert

Page 45: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 46: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

“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”

Page 47: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 48: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Objects as dumb data stores a.k.a.

Anemic Domain Model

This is our industry standard

Page 49: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 50: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Published ?

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

Objects should have behaviour attached

to them- Lex Luther

Page 52: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 53: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 54: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 55: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Objects should tell a tale about its current state and all the possible

ways to change that state- Bruce Lee

Page 56: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 57: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 58: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 59: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Schrödinger's cat

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

UnpaidOrder PaidOrder

Page 61: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 62: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 63: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

“Sounds like a State Machine”- Igor Wiedler

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

Immutabilitybut of a different kind

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

UnpublishedArticle PublishedArticle

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

UnpublishedArticle PublishedArticleDraft Article

Page 67: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 68: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Draft Article RetractedArticle

Page 69: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 70: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Objects should only expose methods relevant to it's current

state

- George Bush

Page 71: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 72: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 73: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

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.

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

But there is still a piece of the puzzle missing…

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

As a Publisher I want to know which Article has been retracted more than X times

Page 76: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 77: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

As an Editor I want to see all the changes that have

been made

Page 78: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 79: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

Store all the Changes (Δ delta’s),that have

happened

Page 80: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP
Page 81: 2015 04-15 object invariants, immutability & you (v2) - NijmegenPHP

EventSourcing!But that is a talk on its own

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

Magic is bad, hmmmkay- Saddam Hussein

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

Questions?https://joind.in/event/nijmegenphp-201504