O'Reilly logo

Programming Elm by Jeremy Fairbank

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required