# Batching updates

**URL:** <https://discourse.elm-lang.org/t/batching-updates/2843>\
**Category:** Learn\
**Created:** [December 29, 2018, 11:45pm UTC](https://discourse.elm-lang.org/t/batching-updates/2843 "2018-12-29T23:45:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [December 29, 2018, 11:45pm UTC](https://discourse.elm-lang.org/t/batching-updates/2843/1 "2018-12-29T23:45:12Z")

</div>

When sending a particular `msg` to `update`, I need several things to update that all have their own `msgs` independently. I see two ways (I’m sure there are more) to accomplish this:  
Option 1:

```auto
update : Msg -> Model -> Model
update msg model =
   Foo strng -> 
       let 
           mdl = { model | foo = strng }
      in
         update (Bar "barred") mdl
  Bar strng -> 
      { model | bar = strng }

```

Option 2:

```auto
update : Msg -> Model -> (Model, Cmd msg)
update msg model =
    case msg of 
        FooBar strng1 strng2-> (model, Cmd.batch 
                                           [ Task.perform (\_ -> Foo strng1) (Task.succeed True)
                                           , Task.perform (\_ -> Bar strng2) (Task.succeed True)
                                           ] )

       Foo strng -> ({ model | foo = strng }, Cmd.none)

       Bar strng -> ({ model | bar = strng }, Cmd.none)

```

Is one preferable to the other? Using Task.perform in this way seems odd but I don’t know if it’s wrong.

---

<div class="post-metadata">

**Author:** ![andys8](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/andys8/32/280_2.png) [@andys8](https://discourse.elm-lang.org/u/andys8)\
**Post date:** [December 29, 2018, 11:53pm UTC](https://discourse.elm-lang.org/t/batching-updates/2843/2 "2018-12-29T23:53:33Z")

</div>

You might also want to take a look at `update-extra` for some syntactic sugar.

[https://package.elm-lang.org/packages/ccapndave/elm-update-extra/4.0.0/Update-Extra#sequence](https://package.elm-lang.org/packages/ccapndave/elm-update-extra/4.0.0/Update-Extra#sequence)

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [December 30, 2018, 12:03am UTC](https://discourse.elm-lang.org/t/batching-updates/2843/3 "2018-12-30T00:03:43Z")

</div>

Thanks! That looks really handy. In the source it seems that the `sequence` function ultimately boils down a list of `msgs` into a sequentially updated model with the `Cmds` not processed immediately but instead gathered into a list that is processed ultimately as `Cmd.batch [commands]`. Am I misreading that? If not, is that preferable to either of the two approaches I laid out?

---

<div class="post-metadata">

**Author:** ![Brian\_Carroll](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/brian_carroll/32/444_2.png) [@Brian\_Carroll](https://discourse.elm-lang.org/u/Brian_Carroll)\
**Post date:** [December 30, 2018, 8:25am UTC](https://discourse.elm-lang.org/t/batching-updates/2843/4 "2018-12-30T08:25:21Z")

</div>

I’ve seen this question come up several times and your Option 1 is usually what’s recommended.

With option 2 you’re unnecessarily making part of the update asynchronous. Looks safe in your simplified example but in more complex cases it might even be possible for another message to come in between your two updates. (Say a user click at the wrong time)

I believe you’re safe with the way you’re putting both updates in a single Cmd.batch here, but in real apps there are lots of ways to accidentally make the updates so they’re not guaranteed to happen together.

On the other hand option 1 makes it synchronous. One event, one message, one update. (Event meaning user click or http received or something)

---

<div class="post-metadata">

**Author:** ![jessta](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jessta/32/660_2.png) [@jessta](https://discourse.elm-lang.org/u/jessta)\
**Post date:** [December 30, 2018, 8:48am UTC](https://discourse.elm-lang.org/t/batching-updates/2843/5 "2018-12-30T08:48:15Z")

</div>

Yep, Option 1 is better than Option2.

But even better is to do:

```
doBar : String -> Model -> Model
doBar strng model = { model | bar = strng }

update : Msg -> Model -> Model
update msg model =
   Foo strng -> 
     let 
       mdl = { model | foo = strng }
     in
       doBar "barred" mdl
  Bar strng -> 
    doBar strng model
```

---

<div class="post-metadata">

**Author:** ![malaire](https://avatars.discourse-cdn.com/v4/letter/m/b782af/32.png) [@malaire](https://discourse.elm-lang.org/u/malaire)\
**Post date:** [December 30, 2018, 10:13am UTC](https://discourse.elm-lang.org/t/batching-updates/2843/6 "2018-12-30T10:13:36Z")

</div>

Could refactor also `doFoo` into a function:

```auto
doFoo : String -> Model -> Model
doFoo strng model = { model | foo = strng }

doBar : String -> Model -> Model
doBar strng model = { model | bar = strng }

update : Msg -> Model -> Model
update msg model =
  case msg of
    Foo strng -> 
      model
        |> doFoo strng
        |> doBar "barred"
    Bar strng -> 
      model
        |> doBar strng

```

---

<div class="post-metadata">

**Author:** ![cynic](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/cynic/32/1612_2.png) [@cynic](https://discourse.elm-lang.org/u/cynic)\
**Post date:** [December 30, 2018, 4:49pm UTC](https://discourse.elm-lang.org/t/batching-updates/2843/7 "2018-12-30T16:49:32Z")

</div>

Separate functions (as pointed out by @jessta and @malaire) are probably the way to go. If you don’t make them into separate functions, then you introduce an implicit dependency: when you receive a `Foo`, a `Bar` will occur via the recursive call, but so will any other logic (e.g. analytics) that also happen to be part of your `update` function. Of course, there is also the danger that as your `update` grows, you forget that `Foo` has an implicit dependency on `Bar`, and when you change `Bar`, you wonder why `Foo` isn’t working properly any more. Splitting things into separate functions will avoid both of these issues.

Lastly, consider splitting things up into different modules if it all gets too big. Then you can simply say

```auto
update : Msg -> Model -> (Model, Cmd msg)
update msg model =
   case msg of
      Foo fooMsg ->
         Foo.update fooMsg

```

… and so on. My guideline is that programming isn’t just for the computer: if it was, we’d all use assembly and be happy with it. Mostly, programming is for the programmer, since humans are so bad at keeping track of state explosions! So if you see an opportunity to split things up decently with very little performance impact, then go for it with a grin on your face and a song in your heart.

---

<div class="post-metadata">

**Author:** ![stevensonmt](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/stevensonmt/32/889_2.png) [@stevensonmt](https://discourse.elm-lang.org/u/stevensonmt)\
**Post date:** [December 30, 2018, 6:41pm UTC](https://discourse.elm-lang.org/t/batching-updates/2843/8 "2018-12-30T18:41:26Z")

</div>

In my actual project I do have things split into modules but I have a weird case where I need to do both Foo.update and Bar.update upon receipt of a msg.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [January 9, 2019, 6:50pm UTC](https://discourse.elm-lang.org/t/batching-updates/2843/9 "2019-01-09T18:50:14Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
