Coupling Rules

These are rules which find instances of high or inappropriate coupling between objects and packages.

CouplingBetweenObjects

Since: PMD1.04

This rule counts unique attributes, local variables and return types within an object. A number higher than specified threshold can indicate a high degree of coupling.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.coupling.CouplingBetweenObjectsRule

Example(s):

import com.Blah;
import org.Bar;
import org.Bardo;
public class Foo {
 private Blah var1;
 private Bar var2;
 //followed by many imports of unique objects
 void ObjectC doWork() {
  Bardo var55;
  ObjectA var44;
  ObjectZ var93;
  return something;
 }
}

    

ExcessiveImports

Since: PMD1.04

A high number of imports can indicate a high degree of coupling within an object. Rule counts the number of unique imports and reports a violation if the count is above the user defined threshold.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.coupling.ExcessiveImportsRule

Example(s):
      
import blah.blah.Baz;
import blah.blah.Bif;
// 18 others from the same package elided
public class Foo {
 public void doWork() {}
}
      
  

LooseCoupling

Since: PMD0.7

Avoid using implementation types (i.e., HashSet); use the interface (i.e, Set) instead

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.coupling.LooseCouplingRule

Example(s):

import java.util.*;
public class Bar {
 // Use List instead
 private ArrayList list = new ArrayList();
 // Use Set instead
 public HashSet getFoo() {
  return new HashSet();
 }
}
  
      

LoosePackageCoupling

Since: PMD5.0

Avoid using classes from the configured package hierarchy outside of the package hierarchy, except when using one of the configured allowed classes.

This rule is defined by the following Java class:net.sourceforge.pmd.lang.java.rule.coupling.LoosePackageCouplingRule

Example(s):

package some.package;
import some.other.package.subpackage.subsubpackage.DontUseThisClass;
public class Bar {
   DontUseThisClass boo = new DontUseThisClass();
}