81
Copyright © 2018 David Thomas Transforming Programming 1 Dave Thomas [email protected] @pragdave

Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Copyright © 2018 David Thomas

Transforming Programming

�1

Dave [email protected]

@pragdave

Page 2: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Copyright © 2018 David Thomas�2

Why bother with design?

Page 3: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Design is what lets us change code

�3

Page 4: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Well designed code is easier to change

than poorly designed code

�4

Page 5: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

When faced with a coding decision, choose

the path that makes future change easier

�5

Page 6: Transforming Programming · 2019. 9. 23. · 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

Page 7: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

S O L I D

ingle Responsibilitypen/closediskov substitutionnterface separationependency inversion

�7

Page 8: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

But…

design principles are

misunderstood

(just like design patterns)

�8

Page 9: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

So…

Don’t sweat the

principles

�9

Page 10: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

When faced with a coding decision, choose

the path that makes future change easier

�10

Page 11: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Copyright © 2018 David Thomas�11

Why bother with design?

How can we remember to design?

Page 12: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 13: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 14: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

State

functioninput output

• stateless• pure transformation

�14

Page 15: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Statefunction

state

• stateful

• state is opaque and local

• stateful

• state is opaque and local

• yes, it’s an object�15

Page 16: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Essence of OO

�16

Page 17: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

�17

Page 18: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Simula

�18

Page 19: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 20: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

set of interacting quasiparallel processes.

�20

Page 21: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 22: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 23: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 24: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 25: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 26: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 27: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 28: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Smalltalk

�28

Page 29: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Smalltalk

• Inspired by Simula, Lisp, and others

• Objects are little independent computers that communicate by sending each other messages

• 1st class objects

�29

Page 30: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 31: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 32: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 33: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 34: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

C++

�34

Page 35: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 36: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Honorable Mentions

• Beta

• Ruby

• JavaScript

• and a secret surprise…

�36

Page 37: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Tao ofObject Orientation

work is done by independent, opaque, processing units that communicate by passing each

other messages

�37

Page 38: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Functional Programming

�38

Page 39: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Functional Programming

• Origins in lambda calculus (Church, 1930s)

• Elements in early Lisp systems (1950s)

• ML (1973) -> OCaml -> F# / Reason

�39

Page 40: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Functional Programming• 1980s saw resurgence in research around program

proving

• refinement of type systems

• lazy/non-strict evaluation

• Miranda -> Haskell

�40

Page 41: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Functional Programming

• immutability

• 1st class, pure functions

�41

Page 42: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Immutability

• values never change

• changed values are never shared

implementation detail

design paradigm

�42

Page 43: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

1st Class Functions

• Functions are values

• (in lambda calculus they’re the only values)

• Program is a composition of functions

�43

Page 44: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Tao ofFunctional Programming

immutable data transformed by pure functions, often in

the presence of an algebraic type system and lazy

evaluation

道 apparently

�44

Page 45: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Great Conflict

�45

Page 46: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 47: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Except…

�47

Page 48: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 49: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

OO FPnon shared state

transformed by functionsinvoked by message

passing

�49

Page 50: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Elixir

�50

Page 51: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

ElixirNot an OO language

Not (really) a functional language

(Joe calls Erlang Concurrency-Oriented)

�51

Page 52: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

ElixirNot an OO language

Not (really) a functional language

(Joe calls Erlang Concurrency-Oriented)

NO MORE LABELS�52

Page 53: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Elixir

Is just a programming language and environment.

Let’s steal the best ideas from OO and FPlearn from

�53

Page 54: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

non shared statetransformed by functions

invoked by message passing

OO FP

�54

Page 55: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

non shared statetransformed by functions

invoked by message passing

OK, Functions ᴙ Us

�55

Page 56: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

non shared statetransformed by functions

invoked by message passing

smells like a process to me

�56

Page 57: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Processes Unify Functional

and OO Code

�57

Page 58: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Actors Unify Functional

and OO Code

�58

Page 59: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

CSP Unifies Functional

and OO Code

�59

Page 60: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

So I No Longer Care

• it’s just coding

• sometimes with state

• sometimes without

�60

Page 61: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 62: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

�62

Page 63: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

X X X X

�63

Page 64: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

XX

XX

�64

Page 65: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

XX

XX

�65

Page 66: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

“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

Page 67: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

MatchMaker Controller

Game Controller

Player Pair

View View

Client JavaScript

Game Model

rails new sixty_four mix phx.new sixty_four

�67

Page 68: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

�68

Page 69: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

“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

Page 70: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

“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

Page 71: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 72: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 73: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

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

Page 74: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

let player join

(web UI)

pass to game

pending player

waiting room

�74

pubsub / events

Page 75: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Copyright © 2018 David Thomas

and so on…

�75

Page 76: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

Before coding something

1. write single sentence–same level of abstraction

2. clauses (commas, and, then, …)

3. split and repeat

4. discover state

�76

Page 77: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Goal

Maximize cohesion

Minimize coupling

�77

Page 78: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Goal

Make it easy to change

�78

Page 79: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Goal

Stop using labels

�79

Page 80: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Goal

Use the past (but don’t be a slave to it)

�80

Page 81: Transforming Programming · 2019. 9. 23. · When faced with a coding decision, choose the path that makes future change easier 5

The Goal

Have fun!

�81