Why does my class fail E11014 and how do I split it?

← Code Quality · Ref: Q2288

E11014 means a class has more than 8 groups of methods that have nothing to do with each other. EK9 counts the groups (LCOM4): two methods are in the same group when they read or write the same field, or when one calls the other. A class whose methods split into 9 or more groups is really several classes sharing a name.

A FAILING CLASS

A StoreManager that keeps stock, pricing, loyalty points, a customer name, shipping, an audit counter, a coupon, a region and a gift-wrap flag, with one or two methods per concern, fails with:

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

THE FIX: SPLIT, THEN COMPOSE

1. List the fields and note which methods use which. Methods that share fields stay together; the rest are separate responsibilities.
2. Give each responsibility its own class, carrying its fields and the methods that use them (the example below: StockLevel, Pricing and Loyalty, each LCOM4 = 1).
3. If callers still want one front door, write a small coordinator that holds the focused classes as properties and delegates (StoreFront below).

Do not add a method that merely touches several fields to make the groups join: that satisfies the number and keeps the design wrong. Data that only travels together and has no behaviour belongs in a record, see Q2291.

See Q761 for the limit, Q2290 for what joins groups, Q314 for cohesion and coupling.

Example

#!ek9
defines module qa.codequality.godclasssplit

  defines class

    <?-
      One responsibility: how much stock there is and when to reorder.
      Both methods use stockLevel, so they form one group (LCOM4 = 1).
    -?>
    StockLevel
      stockLevel Integer: 0
      reorderPoint Integer: 10

      add()
        -> amount as Integer
        stockLevel += amount

      needsReorder() as pure
        <- rtn as Boolean: stockLevel < reorderPoint

      default operator ?

    <?-
      One responsibility: pricing. grossPrice and discounted share basePrice,
      so they are connected (LCOM4 = 1).
    -?>
    Pricing
      basePrice Float: 1.0
      taxRate Float: 0.2
      discountRate Float: 0.0

      grossPrice() as pure
        <- rtn as Float: basePrice * (1.0 + taxRate)

      discounted() as pure
        <- rtn as Float: basePrice * (1.0 - discountRate)

      default operator ?

    <?-
      One responsibility: loyalty points (LCOM4 = 1).
    -?>
    Loyalty
      points Integer: 0

      earn()
        -> amount as Integer
        points += amount

      balance() as pure
        <- rtn as Integer: points

      default operator ?

    <?-
      The coordinator composes the focused classes. Each of its methods
      reaches a different part, so it is small enough to stay under the limit.
    -?>
    StoreFront
      stock as StockLevel: StockLevel()
      pricing as Pricing: Pricing()
      loyalty as Loyalty: Loyalty()

      receive()
        -> amount as Integer
        stock.add(amount)

      reorderNeeded() as pure
        <- rtn as Boolean: stock.needsReorder()

      quote() as pure
        <- rtn as Float: pricing.grossPrice()

      reward()
        -> amount as Integer
        loyalty.earn(amount)

      rewardBalance() as pure
        <- rtn as Integer: loyalty.balance()

      default operator ?

  defines program

    GodClassSplitDemo()
      stdout <- Stdout()
      shop <- StoreFront()

      stdout.println(`Reorder needed: ${shop.reorderNeeded()}`)
      shop.receive(25)
      stdout.println(`Reorder needed: ${shop.reorderNeeded()}`)
      stdout.println(`Quote: ${shop.quote()}`)
      shop.reward(7)
      stdout.println(`Points: ${shop.rewardBalance()}`)

Common mistakes

E50001 — The variable shopXYZ was never declared, so it cannot be resolved, triggering E50001. See ek9 -h E50001 for details.

Incorrect:

shopXYZ.receive(25)

Correct:

shop.receive(25)
Other ways to ask this
  • E11014 LCOM4 of 9 exceeds maximum of 8
  • how do I fix low cohesion in an EK9 class
  • my class has too many unrelated methods, how do I split it
  • class has disconnected method groups consider splitting
  • how to break up a god class in EK9

Coming from another language?

Java: a 'god class' passes the compiler and only a SonarQube LCOM rule, if configured, may mention it. C#: NDepend can report it, as a separate tool. Python/Go/Rust: no cohesion check. EK9: a class with more than 8 disconnected method groups does not compile (E11014), so the split happens before the code is merged.

Keywords: E11014, LCOM4, class, cohesion, composition, coordinator, disconnected, god class, refactor, responsibility, split class