Why does EK9 not count constructors in LCOM4?

← Code Quality · Ref: Q2292

A constructor is not a node in the LCOM4 graph, and whatever it reads or writes is discarded, so it neither joins groups nor forms a group of its own.

WHY

A constructor sets every field - that is its job. If it counted, it would be joined to every method that touches any field and so connect all of them: every class with a constructor would measure LCOM4 = 1 regardless of its methods. The classic definition of LCOM4 leaves constructors out for the same reason. What the metric is after is how the behaviour is organised, and construction says nothing about that. The same goes for 'default operator' and the whole-object operators (Q2289).

THE EXAMPLE

Compass has a constructor that sets all eight fields and eight reading methods, each using one field: 8 groups, the class limit, so it compiles. Add a ninth field and reader and the constructor does not rescue it:

  E11014: 'Compass' on line 13 position 4: LCOM4 of 9 exceeds maximum of 8 for CLASS: low cohesion - class has disconnected method groups, consider splitting

If constructing a class takes many arguments that its methods use separately, the arguments are probably several responsibilities: split the class (Q2288) or carry the values in a record (Q2291).

See Q761 for the limit.

Example

#!ek9
defines module qa.codequality.constructorsnotcounted

  defines class

    <?-
      The constructor sets every field, which in a naive measure would join
      all the methods into one group. LCOM4 ignores constructors, so what
      counts is the behaviour: eight accessors, each reading its own field,
      are eight groups - exactly the class limit, so this compiles.
      A ninth unrelated group would be E11014, constructor or not.
    -?>
    Compass
      north Integer: 0
      south Integer: 0
      east Integer: 0
      west Integer: 0
      up Integer: 0
      down Integer: 0
      left Integer: 0
      right Integer: 0

      Compass()
        ->
          northArg as Integer
          southArg as Integer
          eastArg as Integer
          westArg as Integer
          upArg as Integer
          downArg as Integer
          leftArg as Integer
          rightArg as Integer
        this.north: northArg
        this.south: southArg
        this.east: eastArg
        this.west: westArg
        this.up: upArg
        this.down: downArg
        this.left: leftArg
        this.right: rightArg

      northValue() as pure
        <- rtn as Integer: north

      southValue() as pure
        <- rtn as Integer: south

      eastValue() as pure
        <- rtn as Integer: east

      westValue() as pure
        <- rtn as Integer: west

      upValue() as pure
        <- rtn as Integer: up

      downValue() as pure
        <- rtn as Integer: down

      leftValue() as pure
        <- rtn as Integer: left

      rightValue() as pure
        <- rtn as Integer: right

      default operator ?

  defines program

    ConstructorsNotCountedDemo()
      stdout <- Stdout()
      compass <- Compass(northArg: 1, southArg: 2, eastArg: 3, westArg: 4, upArg: 5, downArg: 6, leftArg: 7, rightArg: 8)
      stdout.println(`${compass.northValue()} ${compass.rightValue()}`)

Common mistakes

E11014 — The constructor does not join groups, so the new centre field and centreValue() make a 9th disconnected method group: LCOM4 is 9, exceeding the class threshold of 8. See ek9 -h E11014 for details.

Incorrect:

      right Integer: 0
      centre Integer: 0

      centreValue() as pure
        <- rtn as Integer: centre

      Compass()

Correct:

      right Integer: 0

      Compass()
Other ways to ask this
  • does my constructor setting every field make the class cohesive
  • do constructors join method groups for E11014
  • why does the constructor not hide low cohesion
  • constructor initialises all fields LCOM4
  • is a constructor a method for cohesion

Coming from another language?

Java: LCOM4 as defined by Hitz and Montazeri, and the tools that implement it, usually ignore constructors; some LCOM variants count them. C#: NDepend ignores constructors in its LCOM measures. EK9: always excluded.

Keywords: E11014, LCOM4, class, cohesion, constructor, fields, group, initialise, metric