NoSingleFieldRecord

rule : Rule

Reports records containing only a single field.

config =
    [ NoSingleFieldRecord.rule
    ]

Fail

type alias SingleFieldRecord =
    { foo : String }

singleFieldRecord : String -> { foo : String }
singleFieldRecord foo =
    { foo = foo }

foo : { r | foo : String } -> String
foo r =
    r.foo

Success

type alias MultipleFieldRecord =
    { foo : String
    , bar : Int
    }

When (not) to enable this rule

Using a record is obsolete if you only plan to store a single field in it. However, there are times when a single field may be desirable, for example:

  • Rapid development, when you're planning ahead and trying out data models.
  • When you're refactoring to or from a record you may need to step through a single record.
  • To match the pattern of similar data types and to make a predictable API.
  • When you're working with generic records matching only one field, e.g., { r | some : String }. Although, these should be refactored to take one field.

Try it out

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

elm-review --template sparksp/elm-review-rules-to-avoid/preview --rules NoSingleFieldRecord