Slide 1

Slide 1 text

Jeremy Fairbank @elpapapollo / jfairbank

Slide 2

Slide 2 text

@testdouble helps improves how the world build software. testdouble.com

Slide 3

Slide 3 text

bit.ly/programming-elm

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

elm ≈

Slide 6

Slide 6 text

{ ? }

Slide 7

Slide 7 text

{ dog: null, }

Slide 8

Slide 8 text

FETCH?

Slide 9

Slide 9 text

{ fetching: true, dog: null, }

Slide 10

Slide 10 text

SUCCESS?

Slide 11

Slide 11 text

{ fetching: false, success: true, dog: { name: 'Tucker' }, }

Slide 12

Slide 12 text

ERRORS?

Slide 13

Slide 13 text

{ fetching: false, success: false, dog: null, error: true, errorMessage: 'Ruh roh!', }

Slide 14

Slide 14 text

{ fetching: true, success: true, dog: { name: 'Tucker' }, error: true, errorMessage: 'Uh oh!', } ¯\_(ツ)_/¯ Invalid State

Slide 15

Slide 15 text

{ fetching: true, success: true, dog: { name: 'Tucker' }, error: true, errorMessage: 'Uh oh!', } ¯\_(ツ)_/¯ Invalid State

Slide 16

Slide 16 text

if (props.error) { ... } else if (props.fetching) { ... } else if (props.success) { ... } else { ... }

Slide 17

Slide 17 text

STATE fetching success error

Slide 18

Slide 18 text

PRIMITIVE OBSESSION USING COMIC SANS IS KINDA LIKE PRIMITIVE OBSESSION

Slide 19

Slide 19 text

The Problem with Booleans…

Slide 20

Slide 20 text

– Robert Harper “There is no information carried by a Boolean beyond its value, and that’s the rub.”

Slide 21

Slide 21 text

binary data type with no inherent meaning boolean

Slide 22

Slide 22 text

bookFlight "TYS" True ?

Slide 23

Slide 23 text

bookFlight "TYS" True ? Offloading abstraction to our memory instead of the code.

Slide 24

Slide 24 text

? bookFlight "TYS" True False True ? ?

Slide 25

Slide 25 text

?

Slide 26

Slide 26 text

Boolean Algebra George Boole x ∧ y x ∨ y x ∨ (y ∧ z) ¬x

Slide 27

Slide 27 text

Boolean Values True && True True || False True || (True && False) not True

Slide 28

Slide 28 text

Propositional Logic

Slide 29

Slide 29 text

Premise 1: If it’s raining then it’s cloudy. Premise 2: It’s raining. Conclusion: It’s cloudy. Propositional Logic

Slide 30

Slide 30 text

Premise 1: If it’s raining then it’s cloudy. Premise 2: It’s raining. Conclusion: It’s cloudy. PROPOSITIONS

Slide 31

Slide 31 text

prop·o·si·tion a statement that expresses a concept that can be true or false

Slide 32

Slide 32 text

Boolean ≠ Proposition A proposition, p, that is true is not the same as saying p is equal to true.

Slide 33

Slide 33 text

LOSS OF INTENT

Slide 34

Slide 34 text

LOSS OF INFORMATION

Slide 35

Slide 35 text

4

Slide 36

Slide 36 text

bookFlight "TYS" True ?

Slide 37

Slide 37 text

bookFlight city isPremiumCustomer = if isPremiumCustomer then ... else ...

Slide 38

Slide 38 text

bookFlight city isPremiumCustomer = if isPremiumCustomer then ... else ...

Slide 39

Slide 39 text

bookFlight city isPremiumCustomer = if isPremiumCustomer then ... else ... Regular customer?

Slide 40

Slide 40 text

viewOpacitySlider = viewSlider True viewVolumeSlider = viewSlider False view model = div [] [ viewOpacitySlider , viewVolumeSlider ] ?

Slide 41

Slide 41 text

viewSlider msg isHorizontal = if isHorizontal then ... else ...

Slide 42

Slide 42 text

viewSlider msg isHorizontal = if isHorizontal then ... else ... Implicit vertical slider

Slide 43

Slide 43 text

– Robert Martin “Boolean arguments loudly declare that the function does more than one thing. They are confusing and should be eliminated.”

Slide 44

Slide 44 text

rotateFuelRod fuelRod 30 True rotateControlRod ?

Slide 45

Slide 45 text

rotateFuelRod fuelRod 30 True rotateControlRod controlRod 30 True

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

rotateFuelRod fuelRod amount inDegrees = ... rotateControlRod controlRod amount inRadians = ...

Slide 48

Slide 48 text

rotateFuelRod fuelRod amount inDegrees = ... rotateControlRod controlRod amount inRadians = ...

Slide 49

Slide 49 text

rotateFuelRod fuelRod amount inDegrees = ... rotateControlRod controlRod amount inRadians = ...

Slide 50

Slide 50 text

? bookFlight "TYS" True False True ? ?

Slide 51

Slide 51 text

bookFlight city isPremiumCustomer hasCheckedLuggage preferWindow = let luggageCost = if hasCheckedLuggage then ... else ... in if isPremiumCustomer then if hasCheckedLuggage then ... else ... else if hasCheckedLuggage then ... else ...

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

– Martin Fowler “…an API should be written to make it easier for the caller, so if we know where the caller is coming from we should design the API with that information in mind.”

Slide 54

Slide 54 text

Write code for humans Not computers

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

bookFlight "TYS" "Premium"

Slide 57

Slide 57 text

bookFlight city customerType = case customerType of "Premium" -> ... "Regular" -> ... _ -> ...

Slide 58

Slide 58 text

bookFlight city customerType = case customerType of "Premium" -> ... "Regular" -> ... _ -> ...

Slide 59

Slide 59 text

bookFlight "TYS" "premium" bookFlight "TYS" "economical" bookFlight "TYS" "" Primitive Obsession Revisited

Slide 60

Slide 60 text

Represent a finite domain in Elm

Slide 61

Slide 61 text

type CustomerType = Premium | Regular bookFlight "TYS" Premium bookFlight "TYS" Regular

Slide 62

Slide 62 text

bookFlight city customerType = case customerType of Premium -> ... Regular -> ...

Slide 63

Slide 63 text

type Angle = Degrees Float | Radians Float rotateFuelRod fuelRod (Degrees 30) rotateControlRod controlRod (Radians pi)

Slide 64

Slide 64 text

rotateFuelRod fuelRod angle = case angle of Degrees amount -> ... Radians amount -> ... rotateControlRod controlRod angle = case angle of Degrees amount -> ... Radians amount -> ...

Slide 65

Slide 65 text

rotate rod angle = case angle of Degrees amount -> ... Radians amount -> ...

Slide 66

Slide 66 text

rotate rod angle = case angle of Degrees amount -> ... Radians amount -> ...

Slide 67

Slide 67 text

rotateInDegrees fuelRod 30 rotateInRadians controlRod pi

Slide 68

Slide 68 text

rotateInDegrees fuelRod 30 rotateInRadians controlRod pi Still encourages primitive obsession

Slide 69

Slide 69 text

rotate rod angle = case angle of Degrees amount -> rotateInDegrees rod amount Radians amount -> rotateInRadians rod amount

Slide 70

Slide 70 text

Make APIs UNDERSTANDABLE and CONVENIENT

Slide 71

Slide 71 text

Function True False

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

time = if doYouKnowTheTime person then tellMeTheTime person else """ Does anybody really know what time it is? """

Slide 74

Slide 74 text

time = if doYouKnowTheTime person then tellMeTheTime person else """ Does anybody really know what time it is? """

Slide 75

Slide 75 text

time = if doYouKnowTheTime person then tellMeTheTime person else """ Does anybody really know what time it is? """

Slide 76

Slide 76 text

time = if doYouKnowTheTime person then tellMeTheTime person else """ Does anybody really know what time it is? """

Slide 77

Slide 77 text

time = if doYouKnowTheTime person then tellMeTheTime person else tellMeTheTime person

Slide 78

Slide 78 text

Boolean Blindness

Slide 79

Slide 79 text

– Conor McBride “To make use of a Boolean you have to know its provenance so that you can know what it means.”

Slide 80

Slide 80 text

Get your organic, homegrown booleans from Rhode Island! Providence, RI

Slide 81

Slide 81 text

PROVENANCE

Slide 82

Slide 82 text

function findDogByName(name, dogs) { if (name in dogs) { return `${name} is a ${dogs[name].breed}`; } else { return 'Heck! No pupper found.'; } }

Slide 83

Slide 83 text

findDogByName name dogs = if Dict.member name dogs then case Dict.get name dogs of Just dog -> name ++ " is a " ++ dog.breed Nothing -> "Heck! No pupper found." else "Heck! No pupper found."

Slide 84

Slide 84 text

findDogByName name dogs = if Dict.member name dogs then case Dict.get name dogs of Just dog -> name ++ " is a " ++ dog.breed Nothing -> "Heck! No pupper found." else "Heck! No pupper found."

Slide 85

Slide 85 text

findDogByName name dogs = if Dict.member name dogs then case Dict.get name dogs of Just dog -> name ++ " is a " ++ dog.breed Nothing -> "Heck! No pupper found." else "Heck! No pupper found."

Slide 86

Slide 86 text

canDivide numerator denominator = denominator /= 0 divisionResult numerator denominator = if canDivide numerator denominator then "The result is " ++ toString (numerator / denominator) else "Could not divide"

Slide 87

Slide 87 text

canDivide numerator denominator = denominator /= 0 divisionResult numerator denominator = if canDivide numerator denominator then "The result is " ++ toString (numerator / denominator) else "Could not divide"

Slide 88

Slide 88 text

divisionResult numerator denominator = if canDivide numerator denominator then "The result is " ++ toString (numerator / denominator) else "The result is " ++ toString (numerator / denominator)

Slide 89

Slide 89 text

– Dan Licata “Boolean tests let you look, options let you see.”

Slide 90

Slide 90 text

Alternative Return Values

Slide 91

Slide 91 text

type Maybe a = Just a | Nothing

Slide 92

Slide 92 text

findDogByName name dogs = case Dict.get name dogs of Just dog -> name ++ " is a " ++ dog.breed Nothing -> "Heck! No pupper found."

Slide 93

Slide 93 text

case whatTimeIsIt person of Just time -> ... Nothing -> ...

Slide 94

Slide 94 text

case whatTimeIsIt person of Just time -> ... Nothing -> ...

Slide 95

Slide 95 text

type TellTime = Time String | NoWatch | NoPhone | NoSundial case whatTimeIsIt person of Time time -> ... NoWatch -> ... NoPhone -> ... NoSundial -> ...

Slide 96

Slide 96 text

divide numerator denominator = case denominator of 0 -> Err "Divide by zero" _ -> Ok (numerator / denominator) divisionResult numerator denominator = case divide 4 2 of Ok result -> "The result is " ++ toString result Err error -> "Could not divide: " ++ error

Slide 97

Slide 97 text

See with Custom Types

Slide 98

Slide 98 text

{ fetching = False , success = True , dog = Just { name = "Tucker" } , error = False , errorMessage = "" } Back to State

Slide 99

Slide 99 text

if model.error then ... else if model.fetching then ... else if model.success then ... else ...

Slide 100

Slide 100 text

if model.fetching then ... else if model.success then ... else ...

Slide 101

Slide 101 text

{ fetching = True , success = True , dog = { name = "Tucker" } , error = True , errorMessage = "Uh oh!" }

Slide 102

Slide 102 text

https://youtu.be/IcgmSRJHu_8

Slide 103

Slide 103 text

STATE fetching success error

Slide 104

Slide 104 text

type RemoteDoggo = Ready | Fetching | Success Dog | Error String type alias Model = { dog : RemoteDoggo , ... }

Slide 105

Slide 105 text

type RemoteDoggo = Ready | Fetching | Success Dog | Error String type alias Model = { dog : RemoteDoggo , ... }

Slide 106

Slide 106 text

type RemoteDoggo = Ready | Fetching | Success Dog | Error String type alias Model = { dog : RemoteDoggo , ... }

Slide 107

Slide 107 text

type RemoteDoggo = Ready | Fetching | Success Dog | Error String type alias Model = { dog : RemoteDoggo , ... }

Slide 108

Slide 108 text

{ dog = Ready, ... } { dog = Fetching, ... } { dog = Success { name = "Tucker" }, ... } { dog = Error "Uh oh!", ... }

Slide 109

Slide 109 text

view : Model -> Html Msg view model = case model.dog of Ready -> viewFindDog model Fetching -> viewSpinner Success dog -> viewDog dog Error error -> viewError error

Slide 110

Slide 110 text

view : Model -> Html Msg view model = case model.dog of Ready -> viewFindDog model Fetching -> viewSpinner Success dog -> viewDog dog -- Error error -> viewError error

Slide 111

Slide 111 text

This `case` does not have branches for all possibilities. 47|> case model.dog of 48|> Ready -> viewFindDog model 49|> 50|> Fetching -> viewSpinner 51|> 52|> Success dog -> viewDog dog You need to account for the following values: Main.Error _ Add a branch to cover this pattern!

Slide 112

Slide 112 text

type alias Todo = { isComplete : Bool , isEditing : Bool } type alias Customer = { isPlatinum : Bool , isGold : Bool , isSilver : Bool }

Slide 113

Slide 113 text

type TodoStatus = Complete | Editing type alias Todo = { status : TodoStatus }

Slide 114

Slide 114 text

type AccountType = Platinum | Gold | Silver type alias Customer = { accountType : AccountType }

Slide 115

Slide 115 text

UT Impossible state becomes impossible* Code becomes understandable *trademark Richard Feldman

Slide 116

Slide 116 text

But wait, There’s MORE! AS SEEN ON U I

Slide 117

Slide 117 text

Save to archive

Slide 118

Slide 118 text

Save to archive A wild checkbox appears!

Slide 119

Slide 119 text

Save to archive ✓

Slide 120

Slide 120 text

Save to archive ?

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

We design APIs for callers So we should design UIs for users

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

hu·mane having or showing compassion or benevolence

Slide 125

Slide 125 text

Save to archive ?

Slide 126

Slide 126 text

Save to archive Save to inbox

Slide 127

Slide 127 text

Save to archive Save to inbox

Slide 128

Slide 128 text

Save to archive Save to inbox Verb labels = when will it happen?

Slide 129

Slide 129 text

Saved to archive Saved to inbox Adjective labels = describe the final result.

Slide 130

Slide 130 text

Goodbye, boolean?

Slide 131

Slide 131 text

B A L A N C E

Slide 132

Slide 132 text

EMPATHY

Slide 133

Slide 133 text

Jeremy Fairbank @elpapapollo / jfairbank Slides: bit.ly/ls-elm-bool