Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a metric analogous to the McCabe Complexity measure to measure how cohesive a routine is and also how loosely (or tightly) coupled the routine is to other code in the same code base?

share|improve this question
up vote 20 down vote accepted

I think the metric you are looking for is LCOM4, although it applies more to classes.

Sonar explains it nicely here:

...metric : LCOM4 (Lack Of Cohesion Methods) to measure how cohesive classes are. Interpreting this metric is pretty simple as value 1 means that a class has only one responsibility (good) and value X means that a class has probably X responsibilities (bad) and should be refactored/split.

There is not any magic here, only common sense. Let’s take a simple example with class Driver. This class has two fields : Car and Brain, and five methods : drive(), goTo(), stop(), getAngry() and drinkCoffee(). Here is the dependency graph between those components. There are three blocks of related components, so LCOM4 = 3, so the class seems to have three different responsibilities and breaks the Single Responsibility Principle.


It's a great tool, if you can use it. :)

share|improve this answer
Wow--that's excellent. Thank you. – Onorio Catenacci May 31 '12 at 15:38
@OnorioCatenacci No problem. :) – Oleksi May 31 '12 at 15:40
Just too bad they don't go into how they calculate the metric. – Onorio Catenacci May 31 '12 at 15:44
This might help with that: – Oleksi May 31 '12 at 15:46
Thanks--exactly what the doctor ordered. – Onorio Catenacci May 31 '12 at 15:57
  • Afferent coupling: Number of responsibilities
  • Efferent coupling: Number of dependencies
  • Instability: Ratio of efferent coupling to total coupling (Afferent + Efferent).

Instability is supported in various code metric tools.

share|improve this answer
Thanks @Brian--exactly the sort of thing I was hoping to find. – Onorio Catenacci May 31 '12 at 15:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.