The Basic checker set contains a collection of good practices which everyone should follow.
Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported.
Here's an example of code that would trigger this checker:
public void doSomething() { try { FileInputStream fis = new FileInputStream("/tmp/bugger"); } catch (IOException ioe) { // not good } }
Empty If Statement finds instances where a condition is checked but nothing is done about it.
Here's an example of code that would trigger this checker:
if (foo == 0) { // why bother checking up on foo? }
Empty While Statement finds all instances where a while statement does nothing. If it is a timing loop, then you should use Thread.sleep() for it; if it's a while loop that does a lot in the exit expression, rewrite it to make it clearer.
Here's an example of code that would trigger this checker:
while (a == b) { // not good }
Avoid empty try blocks - what's the point?
Here's an example of code that would trigger this checker:
// this is bad public void bar() { try { } catch (Exception e) { e.printStackTrace(); } }
Avoid empty finally blocks - these can be deleted.
Here's an example of code that would trigger this checker:
// this is bad public void bar() { try { int x=2; } finally { } }
Avoid empty switch statements.
Here's an example of code that would trigger this checker:
public class Foo { public void bar() { int x = 2; switch (x) { // once there was code here // but it's been commented out or something } } }
Avoid jumbled loop incrementers - it's usually a mistake, and it's confusing even if it's what's intended.
Here's an example of code that would trigger this checker:
public class JumbledIncrementerRule1 { public void foo() { for (int i = 0; i < 10; i++) { for (int k = 0; k < 20; i++) { System.out.println("Hello"); } } } }
Some for loops can be simplified to while loops - this makes them more concise.
Here's an example of code that would trigger this checker:
public class Foo { void bar() { for (;true;) true; // No Init or Update part, may as well be: while (true) } }
Avoid unnecessary temporaries when converting primitives to Strings
Here's an example of code that would trigger this checker:
public String convert(int x) { // this wastes an object String foo = new Integer(x).toString(); // this is better return Integer.toString(x); }
Avoid returning from a finally block - this can discard exceptions.
Here's an example of code that would trigger this checker:
public class Bar { public String bugga() { try { throw new Exception( "My Exception" ); } catch (Exception e) { throw e; } finally { return "A. O. K."; // Very bad. } } }
Avoid empty synchronized blocks - they're useless.
Here's an example of code that would trigger this checker:
// this is bad public void bar() { synchronized (this) {} }
Avoid unnecessary return statements
Here's an example of code that would trigger this checker:
// this is bad public void bar() { int x = 42; return; }
An empty static initializer was found.
Here's an example of code that would trigger this checker:
public class Foo { // why are there no statements in this static block? static {} }
Do not use "if" statements that are always true or always false.
Here's an example of code that would trigger this checker:
public class Foo { public void close() { if (true) { // ... } } }
Detects when the class only has one method of equals/hashCode but another from java.lang.Object
Here's an example of code that would trigger this checker:
public class HEmismatchExample { public boolean equals(Object x){ return false; } }