Elm Conf 2017: Solving the Boolean Identity Crisis

Elm Conf 2017: Solving the Boolean Identity Crisis

94bd558238b69c45d3d3e15797ae94f7?s=128

Jeremy Fairbank

September 28, 2017
Tweet

Transcript

  1. 4.
  2. 5.
  3. 7.
  4. 11.
  5. 13.

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

    error: true, errorMessage: 'Uh oh!', } ¯\_(ツ)_/¯ Invalid State
  6. 14.

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

    error: true, errorMessage: 'Uh oh!', } ¯\_(ツ)_/¯ Invalid State
  7. 15.

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

    } else if (props.success) { ... } else { ... }
  8. 19.

    – Robert Harper “There is no information carried by a

    Boolean beyond its value, and that’s the rub.”
  9. 23.

    ?

  10. 27.

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

    It’s raining. Conclusion: It’s cloudy. Propositional Logic
  11. 28.

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

    It’s raining. Conclusion: It’s cloudy. PROPOSITIONS
  12. 30.

    Boolean ≠ Proposition A proposition, p, that is true is

    not the same as saying p is equal to true.
  13. 33.
  14. 38.
  15. 41.

    – Uncle Bob (Robert Martin) “Boolean arguments loudly declare that

    the function does more than one thing. They are confusing and should be eliminated.”
  16. 44.
  17. 49.

    bookFlight city isPremiumCustomer hasCheckedLuggage preferWindow = let luggageCost = if

    hasCheckedLuggage then ... else ... in if isPremiumCustomer then if hasCheckedLuggage then ... else ... else if hasCheckedLuggage then ... else ...
  18. 50.
  19. 51.

    – 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.”
  20. 53.
  21. 61.

    type Angle = Degrees Float | Radians Float rotateFuelRod fuelRod

    (Degrees 30) rotateControlRod controlRod (Radians pi)
  22. 62.

    rotateFuelRod fuelRod angle = case angle of Degrees amount ->

    ... Radians amount -> ... rotateControlRod controlRod angle = case angle of Degrees amount -> ... Radians amount -> ...
  23. 67.

    rotate rod angle = case angle of Degrees amount ->

    rotateInDegrees rod amount Radians amount -> rotateInRadians rod amount
  24. 70.
  25. 71.

    time = if doYouKnowTheTime person then tellMeTheTime person else """

    Does anybody really know what time it is? """
  26. 72.

    time = if doYouKnowTheTime person then tellMeTheTime person else """

    Does anybody really know what time it is? """
  27. 73.

    time = if doYouKnowTheTime person then tellMeTheTime person else """

    Does anybody really know what time it is? """
  28. 74.

    time = if doYouKnowTheTime person then tellMeTheTime person else """

    Does anybody really know what time it is? """
  29. 77.

    – Conor McBride “To make use of a Boolean you

    have to know its provenance so that you can know what it means.”
  30. 80.

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

    `${name} is a ${dogs[name].breed}`; } else { return 'Heck! No pupper found.'; } }
  31. 81.

    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."
  32. 82.

    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."
  33. 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."
  34. 84.

    canDivide numerator denominator = denominator /= 0 divisionResult numerator denominator

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

    canDivide numerator denominator = denominator /= 0 divisionResult numerator denominator

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

    divisionResult numerator denominator = if canDivide numerator denominator then "The

    result is " ++ toString (numerator / denominator) else "The result is " ++ toString (numerator / denominator)
  37. 89.

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

    dog -> name ++ " is a " ++ dog.breed Nothing -> "Heck! No pupper found."
  38. 92.

    type TellTime = Time String | NoWatch | NoPhone |

    NoSundial case whatTimeIsIt person of Time time -> ... NoWatch -> ... NoPhone -> ... NoSundial -> ...
  39. 93.

    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
  40. 95.

    { fetching = False , success = True , dog

    = Just { name = "Tucker" } , error = False , errorMessage = "" } Back to State
  41. 96.
  42. 98.

    { fetching = True , success = True , dog

    = { name = "Tucker" } , error = True , errorMessage = "Uh oh!" }
  43. 101.

    type RemoteDoggo = Ready | Fetching | Success Dog |

    Error String type alias Model = { dog : RemoteDoggo , ... }
  44. 102.

    type RemoteDoggo = Ready | Fetching | Success Dog |

    Error String type alias Model = { dog : RemoteDoggo , ... }
  45. 103.

    type RemoteDoggo = Ready | Fetching | Success Dog |

    Error String type alias Model = { dog : RemoteDoggo , ... }
  46. 104.

    type RemoteDoggo = Ready | Fetching | Success Dog |

    Error String type alias Model = { dog : RemoteDoggo , ... }
  47. 105.

    { dog = Ready, ... } { dog = Fetching,

    ... } { dog = Success { name = "Tucker" }, ... } { dog = Error "Uh oh!", ... }
  48. 106.

    view : Model -> Html Msg view model = case

    model.dog of Ready -> viewFindDog model Fetching -> viewSpinner Success dog -> viewDog dog Error error -> viewError error
  49. 107.

    view : Model -> Html Msg view model = case

    model.dog of Ready -> viewFindDog model Fetching -> viewSpinner Success dog -> viewDog dog -- Error error -> viewError error
  50. 108.

    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!
  51. 112.

    type alias Todo = { isComplete : Bool , isEditing

    : Bool } type alias Customer = { isPlatinum : Bool , isGold : Bool , isSilver : Bool }
  52. 114.

    type AccountType = Platinum | Gold | Silver type alias

    Customer = { accountType : AccountType }
  53. 121.
  54. 123.
  55. 132.