function rotateFuelRod(fuelRod, amount, inDegrees) {
...
}
function rotateControlRod(controlRod, amount, inRadians) {
...
}
Slide 47
Slide 47 text
function rotateFuelRod(fuelRod, amount, inDegrees) {
...
}
function rotateControlRod(controlRod, amount, inRadians) {
...
}
Slide 48
Slide 48 text
function rotateFuelRod(fuelRod, amount, inDegrees) {
...
}
function rotateControlRod(controlRod, amount, inRadians) {
...
}
Slide 49
Slide 49 text
bookFlight('CLE', true, false, true)
? ? ?
Slide 50
Slide 50 text
function bookFlight(airport, isPremium, hasCheckedLuggage, preferWindow) {
const luggageCost = hasCheckedLuggage
? ...
: ...
if (isPremium) {
if (hasCheckedLuggage) {
...
} else {
...
}
} else if (hasCheckedLuggage) {
...
} else {
...
}
}
Slide 51
Slide 51 text
No content
Slide 52
Slide 52 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 53
Slide 53 text
Write code for humans
Not computers
Slide 54
Slide 54 text
No content
Slide 55
Slide 55 text
bookFlight('CLE', 'Premium')
Slide 56
Slide 56 text
function bookFlight(airport, customerType) {
switch (customerType) {
case 'Premium':
...
case 'Regular':
...
default:
...
}
}
Slide 57
Slide 57 text
function bookFlight(airport, customerType) {
switch (customerType) {
case 'Premium':
...
case 'Regular':
...
default:
...
}
}
function getTime(person) {
if (doYouKnowTheTime(person)) {
return tellMeTheTime(person)
} else {
return `
Does anybody really know
what time it is?
`
}
}
Slide 102
Slide 102 text
function getTime(person) {
if (doYouKnowTheTime(person)) {
return tellMeTheTime(person)
} else {
return `
Does anybody really know
what time it is?
`
}
}
Slide 103
Slide 103 text
function getTime(person) {
if (doYouKnowTheTime(person)) {
return tellMeTheTime(person)
} else {
return `
Does anybody really know
what time it is?
`
}
}
Slide 104
Slide 104 text
function getTime(person) {
if (doYouKnowTheTime(person)) {
return tellMeTheTime(person)
} else {
return `
Does anybody really know
what time it is?
`
}
}
Slide 105
Slide 105 text
function getTime(person) {
if (doYouKnowTheTime(person)) {
return tellMeTheTime(person)
} else {
return tellMeTheTime(person)
}
}
Slide 106
Slide 106 text
Boolean Blindness
Slide 107
Slide 107 text
– Conor McBride
“To make use of a Boolean you
have to know its provenance so
that you can know what it
means.”
Slide 108
Slide 108 text
Get your organic,
homegrown
booleans from
Rhode Island!
Providence, RI
Slide 109
Slide 109 text
PROVENANCE
Slide 110
Slide 110 text
function findDogByName(name, dogs) {
if (name in dogs) {
return `${name} is a ${dogs[name].breed}`;
} else {
return 'Heck! No pupper found.';
}
}
Slide 111
Slide 111 text
function findDogByName(name, dogs) {
if (name in dogs) {
return `${name} is a ${dogs[name].breed}`;
} else {
return `${name} is a ${dogs[name].breed}`;
}
}
Slide 112
Slide 112 text
function canDivide(denominator) {
return denominator != 0
}
function divisionResult(numerator, denominator) {
if (canDivide(denominator)) {
return `The result is ${numerator / denominator}`
} else {
return 'Could not divide'
}
}
Slide 113
Slide 113 text
function canDivide(denominator) {
return denominator != 0
}
function divisionResult(numerator, denominator) {
if (canDivide(denominator)) {
return `The result is ${numerator / denominator}`
} else {
return 'Could not divide'
}
}
Slide 114
Slide 114 text
function divisionResult(numerator, denominator) {
if (canDivide(denominator)) {
return `The result is ${numerator / denominator}`
} else {
return `The result is ${numerator / denominator}`
}
}
Slide 115
Slide 115 text
– Dan Licata
“Boolean tests let you look,
options let you see.”
Slide 116
Slide 116 text
Alternative
Return Values
Slide 117
Slide 117 text
type Maybe a
= Nothing
| Just a
Slide 118
Slide 118 text
type Maybe a
= Nothing
| Just a
Slide 119
Slide 119 text
type Maybe a
= Nothing
| Just a
Slide 120
Slide 120 text
type Maybe a
= Nothing
| Just a
Slide 121
Slide 121 text
42
Just 42
"Hi"
Just "Hi"
Slide 122
Slide 122 text
Just 42 [42]
Nothing [ ]
Slide 123
Slide 123 text
import { Maybe } from 'true-myth'
Maybe.just(42) // Just 42
Maybe.nothing() // Nothing
Maybe.of(42) // Just 42
Maybe.of(null) // Nothing
Maybe.of(undefined) // Nothing
Slide 124
Slide 124 text
function findDogByName(name, dogs) {
return Maybe.of(dogs[name])
.map(dog => `${name} is a ${dog.breed}.`)
.unwrapOr('Heck! No pupper found!')
}
Slide 125
Slide 125 text
function findDogByName(name, dogs) {
return Maybe.of(dogs[name])
.map(dog => `${name} is a ${dog.breed}.`)
.unwrapOr('Heck! No pupper found!')
}
Slide 126
Slide 126 text
Maybe.of
undefined
Maybe.of
Nothing
dog Just dog
Slide 127
Slide 127 text
function findDogByName(name, dogs) {
return Maybe.of(dogs[name])
.map(dog => `${name} is a ${dog.breed}.`)
.unwrapOr('Heck! No pupper found!')
}
Slide 128
Slide 128 text
.map
n => n * 2
Just 21
.map
n => n * 2
Just 42
Nothing Nothing
Slide 129
Slide 129 text
function findDogByName(name, dogs) {
return Maybe.of(dogs[name])
.map(dog => `${name} is a ${dog.breed}.`)
.unwrapOr('Heck! No pupper found!')
}
Slide 130
Slide 130 text
function findDogByName(name, dogs) {
return Maybe.of(dogs[name])
.map(dog => `${name} is a ${dog.breed}.`)
.unwrapOr('Heck! No pupper found!')
}
Slide 131
Slide 131 text
.unwrapOr(-1)
Just 42
.unwrapOr(-1)
42
Nothing -1
Slide 132
Slide 132 text
function findDogByName(name, dogs) {
return Maybe.of(dogs[name])
.map(dog => `${name} is a ${dog.breed}.`)
.unwrapOr('Heck! No pupper found!')
}
function App({ dog }) {
switch (dog.status) {
case RemoteDoggo.Ready:
return
case RemoteDoggo.Fetching:
return
case RemoteDoggo.Success:
return
case RemoteDoggo.Fail:
return
}
}
Slide 157
Slide 157 text
function App({ dog }) {
switch (dog.status) {
case RemoteDoggo.Ready:
return
case RemoteDoggo.Fetching:
return
case RemoteDoggo.Success:
return
case RemoteDoggo.Fail:
return
}
}
function App({ dog }: AppProps): React.ReactElement {
switch (dog.kind) {
case Status.Ready:
return
case Status.Fetching:
return
case Status.Success:
return
// case Status.Fail:
// return
}
}
Slide 160
Slide 160 text
app.tsx:20:34 - error TS2366:
Function lacks ending return statement
and return type does not include 'undefined'.
20 function App({ dog }: AppProps): React.ReactElement {
~~~~~~~~~~~~~~~~~~
Found 1 error.
Slide 161
Slide 161 text
Impossible state becomes impossible*
Code becomes understandable
*trademark Richard Feldman
Slide 162
Slide 162 text
But wait,
There’s MORE!
AS SEEN ON
U I
Slide 163
Slide 163 text
Save to archive
Slide 164
Slide 164 text
Save to archive
A wild checkbox appears!
Slide 165
Slide 165 text
Save to archive
✓
Slide 166
Slide 166 text
Save to archive
?
Slide 167
Slide 167 text
No content
Slide 168
Slide 168 text
We design APIs for callers
So we should design UIs for users
Slide 169
Slide 169 text
No content
Slide 170
Slide 170 text
hu·mane
having or showing compassion or
benevolence
Slide 171
Slide 171 text
Save to archive
?
Slide 172
Slide 172 text
Save to archive
Save to inbox
Slide 173
Slide 173 text
Save to archive
Save to inbox
Slide 174
Slide 174 text
Save to archive
Save to inbox
Verb labels = when will it happen?
Slide 175
Slide 175 text
Saved to archive
Saved to inbox
Adjective labels = describe the final result.
Slide 176
Slide 176 text
Goodbye, boolean?
Slide 177
Slide 177 text
B A L A N C E
Slide 178
Slide 178 text
EMPATHY
Slide 179
Slide 179 text
Jeremy Fairbank
@elpapapollo
Slides: bit.ly/cm-bool