Video in progress

Of the code quality metrics (cyclomatic complexity, LCOM, afferent/efferent coupling, instability), which are useful in practice and which mislead?

Metrics are useful for finding trends and hotspots, and harmful as absolute targets. Complexity plus change frequency, coupling between packages and cycles give the most value; coverage percentage and LOC mislead the most.

📖 Answer

In short:

Metric What it shows Usefulness
Cyclomatic / cognitive complexity branching inside a method a good signal at the method level; cognitive (Sonar) is closer to how hard it is to read
LCOM how much a class's methods use shared fields finds the "two classes in one file" case with low cohesion
Ca / Ce (afferent/efferent) how many incoming/outgoing dependencies a package has a package with a large Ca is risky to change, so it should be stable
Instability I = Ce/(Ca+Ce) how "volatile" a package is dependencies should point toward the more stable side (Stable Dependencies)
Cycles circular dependencies between packages almost always a real problem

Where they mislead:

  • If 80% coverage is the goal, people write tests without asserts. Mutation testing (PIT) shows whether tests really check anything.
  • A method length limit: you can split logic into ten tiny methods and make it harder to read.
  • A one-time snapshot: "complexity 1200" says nothing. Trends and comparisons between modules matter.

The most useful combination is the hotspot: complexity × change frequency. Complex code that nobody touches isn't a problem. Complex code that changes every week is where to invest.

Don't forget Goodhart's law: when a metric becomes a target, it stops being a good measure. So use metrics to start a conversation, not to rank teams. In CI, only stop deterioration (a quality gate on "new code"), not an absolute number.