Code Size Rules

The Code Size Ruleset contains a collection of rules that find code size related problems.

NPathComplexity

Since: PMD 3.9

The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.NpathComplexity

Example:

                
 
 public class Foo {
  void bar() {
   // lots of complicated code
  }
 }
 
    
            

This rule has the following properties:

NameDefault valueDescription
minimum 200 The npath reporting threshold

ExcessiveMethodLength

Since: PMD 0.6

Violations of this rule usually indicate that the method is doing too much. Try to reduce the method size by creating helper methods and removing any copy/pasted code.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.LongMethodRule

Example:

                

public class Foo {
 public void doSomething() {
  System.out.println("Hello world!");
  System.out.println("Hello world!");
  // 98 copies omitted for brevity.
 }
}

   
            

This rule has the following properties:

NameDefault valueDescription
minimum 100 The method size reporting threshold

ExcessiveParameterList

Since: PMD 0.9

Long parameter lists can indicate that a new object should be created to wrap the numerous parameters. Basically, try to group the parameters together.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.LongParameterListRule

Example:

                

public class Foo {
 public void addData(
  int p0, int p1, int p2, int p3, int p4, int p5,
  int p5, int p6, int p7, int p8, int p9, int p10) {
  }
 }
}

   
            

This rule has the following properties:

NameDefault valueDescription
minimum 10 The parameter count reporting threshold

ExcessiveClassLength

Since: PMD 0.6

Long Class files are indications that the class may be trying to do too much. Try to break it down, and reduce the size to something manageable.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.LongClassRule

Example:

                

public class Foo {
  public void bar() {
    // 1000 lines of code
  }
}

   
            

This rule has the following properties:

NameDefault valueDescription
minimum 1000 The class size reporting threshold

CyclomaticComplexity

Since: PMD 1.03

Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.CyclomaticComplexity

Example:

                

// Cyclomatic Complexity = 12
public class Foo {
1   public void example()  {
2       if (a == b)  {
3           if (a1 == b1) {
                fiddle();
4           } else if a2 == b2) {
                fiddle();
            }  else {
                fiddle();
            }
5       } else if (c == d) {
6           while (c == d) {
                fiddle();
            }
7        } else if (e == f) {
8           for (int n = 0; n < h; n++) {
                fiddle();
            }
        } else{
            switch (z) {
9               case 1:
                    fiddle();
                    break;
10              case 2:
                    fiddle();
                    break;
11              case 3:
                    fiddle();
                    break;
12              default:
                    fiddle();
                    break;
            }
        }
    }
}

   
            

This rule has the following properties:

NameDefault valueDescription
reportLevel 10 The Cyclomatic Complexity reporting threshold
showClassesComplexity true Indicate if class average violation should be added to the report
showMethodsComplexity true Indicate if class average violation should be added to the report

ExcessivePublicCount

Since: PMD 1.04

A large number of public methods and attributes declared in a class can indicate the class may need to be broken up as increased effort will be required to thoroughly test it.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.ExcessivePublicCount

Example:

                
    
public class Foo {
 public String value;
 public Bar something;
 public Variable var;
 // [... more more public attributes ...]
 public void doWork() {}
 public void doMoreWork() {}
 public void doWorkAgain() {}
 // [... more more public methods ...]
}
    
    
            

This rule has the following properties:

NameDefault valueDescription
minimum 45 The public item reporting threshold

TooManyFields

Since: PMD 3.0

Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zip fields could instead have one Address field.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.design.TooManyFields

Example:

                
   
public class Person {
   String one;
   int two;
   int three;
   [... many more public fields ...]
}
   
      
            

This rule has the following properties:

NameDefault valueDescription
maxfields 15 The field count reporting threshold

NcssMethodCount

Since: PMD 3.9

This rule uses the NCSS (Non Commenting Source Statements) algorithm to determine the number of lines of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.codesize.NcssMethodCount

Example:

                

public class Foo extends Bar {
 public int methd() {
     super.methd();





 //this method only has 1 NCSS lines
      return 1;
 }
}

   
            

This rule has the following properties:

NameDefault valueDescription
minimum 100 The method NCSS count reporting threshold

NcssTypeCount

Since: PMD 3.9

This rule uses the NCSS (Non Commenting Source Statements) algorithm to determine the number of lines of code for a given type. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.codesize.NcssTypeCount

Example:

                

public class Foo extends Bar {
 public Foo() {
 //this class only has 6 NCSS lines
     super();





      super.foo();
 }
}

   
            

This rule has the following properties:

NameDefault valueDescription
minimum 1500 The type NCSS count reporting threshold

NcssConstructorCount

Since: PMD 3.9

This rule uses the NCSS (Non Commenting Source Statements) algorithm to determine the number of lines of code for a given constructor. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

This rule is defined by the following Java class: net.sourceforge.pmd.rules.codesize.NcssConstructorCount

Example:

                

public class Foo extends Bar {
 public Foo() {
     super();





 //this constructor only has 1 NCSS lines
      super.foo();
 }
}

   
            

This rule has the following properties:

NameDefault valueDescription
minimum 100 The constructor NCSS count reporting threshold

TooManyMethods

Since: PMD 4.2

A class with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

This rule is defined by the following XPath expression:

                    
                    
                     //ClassOrInterfaceDeclaration/ClassOrInterfaceBody
                    [
                        count(descendant::MethodDeclarator[
                            not
                            (
                            starts-with(@Image,'get')
                                or
                            starts-with(@Image,'set')
                            )
                        ]) &gt; $maxmethods
                    ]
                    
                

This rule has the following properties:

NameDefault valueDescription
maxmethods 10 The method count reporting threshold