Design

The Design Checkerset contains a collection of checkers that find questionable designs.

UseSingleton

If you have a class that has nothing but static methods, consider making it a Singleton. Note that this doesn't apply to abstract classes, since their subclasses may well include non-static methods. Also, if you want this class to be a Singleton, remember to add a private constructor to prevent instantiation.

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

			

public class MaybeASingleton {
    public static void foo() {
     // etc
    }
    public static void bar() {
     // etc
    }
}

    
		

SimplifyBooleanReturns

Avoid unnecessary if..then..else statements when returning a boolean

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

			

public class Foo {
  private int bar =2;
  public boolean isBarEqualsTo(int x) {
    // this bit of code
    if (bar == x) {
     return true;
    } else {
     return false;
    }
    // can be replaced with a simple
    // return bar == x;
  }
}

    
		

SimplifyBooleanExpressions

Avoid unnecessary comparisons in boolean expressions - this makes simple code seem complicated.

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

			
  
public class Bar {
 // can be simplified to
 // bar = isFoo();
 private boolean bar = (isFoo() == true);

 public isFoo() { return false;}
}
  
      
		

SwitchStmtsShouldHaveDefault

Switch statements should have a default label.

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

			

public class Foo {
 public void bar() {
  int x = 2;
  switch (x) {
   case 2: int j = 8;
  }
 }
}

    
		

DeeplyNestedIfStmts

Deeply nested if..then statements are hard to read.

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

			

public class Foo {
 public void bar() {
  int x=2;
  int y=3;
  int z=4;
  if (x>y) {
   if (y>z) {
    if (z==x) {
     // this is officially out of control now
    }
   }
  }
 }
}

    
		

ReassigningParameters

Reassigning values to parameters is a questionable practice. Use a temporary local variable instead.

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

			

public class Foo {
 private void foo(String bar) {
  bar = "something else";
 }
}

    
		

SubClassingDesign

Detects when constructors, readObject, and clone invoke an overridable method (one that is not static, private, or final)

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

			

public class SubClassDesignExample {
    public SubClassDesignExample(){
        //this.x = x;
        Object h = new Object();
        foo();
    }
    public void foo(){
        
    }
}

    
		

FinalFieldCouldBeStatic

If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead in each object

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

			
  
public class Foo {
 public final int BAR = 42; // this could be static and save some space
}
  
      
		

NonStaticInitializer

A nonstatic initializer block will be called any time a constructor is invoked (just prior to invoking the constructor). While this is a valid language construct, it is rarely used and is confusing.

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

			
   
public class MyClass {
 // this block gets run before any call to a constructor
 {
  System.out.println("I am about to construct myself");
 }
}
   
       
		

DefaultLabelNotLastInSwitchStmt

The default label in a switch statement should be the last label, by convention. Most programmers will expect the default label (if present) to be the last one.

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

			
   
      switch (a)
      {
         case 1:
            // do something
            break;
         default:
            // the default case should be last, by convention
            break;
         case 2:
            break;
      }