Controversial RulesThe Controversial Ruleset contains rules that, for whatever reason, are considered controversial. They are separated out here to allow people to include as they see fit via custom rulesets. This ruleset was initially created in response to discussions over UnnecessaryConstructorRule which Tom likes but most people really dislike :-) UnnecessaryConstructorRuleUnnecessary constructor detects when a constructor is not necessary; i.e., when there's only one constructor, it's public, has an empty body, and takes no arguments. This rule is defined by the following XPath expression: //UnmodifiedClassDeclaration /ClassBody[count(ClassBodyDeclaration/ConstructorDeclaration)=1] /ClassBodyDeclaration/ConstructorDeclaration [@Public='true'] [not(FormalParameters/*)] [not(BlockStatement)] [not(NameList)] [count(ExplicitConstructorInvocation/Arguments/ArgumentList/Expression)=0] Here's an example of code that would trigger this rule: public class Foo { public Foo() {} } NullAssignmentAssigning a "null" to a variable (outside of its declaration) is usually in bad form. Some times, the assignment is an indication that the programmer doesn't completely understand what is going on in the code. NOTE: This sort of assignment may in rare cases be useful to encourage garbage collection. If that's what you're using it for, by all means, disregard this rule :-) Here's an example of code that would trigger this rule: public class Foo { public void bar() { Object x = null; // This is OK. x = new Object(); // Big, complex piece of code here. x = null; // This is BAD. // Big, complex piece of code here. } } OnlyOneReturnA method should have only one exit point, and that should be the last statement in the method. Here's an example of code that would trigger this rule: public class OneReturnOnly1 { public void foo(int x) { if (x > 0) { return "hey"; // oops, multiple exit points! } return "hi"; } } UnusedModifierUnused modifiers are, well, unused. This rule is defined by the following XPath expression: //InterfaceDeclaration//MethodDeclaration[@Public='true' or @Abstract = 'true'] Here's an example of code that would trigger this rule: public interface Foo { public abstract void bar(); // both abstract and public are ignored by the compiler } AssignmentInOperandRuleAvoid assigments in operands; this can make code more complicated and harder to read. This rule is defined by the following XPath expression: //*[name()='WhileStatement' or name()='IfStatement'][Expression//AssignmentOperator] Here's an example of code that would trigger this rule: public class Foo { public void bar() { int x = 2; if ((x = getX()) == 3) { System.out.println("3!"); } } private int getX() { return 3; } } AtLeastOneConstructorEach class should declare at least one constructor. Note that this rule is the opposite of UnnecessaryConstructorRule, so running them at the same time will result on a rule violation on every class. Here's an example of code that would trigger this rule: public class Foo { // no constructor! not good! } FinalizeShouldBeProtectedIf you override finalize(), make it protected. Otherwise, subclasses may not called your implementation of finalize. This rule is defined by the following XPath expression: //MethodDeclaration[@Protected="false"] /MethodDeclarator[@Image="finalize"] [not(FormalParameters/*)] Here's an example of code that would trigger this rule: public class Foo { public void finalize() { // do something } } |