Software Metrics 101 - Weighted Method Count (WMC)
The Weighted Method Count or Weighted Method per Class metric was orginally defined in A Metrics Suite for Object Oriented Design by Chidamber & Kemerer 1.
The WMC metric is defined as the sum of complexities of all methods declared in a class. This metric is a good indicator how much effort will be necessary to maintain and develop a particular class. There are three slightly different definitions of the WMC, where each definition uses another metric as a measure of the methods' complexity. Possible complexity values are:
- McCabe's Cyclomatic Complexity 2
- Lines of Code
- 1 (Number Of Methods or Unweighted WMC)
PHP_Depend uses the sum of Cyclomatic Complexity Numbers of all methods and constructors declared in a class to calculate the WMC metric. A lower WMC usually indicates to a class with bettwer abstraction and polymorphism. While a class with a high complexity value is a good indicator that it this class is very application specific and does more than one job, and therefore harder to test, reuse and maintain.
Thresholds
An appropriate threshold for the WMC lower limit is 1, because a class should at least consist of one method. An upper limit for the WMC of a class is harder to define and you can find several different suggestions in the literature. But it seems that an upper limit of 50 is a good reference point for most projects that start to use the Weighted Method Count metric.
Bibliography
- http://www.iiitd.ac.in/PhD2010/papers/SW_Paper2.pdf - IEEE Transactions on Software Engineering, A Metrics Suite for Object Oriented Design, Chidamber & Kemerer, 1994
- http://www.literateprogramming.com/mccabe.pdf - IEEE Transactions on Software Enginerring, A Complexity Measure, Thomas McCabe, 1976