Do method calls and helper methods count towards cohesion in LCOM4?

← Code Quality · Ref: Q2290

Yes, calls count - within the same class. Two methods are in one group when they read or write the same field, OR when one calls the other (a bare call, this.m(), and operator expressions on this that resolve to a member of the class).

WHAT DOES NOT JOIN

- A call through super, or to an inherited method: the callee belongs to another class, so it says nothing about this class's own organisation.
- A call or field read on another instance of the same class (other.m(), other.total): only what the object does to itself shows how its own behaviour is organised.
- Constructors and the whole-object operators, see Q2289 and Q2292.

THIS EXAMPLE

TextTally has nine counters; each count method reads its own field, so alone they are 9 groups. summary() calls all nine, so the calls connect everything: LCOM4 = 1. Without summary() the same class is E11014:

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

A class whose subclass calls super in every method gets no help either: a Child with nine own fields and nine methods that each call super.describe() is still 9 groups and fails with E11014.

BE HONEST WITH IT

A coordinating method that really uses the parts is a true connection. A method written only to call everything so the number drops is the metric being gamed; the design has not changed. If the parts are independent, split the class (Q2288).

See Q761 for the limit.

Example

#!ek9
defines module qa.codequality.callsjoin

  defines class

    <?-
      Nine counters. Each count method touches only its own field, but
      summary() calls every one of them, so the calls join all nine into a
      single group: LCOM4 = 1.
    -?>
    TextTally
      lines Integer: 1
      words Integer: 2
      chars Integer: 3
      digits Integer: 4
      spaces Integer: 5
      tabs Integer: 6
      commas Integer: 7
      periods Integer: 8
      quotes Integer: 9

      linesCount() as pure
        <- rtn as Integer: lines

      wordsCount() as pure
        <- rtn as Integer: words

      charsCount() as pure
        <- rtn as Integer: chars

      digitsCount() as pure
        <- rtn as Integer: digits

      spacesCount() as pure
        <- rtn as Integer: spaces

      tabsCount() as pure
        <- rtn as Integer: tabs

      commasCount() as pure
        <- rtn as Integer: commas

      periodsCount() as pure
        <- rtn as Integer: periods

      quotesCount() as pure
        <- rtn as Integer: quotes

      summary() as pure
        <- rtn as Integer: linesCount() + wordsCount() + charsCount() + digitsCount() + spacesCount() + tabsCount() + commasCount() + periodsCount() + quotesCount()

      default operator ?

  defines program

    CallsJoinDemo()
      stdout <- Stdout()
      tally <- TextTally()
      stdout.println(`Summary: ${tally.summary()}`)

Common mistakes

E11014 — summary() no longer calls the counters, so it is a group of its own and the nine counters are nine more: LCOM4 is 10, exceeding the class threshold of 8. See ek9 -h E11014 for details.

Incorrect:

<- rtn as Integer: 0

Correct:

<- rtn as Integer: linesCount() + wordsCount() + charsCount() + digitsCount() + spacesCount() + tabsCount() + commasCount() + periodsCount() + quotesCount()
Other ways to ask this
  • does one method calling another make them cohesive
  • do private helper methods join groups for E11014
  • does a super call count for cohesion
  • how do method calls affect LCOM4
  • what makes two methods connected in EK9 cohesion

Coming from another language?

Java: LCOM4 tools usually count calls between methods of the same class, as EK9 does; a super call is a different class's method in both. C#: NDepend's LCOM variants differ in whether calls count; EK9 fixes the rule (fields and calls). EK9: the rule is exact and the failure is a compile error.

Keywords: E11014, LCOM4, class, cohesion, connected, group, helper methods, inherited, method calls, super