Code Size Rules

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

ExcessiveMethodLength

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

Here's an example of code that would trigger this rule:

			

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
minimum100The method size reporting threshold

ExcessiveParameterList

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

Here's an example of code that would trigger this rule:

			

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
minimum10The parameter count reporting threshold

ExcessiveClassLength

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

Here's an example of code that would trigger this rule:

			

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

   
		

This rule has the following properties:

NameDefault valueDescription
minimum1000The class size reporting threshold

CyclomaticComplexity

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

Here's an example of code that would trigger this rule:

			

// 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
reportLevel10The Cyclomatic Complexity reporting threshold

ExcessivePublicCount

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

Here's an example of code that would trigger this rule:

			
    
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
minimum45The public item reporting threshold

TooManyFields

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

Here's an example of code that would trigger this rule:

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

This rule has the following properties:

NameDefault valueDescription
maxfields15The field count reporting threshold