Skip to content

sparksp/elm-review-always

Repository files navigation

elm-review-always

elm package elm-review 2.0 elm 0.19 Tests

Provides an elm-review rule to forbid the use of always.

Use an anonymous function \_ -> instead of always.

It's more concise, more recognizable as a function, and makes it easier to change your mind later and name the argument.

-- Don't do this --
List.map (always 0) [ 1, 2, 3, 4 ]

-- Instead do this --
List.map (\_ -> 0) [ 1, 2, 3, 4 ]

Example configuration

import NoAlways
import Review.Rule exposing (Rule)


config : List Rule
config =
    [ NoAlways.rule
    ]

Caution: Heavy Computation

If the value you always want is the result of some heavy computation then you will not want that within an anonymous function as the work will be done every time. Instead, do the calculation in a nearby let..in block first.

-- Don't do this --
List.map (always (heavyComputation arg1 arg2)) [ 1, 2, 3, 4 ]

-- Don't do this either --
List.map (\_ -> heavyComputation arg1 arg2) [ 1, 2, 3, 4 ]

-- Instead do this --
let
    heavyComputationResult =
        heavyComputation arg1 arg2
in
List.map (\_ -> heavyComputationResult) [ 1, 2, 3, 4 ]

-- This works too (but is less readable) --
List.map ((\value _ -> value) (heavyComputation arg1 arg2)) [ 1, 2, 3, 4 ]

Try it out

You can try the example configuration above out by running the following command:

elm-review --template sparksp/elm-review-always/example