How does EK9 measure cohesion and coupling?
← Code Quality · Ref: Q314
EK9 measures both cohesion and coupling at class and module level, using established software engineering metrics.
LACK OF COHESION - LCOM4 (E11014)
LCOM4 counts the number of connected components in a class. A connected component is a group of methods joined by shared fields (they read or write the same field) or by calls (one calls the other, within the same class; a call through super does not join). A perfectly cohesive class has LCOM4 of 1. Higher values mean the class should be split. Constructors, the whole-object operators (? == $ #? and the rest) and defaulted operators are not counted, because they read every field by definition. Thresholds: 8 for classes, 10 for components; traits, records, functions, services and field-less classes are exempt. Built-in types are excluded from this check. See Q761 for the boundary and Q2288 for how to split a class that fails.
EFFERENT COUPLING - Ce (E11015)
Efferent coupling counts how many other types a class depends on. High Ce means a class is tightly coupled to many other types and will break when any of them change. Thresholds: 8 for records, 12 for classes, 15 for components. Built-in types (String, Integer, List, etc.) are excluded because depending on standard library types is normal.
MODULE COUPLING (E11016)
Counts how many other modules a module depends on. High module coupling means changes ripple across module boundaries. Threshold: 10 external module dependencies.
MODULE COHESION (E11017)
Measures how related the contents of a module are. Low module cohesion suggests the module is a grab-bag of unrelated functionality and should be split.
CHIDAMBER AND KEMERER EVIDENCE
These metrics come from Chidamber and Kemerer (1994) research on object-oriented metrics. A later validation (Basili, Briand and Melo, IEEE TSE 1996, eight projects) found coupling between objects among the metrics that predicted fault-prone classes.
See Q311 for the full quality checks catalog. See Q312 for complexity metrics. See Q763 for inheritance depth.
See Q694 for named arguments pattern. See Q698 for excessive Boolean params.
Example
#!ek9 defines module qa.codequality.cohesion defines class <?- A cohesive class where all methods share fields. LCOM4 is 1 because every method uses the shared state. -?> TemperatureConverter celsiusReading as Float: Float() TemperatureConverter() -> initialCelsius as Float this.celsiusReading := initialCelsius celsius() as pure <- rtn as Float: celsiusReading toFahrenheit() as pure <- rtn as Float: Float() conversionFactor <- 9.0 / 5.0 freezingOffset <- 32.0 rtn: celsiusReading * conversionFactor + freezingOffset toKelvin() as pure <- rtn as Float: Float() kelvinOffset <- 273.15 rtn: celsiusReading + kelvinOffset override operator ? as pure <- rtn as Boolean: celsiusReading? operator $ as pure <- rtn as String: `${celsiusReading}C` defines program CohesionCouplingDemo() stdout <- Stdout() boilingPoint <- 100.0 reading <- TemperatureConverter(boilingPoint) stdout.println(`Celsius: ${reading.celsius()}`) stdout.println(`Fahrenheit: ${reading.toFahrenheit()}`) stdout.println(`Kelvin: ${reading.toKelvin()}`)
Common mistakes
E50001 — The argument names a variable that was never declared, so it cannot be resolved, triggering E50001. See ek9 -h E50001 for details.
Incorrect:
reading <- TemperatureConverter(boilingPointXYZ)
Correct:
reading <- TemperatureConverter(boilingPoint)
E50001 — The variable readingXYZ was never declared, so it cannot be resolved, triggering E50001. See ek9 -h E50001 for details.
Incorrect:
stdout.println(`Celsius: ${readingXYZ.celsius()}`)
Correct:
stdout.println(`Celsius: ${reading.celsius()}`)
Other ways to ask this
- What is LCOM4 in EK9?
- How does EK9 detect tightly coupled modules?
- Does EK9 measure efferent coupling?
Coming from another language?
Java: SonarQube measures LCOM4 and coupling but as informational metrics, not enforcement. JDepend measures package coupling but is a separate tool. Rust: no cohesion or coupling metrics built-in or in clippy. Go: no cohesion or coupling metrics. Python: pylint has no coupling metrics. C++: CppDepend measures metrics but is a commercial separate tool. EK9: LCOM4, efferent coupling, and module coupling are mandatory compiler errors with fixed thresholds.
Keywords: E11014, E11015, E11016, LCOM4, chidamber, clean-code, cohesion, component, connected, coupling, efferent, kemerer, metric, migrate, module, quality