Rule | Description | Example | KPI |
---|---|---|---|
ControlStatementBraces | Enforce a policy for braces on control statements. It is recommended to use braces on ‘if … else’ statements and loop statements, even if they are optional. More info | while (true) // not recommended x++; while (true) { // preferred approach x++; } | Maintainability |
IdenticalCatchBranches | Identical catch branches use up vertical space and increase the complexity of code without adding functionality. It’s better style to collapse identical branches into a single multi-catch branch. More info | try { // do something } catch (IllegalArgumentException e) { throw e; } catch (IllegalStateException e) { // Can be collapsed into the previous block throw e; } try { // do something } catch (IllegalArgumentException | IllegalStateException e) { // This is better throw e; } | Maintainability |
LinguisticNaming | This rule finds Linguistic Naming Antipatterns. It checks for fields, that are named, as if they should be boolean but have a different type. It also checks for methods, that according to their name, should return a boolean, but don’t. More info | public class LinguisticNaming { int isValid; // the field name indicates a boolean, but it is an int. boolean isTrue; // correct type of the field void myMethod() { int hasMoneyLocal; // the local variable name indicates a boolean, but it is an int. boolean hasSalaryLocal; // correct naming and type } // the name of the method indicates, it is a boolean, but the method returns an int. int isValid() { return 1; } // correct naming and return type boolean isSmall() { return true; } // the name indicates, this is a setter, but it returns something int setName() { return 1; } // the name indicates, this is a getter, but it doesn't return anything void getName() { // nothing to return? } // the name indicates, it transforms an object and should return the result void toDataType() { // nothing to return? } // the name indicates, it transforms an object and should return the result void grapeToWine() { // nothing to return? } } | Maintainability |
LocalVariableNamingConventions | Configurable naming conventions for local variable declarations and other locally-scoped variables. More info | class Foo { void bar() { int localVariable = 1; // This is in camel case, so it's ok int local_variable = 1; // This will be reported unless you change the regex final int i_var = 1; // final local variables can be configured separately try { foo(); } catch (IllegalArgumentException e_illegal) { // exception block parameters can be configured separately } } } | Maintainability |