My class is only getters and setters and fails E11014 - what should it be?
← Code Quality · Ref: Q2291
If a class is a bag of fields with a read method and a write method for each one, every field is its own group (read and write of one field touch only that field): nine fields are nine groups and E11014. It is the metric telling you the truth - there is no behaviour holding the class together - and the answer is a record.
A FAILING FORM
ContactDetails as a class with nine String fields, each with readX() and writeX(), fails with:
E11014: 'ContactDetails' on line 6 position 4: LCOM4 of 9 exceeds maximum of 8 for CLASS: low cohesion - class has disconnected method groups, consider splitting
THE FIX
A record exists to carry data. It cannot have methods, so it has no method groups and is exempt from the LCOM4 limit (so are traits, functions and field-less classes). Declare the fields once, give the record a constructor, 'default operator', and put real behaviour in a small class that uses it (ContactLabel below, LCOM4 = 1). Callers read the record's fields directly, so no accessors are needed, and call the constructor with named arguments when it has four or more.
If the data really does have behaviour of its own, keep a class but give it methods that use its fields together, and split off the rest (Q2288).
See Q761 for the limit.
Example
#!ek9 defines module qa.codequality.contactrecord defines record <?- Pure data: nine values carried together. A record has no methods, so there are no method groups to count and no LCOM4 limit applies. -?> ContactDetails name String: "" email String: "" phone String: "" street String: "" city String: "" postcode String: "" country String: "" language String: "" currency String: "" ContactDetails() -> name as String email as String phone as String street as String city as String postcode as String country as String language as String currency as String this.name: name this.email: email this.phone: phone this.street: street this.city: city this.postcode: postcode this.country: country this.language: language this.currency: currency default operator defines class <?- The behaviour lives in a small class that uses the record. Its one method reads the one property: LCOM4 = 1. -?> ContactLabel contact as ContactDetails: ContactDetails() ContactLabel() -> details as ContactDetails contact: details oneLine() as pure <- rtn as String: `${contact.name}, ${contact.city}, ${contact.country}` default operator ? defines program ContactRecordDemo() stdout <- Stdout() details <- ContactDetails(name: "Ada Lovelace", email: "ada@example.org", phone: "0123", street: "1 High St", city: "London", postcode: "N1", country: "UK", language: "en", currency: "GBP") stdout.println(ContactLabel(details).oneLine())
Common mistakes
E50001 — The argument names a variable that was never declared, so it cannot be resolved, triggering E50001. See ek9 -h E50001 for details.
Incorrect:
ContactLabel(detailsXYZ).oneLine()
Correct:
ContactLabel(details).oneLine()
Other ways to ask this
- class with only accessors fails low cohesion
- data holder class E11014
- how do I carry nine values together without LCOM4 failing
- should I use a record instead of a class for data
- are records exempt from the cohesion limit
Coming from another language?
Java: a POJO with getters and setters (or a Lombok @Data class) passes the compiler and most linters; records arrived in Java 16 for the same purpose. C#: a class of properties, or a record. Kotlin: data class. EK9: a data-only class does not compile past 8 fields; use a record.
Keywords: E11014, LCOM4, POJO, accessors, carry data, cohesion, data class, getters, record, setters