PMD rule God Class - understanding the metrics

Cimlman picture Cimlman · May 23, 2016 · Viewed 8k times · Source

We are using the source code analyzer PMD in our Java project. I am trying to resolve the reported issues and I am currently struggling with the GodClass rule. I know that the idea is not to create huge classes.

However, I don't like the word "huge" because it's too vague. Can anybody explain how the metrics of this rule works? The report says e.g.

Possible God class (WMC=47, ATFD=11, TCC=0.06315789473684211)

What do all these numbers mean? Does anybody know the formula that decides whether a particular class is huge or not?

Javadoc to this rule states

The rule uses the detection strategy described in [1]. The violations are reported against the entire class. [1] Lanza. Object-Oriented Metrics in Practice. Page 80.

Well, I don't wont to order some book just because of its page 80.

Btw. is there a way to configure such rule, i.e. change its parameters?

Thanks for explanation.

Answer

Nathan picture Nathan · Nov 9, 2017

WMC stands for Weighted Methods Count or Weighted Method per Class. 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.

ATFD stands for Access to Foreign Data. This metric represents the number of external classes from which a given class accesses attributes, directly or via accessor-methods.

TCC stands for Tight Class Cohesion. TCC is the relative number of methods directly connected via accesses of attributes.

The code triggers a violation if WMC >= 47 and ATFD > 5 and TCC < 1/3.

You can read about the God class on page 55 in Object-Oriented Metrics in Practice (and you do not have to buy the book to just read 1 page). You can also read the PMD documentation.