Simplify Messages

The update function suffers from code duplication and unnecessary complexity. In this section, you will simplify it by reducing the messages it handles. You will learn how to collapse multiple message values into one parameterized message value.

Look for the update function near the bottom of SaladBuilder.elm. This function is over 100 lines, so we have a problem.

 update : ​Msg​ -> ​Model​ -> ( ​Model​, ​Cmd​ ​Msg​ )
 update msg model =
 case​ msg ​of
 SelectLettuce​ ->
  ( { model | base = ​Lettuce​ }
  , ​Cmd​.none
  )
 -- other branches

Notice the duplication where we handle the ToggleTomatoes, ToggleCucumbers, and ToggleOnions messages. Surprisingly, we have duplication with the other salad-related messages for ...

Get Programming Elm now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.