jfmengels / elm-review-the-elm-architecture / NoRecursiveUpdate

rule : Review.Rule.Rule

Reports when the update function calls itself.

This is often done in order to have one message (A) trigger (all or some of) the same model updates and commands as another message (B).

update msg model =
    case msg of
        Foo ->
            { model | foo = True }

        Bar ->
            update Foo { model | bar = True }

This is advised against, because if the way that message B is handled changes, that will implicitly change how message A is handled in ways that may not have been foreseen.

A better solution is to move the common handling into a different function and have it called in the handling of both messages.

update msg model =
    case msg of
        Foo ->
            commonOperationOnModel model

        Bar ->
            commonOperationOnModel { model | bar = True }

commonOperationOnModel model =
    { model | foo = True }

Calls to other modules' update function are allowed.

To add the rule to your configuration:

config =
    [ NoRecursiveUpdate.rule
    ]

Try it out

You can try this rule out by running the following command:

elm-review --template jfmengels/elm-review-the-elm-architecture/example --rules NoRecursiveUpdate