sparksp / elm-review-always / NoAlways

rule : Review.Rule.Rule

Forbid the use of always.

config : List Rule
config =
    [ NoAlways.rule
    ]

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 ]

When (not) to use this rule

If you are in a team then other members may have strong opinions about always - make sure that everyone is on board before you decide to adopt this 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 ]