Code Quality – WMC Calculation in CK Metrics Suite

code metricscode-qualityobject-orientedproject-management

I have two classes C1 and C2. C1 has 2 methods and C2 has 3 methods each of complexity value 1. C2 inherits from C1. So, I know C2 has 2+3=5 methods in all. The question is, should I take C2 to have 5 methods while calculating the WMC for the classes?

I couldn't find any example or proof to justify or refute the claim.

WMC is Weighted Methods per Class as specified in CK Metrics Suite

The WMC metric is the sum of the complexities of all class methods. It is an indicator of how much effort is required to develop and maintain a particular class. RefactorIT sums the V(G) (cyclomatic complexity) of all declared methods and constructors of class to calculate the WMC…

Best Answer

In their seminal work on metric for object oriented design, Chidamber & Kemerer defined in 1993 6 metrics, among which the Weighted Methods Per Class (WMC).

Their definition is somewhat ambiguous:

Consider a Class C1, with methods M1,...Mn that are defined in the class. Let c1,...cn be the complexity of the methods. Then WMC is the sum of ci for i from 1 to n. .

Possible interpretations

Defined could mean declared, as in the definition that you have quoted. In this case, you would not count inherited methods. This would make sense because the metric intends to measure how difficult it is to write and maintain the class:

  • if a class D would inherit from a base class B, but declare no methods on its own, it is definitively not difficult to maintain. It's certainly easier to maintain, even if B would have 100 methods, than another class that has a couple of methods.
  • if a class B would have 10 different constructors, but class D has only 2, the maintenance effort is related to the 2 existing constructors and not to the 10 of the base class.

But defined could also mean available. In this case you should count all the inherited methods. This seems to be the probable interpretation of the authors in their article:

The larger the number of methods in a class the greater the potential impact on children, since children will inherit all the methods defined in the class.

And this can also make sense:

  • a high number of methods, even inherited, makes the class more specific and less easy to reuse.
  • a high number of inherited methods make the class more difficult to use, and the difficulty to maintain consistency between the many methods could also be higher.
  • WMC should be relevant for class semantic (according to C&K), and inherited methods definitively contribute to class semantic.

So you should opt for this second interpretation.

Additional remarks

More recent metrics focus more on quality rather than only quantitative impact for maintenance. These are less ambiguous and take into account attributes and methods and make difference between:

  • visible and private methods, to measure encapsulation, and
  • inherited and available methods, to better take into account the structural benefits of inheritance.