Coming from Java or C#: why are equals, hashCode and toString style operators ignored by cohesion, and why is low cohesion an error and not a warning?
← Code Quality · Ref: Q2293
TOSTRING-LIKE OPERATORS
In EK9 the equivalents of equals, hashCode and toString are the operators == , #? and $ (with ? for 'is set', <=> for ordering, and :=: for copy). They are part of the type's fixed operator set, they read every field by their nature, and EK9 does not count them in LCOM4 - hand-written or defaulted. The hand-written '$' below reads both price and label, yet PricedItem still measures LCOM4 = 2: priceWithTax() and shortLabel() are separate groups. A nine-group class with a '$' that prints all nine fields is still E11014:
E11014: 'Compass' on line 6 position 4: LCOM4 of 9 exceeds maximum of 8 for CLASS: low cohesion - class has disconnected method groups, consider splitting
So writing the usual overrides does not make an unrelated class look cohesive, which is what happens to a count that includes them.
WHY AN ERROR, NOT A WARNING
EK9 has no warnings, no suppression comments and no build-tool configuration to lower the limit: the compiler is the single gate, and code that passes it has passed every quality check. An advisory metric is the one that gets ignored under deadline pressure. The limit is 8 for a class and 10 for a component; traits, records, functions, services and field-less classes are exempt.
The example is a deliberately small class with two groups - legal, and honest about it. If the groups are unrelated, splitting is better (Q2288).
See Q761 for the limit and Q2289 for default operators.
Example
#!ek9 defines module qa.codequality.wholeobjectoperators defines class <?- Two responsibilities share one class: a price and a label. The hand-written '$' reads BOTH fields, but a whole-object operator is not counted, so it does not hide the two groups: LCOM4 = 2 (well under the limit of 8). -?> PricedItem price Float: 0.0 label String: "" PricedItem() -> priceArg as Float labelArg as String this.price: priceArg this.label: labelArg priceWithTax() as pure <- rtn as Float: price * 1.2 shortLabel() as pure <- rtn as String: label.upperCase() operator $ as pure <- rtn as String: `${label} costs ${price}` default operator ? defines program ToStringLikeDemo() stdout <- Stdout() item <- PricedItem(10.0, "pen") stdout.println($item) stdout.println(`${item.priceWithTax()}`) stdout.println(item.shortLabel())
Common mistakes
E50001 — The variable itemXYZ was never declared, so it cannot be resolved, triggering E50001. See ek9 -h E50001 for details.
Incorrect:
stdout.println($itemXYZ)
Correct:
stdout.println($item)
Other ways to ask this
- SonarQube LCOM warning versus EK9 E11014 error
- does toString count in LCOM4 like in Java
- NDepend lack of cohesion informational versus EK9 compile error
- why can I not suppress E11014
- does my hand-written $ operator make my class cohesive
Coming from another language?
Java: SonarQube and PMD can report LCOM as an advisory issue that teams mark 'won't fix' or suppress with an annotation; equals, hashCode and toString are often what hides a bad score. C#: NDepend reports LCOM as an informational rule. Kotlin, Python, Go, Rust: no cohesion rule in the compiler. EK9: operators are excluded by rule, and low cohesion is a compile error with no suppression.
Keywords: C#, E11014, Java, LCOM4, NDepend, SonarQube, equals, error, hashCode, migration, operators, suppress, toString, warning