Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PMD rule God Class - understanding the metrics

Tags:

java

pmd

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.

like image 789
Cimlman Avatar asked May 23 '16 11:05

Cimlman


People also ask

What are PMD rules?

Conceptually, PMD rules work by matching a “pattern” against the AST of a file. Rules explore the AST and find nodes that satisfy some conditions that are characteristic of the specific thing the rule is trying to flag. Rules then report a violation on these nodes.


1 Answers

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.

like image 162
Nathan Avatar answered Oct 03 '22 03:10

Nathan