Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
Copyright © 2018 David Thomas�2
Why bother with design?
Design is what lets us change code
�3
Well designed code is easier to change
than poorly designed code
�4
When faced with a coding decision, choose
the path that makes future change easier
�5
Decoupling
http://happypontist.blogspot.com/2014/06/sydney-harbour-bridge.html�6
S O L I D
ingle Responsibilitypen/closediskov substitutionnterface separationependency inversion
�7
But…
design principles are
misunderstood
(just like design patterns)
�8
So…
Don’t sweat the
principles
�9
When faced with a coding decision, choose
the path that makes future change easier
�10
Copyright © 2018 David Thomas�11
Why bother with design?
How can we remember to design?
HabitWe first make our habits and then our habits make us. – unknown
All habits gather, by unseen degrees, as brooks make rivers, rivers run to seas. – Dryden
�12
My HabitBefore coding something
1. write single sentence–same level of abstraction
2. clauses (commas, and, then, …)
3. split and repeat
4. discover state
deliverable component
module function
data structure
�13
State
functioninput output
• stateless• pure transformation
�14
Statefunction
state
• stateful
• state is opaque and local
• stateful
• state is opaque and local
• yes, it’s an object�15
The Essence of OO
�16
�17
Simula
�18
SimulaOriginal idea of passive customers moving through a
network of active components (activity approach) became
a limiting factor. Other way round possible - active
customers moving through a passive network. This
resulted in a unifying general process concept:
System = set of interacting quasiparallel processes.
1964
�19
set of interacting quasiparallel processes.
�20
Class Rectangle (Width, Height); Real Width, Height; Begin Real Area, Perimeter; Procedure Update; Begin Area ":= Width * Height; Perimeter ":= 2*(Width + Height) End of Update; Boolean Procedure IsSquare; IsSquare ":= Width=Height; Update; OutText("Rectangle created: "); OutFix(Width,2,6); OutFix(Height,2,6); OutImage End of Rectangle;
http://staff.um.edu.mt/jskl1/talk.html�21
Class Rectangle (Width, Height); Real Width, Height; Begin Real Area, Perimeter; Procedure Update; Begin Area ":= Width * Height; Perimeter ":= 2*(Width + Height) End of Update; Boolean Procedure IsSquare; IsSquare ":= Width=Height; Update; OutText("Rectangle created: "); OutFix(Width,2,6); OutFix(Height,2,6); OutImage End of Rectangle;
http://staff.um.edu.mt/jskl1/talk.html
parameters used to instantiate instances of
this class
�22
Class Rectangle (Width, Height); Real Width, Height; Begin Real Area, Perimeter; Procedure Update; Begin Area ":= Width * Height; Perimeter ":= 2*(Width + Height) End of Update; Boolean Procedure IsSquare; IsSquare ":= Width=Height; Update; OutText("Rectangle created: "); OutFix(Width,2,6); OutFix(Height,2,6); OutImage End of Rectangle;
http://staff.um.edu.mt/jskl1/talk.html
instance variables
�23
Class Rectangle (Width, Height); Real Width, Height; Begin Real Area, Perimeter; Procedure Update; Begin Area ":= Width * Height; Perimeter ":= 2*(Width + Height) End of Update; Boolean Procedure IsSquare; IsSquare ":= Width=Height; Update; OutText("Rectangle created: "); OutFix(Width,2,6); OutFix(Height,2,6); OutImage End of Rectangle;
http://staff.um.edu.mt/jskl1/talk.html
instance method
instance function
�24
Class Rectangle (Width, Height); Real Width, Height; Begin Real Area, Perimeter; Procedure Update; Begin Area ":= Width * Height; Perimeter ":= 2*(Width + Height) End of Update; Boolean Procedure IsSquare; IsSquare ":= Width=Height; Update; OutText("Rectangle created: "); OutFix(Width,2,6); OutFix(Height,2,6); OutImage End of Rectangle;
http://staff.um.edu.mt/jskl1/talk.html
lifecycle code
�25
Lifecycle Code
• Here used to initialize the instance (like a constructor would)
• But commonly continues to run alongside instance in pseudo-parallel to other instances
(because simulation language)
�26
Simula Instances
• Are created by factories (classes)
• Have per-instance state
• Have methods and functions that operate on that state
• Have an pseudo-independent execution existence
�27
Smalltalk
�28
Smalltalk
• Inspired by Simula, Lisp, and others
• Objects are little independent computers that communicate by sending each other messages
• 1st class objects
�29
The mental image was one of separate computers
sending requests to other computers that had to be
accepted and understood by the receivers before
anything could happen. In today's terms every
object would be a server…
The Early History Of Smalltalk Alan C. Kay Apple Computer [email protected]#
HOPL-II/4/93/MA, USA © 1993 ACM 0-89791-571-2/93/0004/0069
�30
The mental image was one of separate computers
sending requests to other computers that had to be
accepted and understood by the receivers before
anything could happen. In today's terms every
object would be a server…
The Early History Of Smalltalk Alan C. Kay Apple Computer [email protected]#
HOPL-II/4/93/MA, USA © 1993 ACM 0-89791-571-2/93/0004/0069
�31
The last thing you wanted any programmer to do is
mess with internal state even if presented
figuratively. Instead, the objects should be
presented as sites of higher level behaviors more
appropriate for use as dynamic components.
The Early History Of Smalltalk Alan C. Kay Apple Computer [email protected]#
HOPL-II/4/93/MA, USA © 1993 ACM 0-89791-571-2/93/0004/0069
�32
Smalltalk Instances
• Are created by factories (objects of #Object)
• Have per-instance state
• Have functions that operate on that state
• Should be considered to be independent processes
�33
C++
�34
C++• Single-handedly influenced the next 20 years of
programming language development
• Classes are code, instances are allocated memory
• Not just inheritance; multiple inheritance
• Class-orientation; objects are second class
�35
Honorable Mentions
• Beta
• Ruby
• JavaScript
• and a secret surprise…
�36
The Tao ofObject Orientation
work is done by independent, opaque, processing units that communicate by passing each
other messages
道
�37
Functional Programming
�38
Functional Programming
• Origins in lambda calculus (Church, 1930s)
• Elements in early Lisp systems (1950s)
• ML (1973) -> OCaml -> F# / Reason
�39
Functional Programming• 1980s saw resurgence in research around program
proving
• refinement of type systems
• lazy/non-strict evaluation
• Miranda -> Haskell
�40
Functional Programming
• immutability
• 1st class, pure functions
�41
Immutability
• values never change
• changed values are never shared
implementation detail
design paradigm
�42
1st Class Functions
• Functions are values
• (in lambda calculus they’re the only values)
• Program is a composition of functions
�43
The Tao ofFunctional Programming
immutable data transformed by pure functions, often in
the presence of an algebraic type system and lazy
evaluation
道 apparently
�44
The Great Conflict
�45
immutable data transformed by pure
functions, often in the presence of an
algebraic type system
independent, opaque, processing units that communicate by passing each other messages
OO FP— vs —
�46
Except…
�47
immutable data transformed by pure
functions, often in the presence of an
algebraic type system
independent, opaque, processing units that communicate by passing each other messages
OO FP
�48
OO FPnon shared state
transformed by functionsinvoked by message
passing
�49
Elixir
�50
ElixirNot an OO language
Not (really) a functional language
(Joe calls Erlang Concurrency-Oriented)
�51
ElixirNot an OO language
Not (really) a functional language
(Joe calls Erlang Concurrency-Oriented)
NO MORE LABELS�52
Elixir
Is just a programming language and environment.
Let’s steal the best ideas from OO and FPlearn from
�53
non shared statetransformed by functions
invoked by message passing
OO FP
�54
non shared statetransformed by functions
invoked by message passing
OK, Functions ᴙ Us
�55
non shared statetransformed by functions
invoked by message passing
smells like a process to me
�56
Processes Unify Functional
and OO Code
�57
Actors Unify Functional
and OO Code
�58
CSP Unifies Functional
and OO Code
�59
So I No Longer Care
• it’s just coding
• sometimes with state
• sometimes without
�60
My HabitBefore coding something
1. write single sentence–same level of abstraction
2. clauses (commas, and, then, …)
3. split and repeat
4. discover state
�61
�62
X X X X
�63
XX
XX
�64
XX
XX
�65
“Web app that waits for two players to join, then mediates
a game of 4⨉4⨉4 naughts and crosses between them”
�66
1. write single sentence–same level of abstraction
2. clauses (commas, and, then, …) 3. split and repeat 4. discover state
MatchMaker Controller
Game Controller
Player Pair
View View
Client JavaScript
Game Model
rails new sixty_four mix phx.new sixty_four
�67
�68
“Web app waits for two players to join, then mediates
a game of 4⨉4⨉4 naughts and crosses between them”
1. write single sentence–same level of abstraction
2. clauses (commas, and, then, …) 3. split and repeat 4. discover state
�69
“Web app waits for two players to join, then mediates a game of 4⨉4⨉4
naughts and crosses between them”
MatchMaker GameWeb apps
Not
hing
sh
ared
�70
MatchMaker
“when a player arrives make them wait until the next player arrives, then pass the two to the game”
1. write single sentence–same level of abstraction
2. clauses (commas, and, then, …) 3. split and repeat 4. discover state
�71
MatchMaker
“when a player arrives make them wait until the next player arrives, then pass the two to the game”
let player join
pass to game
1. write single sentence–same level of abstraction
2. clauses (commas, and, then, …) 3. split and repeat 4. discover state
waiting room
�72
MatchMaker
“when a player arrives make them wait until the next player arrives, then pass the two to the game”
let player join
pass to game
1. write single sentence–same level of abstraction
2. clauses (commas, and, then, …) 3. split and repeat 4. discover state
pending player
waiting room
�73
let player join
(web UI)
pass to game
pending player
waiting room
�74
pubsub / events
Copyright © 2018 David Thomas
and so on…
�75
Before coding something
1. write single sentence–same level of abstraction
2. clauses (commas, and, then, …)
3. split and repeat
4. discover state
�76
The Goal
Maximize cohesion
Minimize coupling
�77
The Goal
Make it easy to change
�78
The Goal
Stop using labels
�79
The Goal
Use the past (but don’t be a slave to it)
�80
The Goal
Have fun!
�81