What is the efferent coupling limit and how do I stay within it?

← Code Quality · Ref: Q762

EK9 measures efferent coupling (Ce) — the number of distinct external types a construct depends on. When Ce exceeds the threshold, E11015 triggers.

THRESHOLDS

- Class: max Ce of 12
- Record: max Ce of 8
- Trait: max Ce of 8
- Service: max Ce of 15
- Component: max Ce of 15
- Function: max Ce of 8

THIS EXAMPLE

The HighCouplingClass below depends on exactly 12 distinct record types via fields. This is at the class threshold. Adding a 13th dependency triggers E11015.

See Q310 for code quality overview. See Q314 for cohesion and coupling details.

Example

defines module qa.codequality.couplingboundary

  defines record

    Dep1
      mainValue Integer: 0
      default operator ?
    Dep2
      mainValue Integer: 0
      default operator ?
    Dep3
      mainValue Integer: 0
      default operator ?
    Dep4
      mainValue Integer: 0
      default operator ?
    Dep5
      mainValue Integer: 0
      default operator ?
    Dep6
      mainValue Integer: 0
      default operator ?
    Dep7
      mainValue Integer: 0
      default operator ?
    Dep8
      mainValue Integer: 0
      default operator ?
    Dep9
      mainValue Integer: 0
      default operator ?
    Dep10
      mainValue Integer: 0
      default operator ?
    Dep11
      mainValue Integer: 0
      default operator ?
    Dep12
      mainValue Integer: 0
      default operator ?
    Dep13
      mainValue Integer: 0
      default operator ?

  defines class

    HighCouplingClass
      d1 as Dep1: Dep1()
      d2 as Dep2: Dep2()
      d3 as Dep3: Dep3()
      d4 as Dep4: Dep4()
      d5 as Dep5: Dep5()
      d6 as Dep6: Dep6()
      d7 as Dep7: Dep7()
      d8 as Dep8: Dep8()
      d9 as Dep9: Dep9()
      d10 as Dep10: Dep10()
      d11 as Dep11: Dep11()
      d12 as Dep12: Dep12()

      allSet()
        <- rtn as Boolean: d1? and d2? and d3? and d4? and d5? and d6? and d7? and d8? and d9? and d10? and d11? and d12?

      default operator ?

  defines program

    CouplingBoundaryDemo()
      stdout <- Stdout()
      tracker <- HighCouplingClass()
      stdout.println($tracker.allSet())

Common mistakes

E11015 — Adding field d13 of type Dep13 pushes Ce from 12 to 13, exceeding the class threshold of 12. Group related dependencies into a record or split the class. See ek9 -h E11015 for details.

Incorrect:

      d12 as Dep12: Dep12()
      d13 as Dep13: Dep13()

      allSet()

Correct:

      d12 as Dep12: Dep12()

      allSet()
Other ways to ask this
  • What triggers E11015 excessive coupling?
  • How many types can a class depend on?
  • What is the Ce threshold in EK9?
  • Why does EK9 limit type dependencies?

Coming from another language?

Java: no coupling limit in javac; SonarQube reports Ce as informational. C#: NDepend measures Ce but does not enforce. Python: no coupling metrics. Go: no coupling enforcement. EK9: Ce exceeding threshold is a compiler error.

Keywords: threshold, E11015, boundary, class, quality, dependency, Ce, coupling, efferent, type