Slide 1

Slide 1 text

Jeremy Fairbank @elpapapollo / jfairbank

Slide 2

Slide 2 text

Software is broken. We are here to fix it. Say [email protected]

Slide 3

Slide 3 text

+ Follow @elpapapollo for future updates

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

{ ? }

Slide 6

Slide 6 text

{ dog: null, }

Slide 7

Slide 7 text

FETCH?

Slide 8

Slide 8 text

{ fetching: true, dog: null, }

Slide 9

Slide 9 text

SUCCESS?

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

ERRORS?

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

STATE fetching success error

Slide 17

Slide 17 text

PRIMITIVE OBSESSION USING COMIC SANS IS KINDA LIKE PRIMITIVE OBSESSION

Slide 18

Slide 18 text

The Problem with Booleans…

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

binary data type with no inherent meaning boolean

Slide 21

Slide 21 text

bookFlight "St. Louis" True ?

Slide 22

Slide 22 text

? bookFlight "St. Louis" True False True ? ?

Slide 23

Slide 23 text

?

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Propositional Logic

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

LOSS OF INTENT

Slide 32

Slide 32 text

LOSS OF INFORMATION

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

bookFlight "St. Louis" True ?

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

rotateFuelRod fuelRod 30 True rotateControlRod ?

Slide 43

Slide 43 text

rotateFuelRod fuelRod 30 True rotateControlRod controlRod 30 True

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

? bookFlight "St. Louis" True False True ? ?

Slide 49

Slide 49 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 50

Slide 50 text

No content

Slide 51

Slide 51 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 52

Slide 52 text

Write code for humans Not computers

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

bookFlight "St. Louis" "Premium"

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

bookFlight "St. Louis" "premium" bookFlight "St. Louis" "economical" bookFlight "St. Louis" "" Primitive Obsession Revisited

Slide 58

Slide 58 text

How do we represent a finite domain in Elm?

Slide 59

Slide 59 text

type CustomerType = Premium | Regular bookFlight "St. Louis" Premium bookFlight "St. Louis" Regular

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

rotateInDegrees fuelRod 30 rotateInRadians controlRod pi

Slide 66

Slide 66 text

rotateInDegrees fuelRod 30 rotateInRadians controlRod pi Still encourages primitive obsession

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

Make APIs UNDERSTANDABLE and CONVENIENT

Slide 69

Slide 69 text

Function True False

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

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

Slide 76

Slide 76 text

Boolean Blindness

Slide 77

Slide 77 text

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

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

PROVENANCE

Slide 80

Slide 80 text

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

Slide 81

Slide 81 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 82

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

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

Slide 85

Slide 85 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 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

Alternative Return Values

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 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 94

Slide 94 text

See with Union Types

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

https://youtu.be/IcgmSRJHu_8

Slide 100

Slide 100 text

STATE fetching success error

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

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

Slide 105

Slide 105 text

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

Slide 106

Slide 106 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 107

Slide 107 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 108

Slide 108 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 109

Slide 109 text

package.elm-lang.org/packages/krisajenkins/remotedata/latest type RemoteData e a = NotAsked | Loading | Failure e | Success a

Slide 110

Slide 110 text

package.elm-lang.org/packages/krisajenkins/remotedata/latest type RemoteData e a = NotAsked | Loading | Failure e | Success a

Slide 111

Slide 111 text

package.elm-lang.org/packages/krisajenkins/remotedata/latest type RemoteData e a = NotAsked | Loading | Failure e | Success a

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/elm-bool