Do default operators like ? == $ and #? count towards class cohesion?
← Code Quality · Ref: Q2289
No. The whole-object operators - ? == <> <=> <~> < <= > >= #? $ $$ :=: :^: :~: - are not counted in LCOM4, whether you write them by hand or use 'default operator'. Neither is any other defaulted operator.
WHY
A whole-object operator reads every field by definition, and every class with fields must have a '?' operator. If they counted, they would join every method to every other and every class would measure LCOM4 = 1, however unrelated its methods. Leaving them out means the score describes how the behaviour you wrote is organised, not what the value is.
THE EXAMPLE
Gauges has eight fields and eight reading methods, each touching its own field: 8 groups, the class limit, with default operator ?, ==, #? and $ present. Adding a ninth field and reader makes it 9 groups and E11014 - the defaulted operators do not help:
E11014: 'Gauges' on line 6 position 4: LCOM4 of 9 exceeds maximum of 8 for CLASS: low cohesion - class has disconnected method groups, consider splitting
So 'default operator ?' is never a cohesion fix; a class that fails needs to be split (Q2288).
See Q761 for the limit and Q2293 for hand-written operators.
Example
#!ek9 defines module qa.codequality.defaultoperators defines class Gauges alpha Integer: 1 beta Integer: 2 gamma Integer: 3 delta Integer: 4 epsilon Integer: 5 zeta Integer: 6 eta Integer: 7 theta Integer: 8 alphaReading() as pure <- rtn as Integer: alpha betaReading() as pure <- rtn as Integer: beta gammaReading() as pure <- rtn as Integer: gamma deltaReading() as pure <- rtn as Integer: delta epsilonReading() as pure <- rtn as Integer: epsilon zetaReading() as pure <- rtn as Integer: zeta etaReading() as pure <- rtn as Integer: eta thetaReading() as pure <- rtn as Integer: theta default operator ? default operator == default operator #? default operator $ defines program DefaultOperatorsDemo() stdout <- Stdout() first <- Gauges() second <- Gauges() stdout.println(`Same: ${first == second}`) stdout.println(`Set: ${first?}`) stdout.println(`Alpha: ${first.alphaReading()}`)
Common mistakes
E11014 — Adding iota and iotaReading() makes a 9th disconnected method group; the defaulted operators do not join groups, so LCOM4 is 9, exceeding the class threshold of 8. See ek9 -h E11014 for details.
Incorrect:
theta Integer: 8 iota Integer: 9 iotaReading() as pure <- rtn as Integer: iota alphaReading() as pure
Correct:
theta Integer: 8 alphaReading() as pure
Other ways to ask this
- does default operator ? hide low cohesion
- do the == and $ operators join method groups in LCOM4
- can I add default operator to fix E11014
- do generated operators affect the LCOM4 score
- why does my cohesion not improve when I add default operator
Coming from another language?
Java: equals, hashCode and toString read every field, so LCOM tools that count them see a cohesive class; many tools exclude them for that reason, and EK9 does it by rule for its operator set. C#: same story for Equals/GetHashCode/ToString. EK9: the operator set is fixed, so the exclusion is exact and 'default operator' cannot mask a split.
Keywords: E11014, LCOM4, class, cohesion, default operator, equality, generated, isSet, operator, whole-object