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

Post on 18-Jul-2015

141 views 1 download

Tags:

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

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

“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 Objecthttps://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 test

● Caching (profit!)

Immutable Object, Best Object

- The Hulk

“A Customer should have a valid email address

and a password”

Make the Implicit, Explicit !

Assertion Libraryhttps://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 be able

to Retract an Article with a Reason”

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

Immutabilitybut of a different kind

UnpublishedArticle PublishedArticle

UnpublishedArticle PublishedArticleDraft 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

• Broadcast their state

• Only care about the state it’s currently in.

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