Lambda Squared 2019: Solving the Boolean Identity Crisis

Lambda Squared 2019: Solving the Boolean Identity Crisis

Powerful in its simplicity, the boolean can be limiting. In this talk, we will look at examples where booleans obscure the meaning of code and make it harder to maintain. You will learn patterns such as ADTs to write clear code without booleans and gain greater empathy for your teammates and users.

94bd558238b69c45d3d3e15797ae94f7?s=128

Jeremy Fairbank

April 26, 2019
Tweet

Transcript

  1. Jeremy Fairbank @elpapapollo / jfairbank

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

  3. bit.ly/programming-elm

  4. None
  5. elm ≈

  6. { ? }

  7. { dog: null, }

  8. FETCH?

  9. { fetching: true, dog: null, }

  10. SUCCESS?

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

    }
  12. ERRORS?

  13. { fetching: false, success: false, dog: null, error: true, errorMessage:

    'Ruh roh!', }
  14. { fetching: true, success: true, dog: { name: 'Tucker' },

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

    error: true, errorMessage: 'Uh oh!', } ¯\_(ツ)_/¯ Invalid State
  16. if (props.error) { ... } else if (props.fetching) { ...

    } else if (props.success) { ... } else { ... }
  17. STATE fetching success error <ready>

  18. PRIMITIVE OBSESSION USING COMIC SANS IS KINDA LIKE PRIMITIVE OBSESSION

  19. The Problem with Booleans…

  20. – Robert Harper “There is no information carried by a

    Boolean beyond its value, and that’s the rub.”
  21. binary data type with no inherent meaning boolean

  22. bookFlight "TYS" True ?

  23. bookFlight "TYS" True ? Offloading abstraction to our memory instead

    of the code.
  24. ? bookFlight "TYS" True False True ? ?

  25. ?

  26. Boolean Algebra George Boole x ∧ y x ∨ y

    x ∨ (y ∧ z) ¬x
  27. Boolean Values True && True True || False True ||

    (True && False) not True
  28. Propositional Logic

  29. Premise 1: If it’s raining then it’s cloudy. Premise 2:

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

    It’s raining. Conclusion: It’s cloudy. PROPOSITIONS
  31. prop·o·si·tion a statement that expresses a concept that can be

    true or false
  32. Boolean ≠ Proposition A proposition, p, that is true is

    not the same as saying p is equal to true.
  33. LOSS OF INTENT

  34. LOSS OF INFORMATION

  35. 4

  36. bookFlight "TYS" True ?

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

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

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

    Regular customer?
  40. viewOpacitySlider = viewSlider True viewVolumeSlider = viewSlider False view model

    = div [] [ viewOpacitySlider , viewVolumeSlider ] ?
  41. viewSlider msg isHorizontal = if isHorizontal then ... else ...

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

    Implicit vertical slider
  43. – Robert Martin “Boolean arguments loudly declare that the function

    does more than one thing. They are confusing and should be eliminated.”
  44. rotateFuelRod fuelRod 30 True rotateControlRod ?

  45. rotateFuelRod fuelRod 30 True rotateControlRod controlRod 30 True

  46. None
  47. rotateFuelRod fuelRod amount inDegrees = ... rotateControlRod controlRod amount inRadians

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

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

    = ...
  50. ? bookFlight "TYS" True False True ? ?

  51. bookFlight city isPremiumCustomer hasCheckedLuggage preferWindow = let luggageCost = if

    hasCheckedLuggage then ... else ... in if isPremiumCustomer then if hasCheckedLuggage then ... else ... else if hasCheckedLuggage then ... else ...
  52. None
  53. – 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.”
  54. Write code for humans Not computers

  55. None
  56. bookFlight "TYS" "Premium"

  57. bookFlight city customerType = case customerType of "Premium" -> ...

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

    "Regular" -> ... _ -> ...
  59. bookFlight "TYS" "premium" bookFlight "TYS" "economical" bookFlight "TYS" "" Primitive

    Obsession Revisited
  60. Represent a finite domain in Elm

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

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

    Regular -> ...
  63. type Angle = Degrees Float | Radians Float rotateFuelRod fuelRod

    (Degrees 30) rotateControlRod controlRod (Radians pi)
  64. rotateFuelRod fuelRod angle = case angle of Degrees amount ->

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

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

    ... Radians amount -> ...
  67. rotateInDegrees fuelRod 30 rotateInRadians controlRod pi

  68. rotateInDegrees fuelRod 30 rotateInRadians controlRod pi Still encourages primitive obsession

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

    rotateInDegrees rod amount Radians amount -> rotateInRadians rod amount
  70. Make APIs UNDERSTANDABLE and CONVENIENT

  71. Function True False

  72. None
  73. time = if doYouKnowTheTime person then tellMeTheTime person else """

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

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

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

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

    person
  78. Boolean Blindness

  79. – Conor McBride “To make use of a Boolean you

    have to know its provenance so that you can know what it means.”
  80. Get your organic, homegrown booleans from Rhode Island! Providence, RI

  81. PROVENANCE

  82. function findDogByName(name, dogs) { if (name in dogs) { return

    `${name} is a ${dogs[name].breed}`; } else { return 'Heck! No pupper found.'; } }
  83. 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."
  84. 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."
  85. 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."
  86. canDivide numerator denominator = denominator /= 0 divisionResult numerator denominator

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

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

    result is " ++ toString (numerator / denominator) else "The result is " ++ toString (numerator / denominator)
  89. – Dan Licata “Boolean tests let you look, options let

    you see.”
  90. Alternative Return Values

  91. type Maybe a = Just a | Nothing

  92. findDogByName name dogs = case Dict.get name dogs of Just

    dog -> name ++ " is a " ++ dog.breed Nothing -> "Heck! No pupper found."
  93. case whatTimeIsIt person of Just time -> ... Nothing ->

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

    ...
  95. type TellTime = Time String | NoWatch | NoPhone |

    NoSundial case whatTimeIsIt person of Time time -> ... NoWatch -> ... NoPhone -> ... NoSundial -> ...
  96. 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
  97. See with Custom Types

  98. { fetching = False , success = True , dog

    = Just { name = "Tucker" } , error = False , errorMessage = "" } Back to State
  99. if model.error then ... else if model.fetching then ... else

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

    ...
  101. { fetching = True , success = True , dog

    = { name = "Tucker" } , error = True , errorMessage = "Uh oh!" }
  102. https://youtu.be/IcgmSRJHu_8

  103. STATE fetching success error <ready>

  104. type RemoteDoggo = Ready | Fetching | Success Dog |

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

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

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

    Error String type alias Model = { dog : RemoteDoggo , ... }
  108. { dog = Ready, ... } { dog = Fetching,

    ... } { dog = Success { name = "Tucker" }, ... } { dog = Error "Uh oh!", ... }
  109. view : Model -> Html Msg view model = case

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

    model.dog of Ready -> viewFindDog model Fetching -> viewSpinner Success dog -> viewDog dog -- Error error -> viewError error
  111. 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!
  112. type alias Todo = { isComplete : Bool , isEditing

    : Bool } type alias Customer = { isPlatinum : Bool , isGold : Bool , isSilver : Bool }
  113. type TodoStatus = Complete | Editing type alias Todo =

    { status : TodoStatus }
  114. type AccountType = Platinum | Gold | Silver type alias

    Customer = { accountType : AccountType }
  115. UT Impossible state becomes impossible* Code becomes understandable *trademark Richard

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

  117. Save to archive

  118. Save to archive A wild checkbox appears!

  119. Save to archive ✓

  120. Save to archive ?

  121. None
  122. We design APIs for callers So we should design UIs

    for users
  123. None
  124. hu·mane having or showing compassion or benevolence

  125. Save to archive ?

  126. Save to archive Save to inbox

  127. Save to archive Save to inbox

  128. Save to archive Save to inbox Verb labels = when

    will it happen?
  129. Saved to archive Saved to inbox Adjective labels = describe

    the final result.
  130. Goodbye, boolean?

  131. B A L A N C E

  132. EMPATHY

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