stil4m/elm-analyse

Check: Single field records

Closed this issue · 3 comments

A record with one field does not have any meaning. For example:

type alias Model =
    { thing : Thing }

could easily be refactored to

type alias Model =
    Thing

-- or maybe better

type Model
    = Model Thing

If you have multiple fields in the future. Just refactor it then. Elm will allow you to refactor that easily.

mk12 commented

This lint feels too restrictive to me.

Here are two examples where I think a single-field record is useful:

  1. Records can be used as a lightweight named-parameter syntax, improving type safety and readability while avoiding creating lots of bespoke types. For example, you could have a helper function:
onChange : { preventDefault : Bool } -> msg -> Attribute msg
-- usage:              input [ onChange { preventDefault = False } UpdateFoo ] ...
-- more readable than:        onChange False UpdateFoo
  1. You might want a single-field record for consistency. Below is an example of some types I had. Even though OutputState happens to only have one field, it's more pleasing and symmetrical to treat it similarly to InputState.
type alias Model =
    { ...
    , inputMap : Dict String InputState
    }
type alias InputState =
    { ...
    , outputMap : Dict String OutputState
    }
type alias OutputState =
    { options: Options
    }

Agree with @mk12 on the naming parameters and symmetry in consistency

Adding my own examples which I encountered this:

type alias APIParamsRegister = {
  firstName: String,
  lastName: String,
  email: String,
  password: String
}

type alias APIParamsEmailActivation = {
  token: String
}

I am turning off this rule in my own work.

I too need it for decoding a single value:

type alias Expire =
    { expire : Int }


decodeExpireFromClaim : Decoder Expire
decodeExpireFromClaim =
    Decode.succeed Expire
        |> required "expire" Decode.int