What is the LCOM4 cohesion limit and how do I stay within it?

← Code Quality · Ref: Q761

EK9 measures class cohesion using LCOM4 (Lack of Cohesion of Methods version 4). It builds a graph of the class's methods and counts the connected groups. Two methods are joined when they read or write the same field, or when one calls the other (a member of the same class; a call through super does not join). Only what the object does to itself counts: a call or field read on another instance of the class joins nothing. The number of groups is the LCOM4 score.

THRESHOLDS

- Class: max LCOM4 of 8 (E11014)
- Component: max LCOM4 of 10
Traits, records, functions, services (which have no fields) and classes with no fields are exempt.

WHAT IS NOT COUNTED

Abstract methods (no body, so never a group of their own; methods that call one are still joined through it), constructors (they set every field), the whole-object operators (? == <> <=> <~> < <= > >= #? $ $$ :=: :^: :~:) and every defaulted operator. They read every field by definition, so counting them would make any class look cohesive. See Q2289 and Q2292.

WHAT LCOM4 MEASURES

LCOM4 = 1 means perfect cohesion - all methods are connected through shared fields or calls. LCOM4 = 8 means 8 groups of methods that share no field and call no one else in the group. Above the threshold, the class is doing too many unrelated things.

THIS EXAMPLE

The InventoryTracker class below has exactly 8 disconnected method groups (LCOM4 = 8), which is the maximum allowed for a class. Each method group accesses only its own field. Adding a 9th unrelated method would trigger E11014.

HOW TO FIX HIGH LCOM4

1. Extract each disconnected group into its own focused class and move its fields and methods together (see Q2288 for a worked split)
2. If the original class still has to present one front door, compose the new classes in a small coordinator that delegates to them
3. If the class only holds data, with a reader and a writer per field, make it a record (see Q2291)

Do not add a method whose only job is to touch several fields or call every other method so the groups join. That moves the number, not the design; the class still has several unrelated responsibilities.

See Q310 for code quality overview. See Q314 for cohesion and coupling details. See Q696 for complexity limits. See Q2290 for what joins groups.

Example

#!ek9
defines module qa.codequality.lcom4boundary

  defines class

    <?-
      This class has exactly 8 disconnected method groups (LCOM4 = 8).
      Each method accesses only one unique field and calls no other method.
      No method shares a field with another method.
      The '?' operator reads every field but is not counted.
      This is exactly at the class threshold — it compiles.
      Adding a 9th unrelated method/field pair would trigger E11014.
    -?>
    InventoryTracker
      field1 Integer: 1
      field2 Integer: 2
      field3 Integer: 3
      field4 Integer: 4
      field5 Integer: 5
      field6 Integer: 6
      field7 Integer: 7
      field8 Integer: 8

      method1()
        <- rtn Integer := field1

      method2()
        <- rtn Integer := field2

      method3()
        <- rtn Integer := field3

      method4()
        <- rtn Integer := field4

      method5()
        <- rtn Integer := field5

      method6()
        <- rtn Integer := field6

      method7()
        <- rtn Integer := field7

      method8()
        <- rtn Integer := field8

      override operator ? as pure
        <- rtn Boolean := field1?

  defines program

    Lcom4BoundaryDemo()
      stdout <- Stdout()
      tracker <- InventoryTracker()
      stdout.println($tracker.method1())
      stdout.println($tracker.method8())

Common mistakes

E11014 — Adding field9 and method9 creates a 9th disconnected method group (method9 accesses only field9). LCOM4 goes from 8 to 9, exceeding the class threshold of 8. Extract unrelated methods into separate classes. See ek9 -h E11014 for details.

Incorrect:

      field8 Integer: 8
      field9 Integer: 9

      method9()
        <- rtn Integer := field9

      method1()

Correct:

      field8 Integer: 8

      method1()
Other ways to ask this
  • What triggers E11014 low cohesion?
  • What is the LCOM4 threshold for classes?
  • How does EK9 measure class cohesion?
  • Why does EK9 limit disconnected method groups?

Coming from another language?

Java: SonarQube measures LCOM but only as advisory metric. C#: NDepend reports LCOM informational only. Python: no cohesion analysis. Go: no cohesion measurement. Kotlin: Detekt does not measure LCOM. EK9: LCOM4 > 8 is a compiler error — class won't compile, and there is no way to suppress it.

Keywords: E11014, LCOM4, boundary, class, cohesion, disconnect, field, method, metric, quality, split, threshold