In search of good design...
// When I wrote this code, only God and I understood what I was doing// Now, God only knows
Monday, October 8, 12
ABOUT ME
TWITTER.COM/FAUST45GITHUB.COM/FAUST45
Monday, October 8, 12
Goal of program convert input to reasonable output.
We calculate something and storing temp results in vars, until we can produce complete result.
What is programing about?
Monday, October 8, 12
What is programing about?
Calculates Calculates Calculates
temp results
temp results
complete result
Monday, October 8, 12
What is programing about?
Calculates Calculates Calculates
temp results
temp results
complete result
Program State
Monday, October 8, 12
What is programing about?
Calculates Calculates Calculates
temp results
temp results
complete result
Program State
Layer control order of calculations
Monday, October 8, 12
What is programing about?
Calculates Calculates Calculates
temp results
temp results
complete result
Program State
Layer manages access to program state
Layer control order of calculations
Monday, October 8, 12
Looks simple?
Calculates Calculates Calculates
temp results
temp results
complete result
Program State
Layer manages access to program state
Layer control order of calculations
Monday, October 8, 12
Lets add some complexity
Monday, October 8, 12
Lets make control order layer hard to understand
Monday, October 8, 12
Let’s split control order layer
and hide in Calculate units
Monday, October 8, 12
Who can help us?
Monday, October 8, 12
GOTO
Monday, October 8, 12
Lets do the same with state manages layer
Monday, October 8, 12
Lets makestate manages layer hard to understand
Monday, October 8, 12
Lets split state manages layer
and hide in Calculate units
Monday, October 8, 12
Example
Monday, October 8, 12
Example
Will never happens
Monday, October 8, 12
Happy debugging suckers
Monday, October 8, 12
Any part of the program can rewrite state, and it's not easy to
tell where it's happening.
Monday, October 8, 12
Unit tests don’t save us
Unit test passed success.
Only when you run all program you can touch the bug
Monday, October 8, 12
Thats most popular programing style, for
today.Imperative
Monday, October 8, 12
Where the answer?
Monday, October 8, 12
Functional Programming
Monday, October 8, 12
Functional model
Calculates Calculates Calculates
temp results
temp results
complete result
Program State
Layer manages access to program state
Layer control order of calculations
Monday, October 8, 12
You construct program from units of pure
calculations
Monday, October 8, 12
Units of pure calculations have no direct access to
program state
Monday, October 8, 12
You pass a pice of state in params
function send_money(sender_account, receiver_account, amount_to_send)
Monday, October 8, 12
and return result of calculations, some new pice of state
function send_money(sender_account, ...) return how_sender_account_looks_after_money_was_send
Monday, October 8, 12
Calculations depends only from params you passed,
nothing hidden
Monday, October 8, 12
Calculate
Pice of program state
return result
Program state
Functional
Monday, October 8, 12
Calculate
Pice of program state
return result
Program state
Imperative
direct access
Monday, October 8, 12
Rich Hickey
Creator of clojure, language with immutable data by default.
Dialect of Lisp on top of Java Virtual Machine
He was inspired by paper “Out of the Tar Pit”
Monday, October 8, 12
Model where you don’t rewrite values but
produce new values
Monday, October 8, 12
Clojure introduce fast immutable data
structures
Monday, October 8, 12
Implemented with Binary trees
and structure sharing
Monday, October 8, 12
Monday, October 8, 12
Mutable data
Monday, October 8, 12
Resources
★ “Out of the Tar Pit”★ “The Art of Unix Programming”★ “Functional programming is a ghetto” (blog bost by Michael O.Church)★ Rich Hickey videos★ Stuart Holloway videos - “Simplicity Ain't Easy”
Monday, October 8, 12