Mar 8, 2018 11:20:56 AM seb leridon avatar   835

Checkstyle / PMD rules

The following table describes the rules for Checkstyle and PMD tools for code analysis. The abbreviation c in the corresponding column tool at Checkstyle, p at PMD

Functionality Value Tools Name of the rule in the tool
Naming rules
Local variables, final ^ [A-Z] * $ - strict (TODO): strName, nName or lName c LocalFinalVariableName
Local variables, non-final- attention, including catch parameters ^ [a-z] [a-zA-Z0-9] * $ - strict (TODO): strName, nName or lName c LocalVariableName
Non-static attributes ^ _ [a-z] (_? [A-zA-Z0-9]) * $ - strict (TODO): strName, nName, or lName c MemberName
Parameters ^ [a-z] [a-zA-Z0-9] * $ - strict (TODO): strName, nName or lName c ParameterName
Constants (static and final fields) ^ [A-Z0-9] * $ c ConstantName
Static, non-final variables ^ _ [a-z] (_? [A-zA-Z0-9]) * $ - strict (TODO): strName, nName or lName c StaticVariableName
Method [az] [a-zA-Z0-9] * $ c MethodName
Class (and interface) ^ [A-Z] [a-zA-Z0-9] * $ c TypeName
Package . ^ En \ .paris \ [az \.] * $ C PackageName
Abstract classes strict only: abstract keyword c AbstractClassName
Maximum length of a variable 80 - strict 50 p LongVariable
Min length of the name of a method 3 p ShortMethodNameRule
Min length of the name of a variable not retained p ShortVariable
Rules not retained (redundant)
PMD VariableNamingConventionRule, MethodNamingConvention,ClassNamingConventionRule, AbstractNamingRule
Functionality Value Tools Name of the rule in the tool
File header
Files must all contain the same header $ {checkstyle.header.file} c header
The header must match a regular expression not retained c RegexpHeader
Functionality Value Tools Name of the rule in the tool
Useless import: duplicate, java.lang. * Or the same package na c RedundantImports
Same as above + unused import na c UnusedImports
Imports should not use * na c AvoidStarImport
Check the import order (java, then javax, ...) not accepted c ImportOrder
Reject the illegal packages (by default, those of sun.) Na c IllegalImport
Rules not retained (redundant)
PMD ImportFromSamePackage, UnusedImports, DontimportJavaLang,DuplicateImport
Functionality Value Tools Name of the rule in the tool
Non respect of length
Maximum length of a method default: 100 lines, counting comments and empty lines p ExcessiveMethodLength
Maximum length of a class default: 1000 lines, counting comments and blank lines p ExcessiveClassLength
Max number of parameters default: 7 c ParameterNumber
Complexity: number of decision points (if, while, do, for,?:, catch, switch,, OR and case) + 1 for the method 10 - strict only p CyclomaticComplexityRule
Max number of executable commands not retained c ExecutableStatementCount
Maximum file length not retained c FileLenght
Maximum length of a line not retained c LineLength
Max length for an anonymous inner class not retained c AnonInnerLenght
Number of methods and public attributes 45 p ExcessivePublicCount
Rules not retained (redundant)
PMD ExcessiveParameterList
Checkstyle MethodLenght, CyclomaticComplexity
Functionality Value Tools Name of the rule in the tool
Unauthorized space if one of the elements of the for instance is empty: for (int i ;; i ++) not retained c EmptyForIteratorPad
Only = and == can be followed by a space token = ASSIGN, EQUAL c WhitespaceAround
Parentheses must have spaces space c ParenPad
Checks that there is no space after some operators not retained c NoWhiteSpaceAfter
Check that there is no space before some operators not retained c NoWhiteSpaceBefore
Checks how to handle long lines after operators not retained c OperatorWrap
Check spaces for the particular case of cast not retained c TypeCastParenPad
Check that there is no tab in the source code not retained c TabCharacter
Check that the items listed are followed by a space not retained c WhiteSpaceAfter
Functionality Value Tools Name of the rule in the tool
The order of the qualifiers is: public - protected - private - abstract - static - final - transient - volatile - synchronized - native - strictfp na c ModifyOrder
The interfaces are neither public nor abstract, the methods and private attributes of a final class are not final, in an interface the variables are neither public nor static nor final na c RedundantModifier
Functionality Value Tools Name of the rule in the tool
Blocks can not be empty (affects: catch, do, else, finally, if, for, try, while and init) na c EmptyBlock
Switch blocks are non-empty na p EmptySwitchStatements
Synchronized blocks are non-empty na p EmptySynchronizedBlock
Verifies that opening braces ({) go to the line na c LeftCurly
Verifies that code blocks are surrounded by braces {} na c NeedBraces
Verifies that closing braces (}) go to the line na c RightCurly
Check that there are no blocks {} left in the code (left after debugging) na c AvoidNestedBlocks
Rules not retained (redundant)
PMD EmptyCatchBlock, EmptyIfStmt, EmptyWhileStmt, EmptyTryBlock, EmptyFinallyBlock, IfStmtsMustUseBraces, WhileLoopsMustUseBracesRule, IfElseStmtsMustUseBracesRule, ForLoopsMustUseBracesRule
Functionality Value Tools Name of the rule in the tool
Code problems
Avoid syntaxes of type ?: not retained c AvoidInlineConditionals
Check if a test is nested in a synchronize that is itself nested in the same test, EX: if (theInstance == null) {synchronized (MySingleton.class) {if (theInstance == null) {theInstance = new MySingleton (); }}} na c Double checked locking
Detect empty statements (;) na c EmptyStatement
If we overload equals [resp hashcode] then we must override hashcode [resp equals] na c EqualsHashCode
A local variable or parameter should not overwrite an na c HiddenField
Simplify the return of boolean - ex: one must have return (! IsValid ()) instead of if (isValid ()) return false; else return true; na c SimplifyBooleanReturn
No need to test with == true or! False ... na c SimplifyBooleanExpression
Checks that we do not have a number that is not a constant (except -, 0, 1, and 2) strict only c MagicNumber
Check that the switch is a default case na c MissingSwitchDefault
Check that there is not a comma after the last element by initializing an array na c ArrayTrailingComma
Checks that if we define an equals we also override equals (java.lang.Object) na c CovariantEquals
Checks that we define well in order: static, public, protected attributes, those for the package (without getter or setter), private; the builders; the methods. na c DeclarationOrder
Verifies that objects are not unnecessarily initialized (such as int i = 0; or object o = null;) na c ExplicitInitialization
Parameters and variables that are never changed must be declared final not retained c FinalLocalVariable
Checks that there are no bad exceptions ( java.lang.Exception , java.lang.Error or java.lang.RuntimeException ) not retained c FinalLocalVariable
Checks that factory is used when needed not retained c IllegalInstantiation
Forbidden some token (like ++) not retained c IllegalToken
Prohibits the values ​​defined by type (eg: no string that has the value a href, no integer that starts with 0, ...) not retained c IllegalTokenText
Forbidden certain types / classes not retained c IllegalType
Deny assignments in expressions na c InnerAssignment
Limit the number of nested if-else 3 c NestedIfDepth
Limit the number of nested tries 3 c NestedTryDepth
Each class must have a package na c PackageDeclaration
Parameters must not be assigned na c ParameterAssignment
Check that the same exception, unchecked exception, or exception that is a subclass of an already launched exception is not thrown twice na c RedundantThrows
Limit the number of return 5 c ReturnCount
Checks that we do not compare strings with == or!Na c StringLiteralEquality
Check that when overloaded clone (), we call super.clone () na c SuperClone
Checks that when overfinishing finalize (), we call super.finalize () na c SuperFinalize
Check loop nesting for 2 p JumbledIncrementer
Certain for loops can / should be replaced by while na p ForLoopShouldBeWhileLoop
It is better to convert in the return than in a temporary variable na p UnnecessaryConversionTemporaryRule
Check that we do not make a return in a finally unresolved p ReturnFromFinallyBlock
Detects the return; needless na p UnnecessaryReturn
Detects unused private fields na p UnusedPrivateField
Detects unused local variables na p UnusedLocalVariable
Detects unused private methods na p UnusedPrivateMethod
Detects unused parameters na p UnusedFormalParameter
A class that only has static methods should be a singleton na strictly only p UnusedLocalVariable
Using the interface (ex: Set) rather than its implementation (ex HashSet) na p LooseCouplingRule
It's better to use a local variable than to reuse a parameter na p AvoidReassigningParametersRule
Check the size of the switch (max length of the box) 5 p SwitchDensity
ConstructorCallsOverridableMethodRule builder must not call overloaded method na p
Do not call a private constructor outside the constructor class na p AccessorClassGenerationRule
A constant final field must be static na p FinalFieldCouldBeStatic
Connections must always be closed strict only p CloseConnectionRule
If the same string is used more than once, it must become a constant 4 p AvoidDuplicateLiterals
Do not instantiate strings with a new na p StringInstantiation
No need to use toString () on a string na p StringToString
Consider that an object is too coupled with another if the number of attributes, local variables and types returned is too important 20 p CouplingBetweenObjectsRule
Too many imports indicate that the object is too coupled 25 p ExcessiveImportsRule
A method must not throw exception of type Exception not retained p SignatureDeclareThrowsException
It is cleaner to catch each type of exception (rather than catcher Exception and then make instanceof) na p ExceptionTypeChecking
Rules not retained (redundant)
PMD OverrideBothEqualsAndHashcodeRule, DoubleCheckedLockingRule, SimplifyBooleanReturnsRule, SimplifyBooleanExpressions, SwitchStmtsShouldHaveDefault, AvoidDeeplyNestedIfStmts, BooleanInstantiation, ProperCloneImplementationRule, AvoidCatchingThrowable, AssignmentInOperandRule
Functionality Value Tools Name of the rule in the tool
Design issues
A non-private and non-static method of a class that can be specialized must be either abstract, or final, or empty na strict only c DesignForExtension
A class with only private constructors must be declared final na c FinalClass
A class that contains only static methods must not have a public constructor na c HideUtilityClassConstructor
Interfaces must only define types (no interface that contains no method, only constants) na strict only c InterfaceIsType
Only static and final attributes can be public na c VisibilityModifier
Checks that exception classes have only end fields na c MutableException
Checks the max number of throw not retained c ThrowsCount
Detects when there is only one constructor, public, empty and without arguments unresolved p UnnecessaryConstructorRule
Verifies that we do not assign null to objects na p NullAssignment
Check that there is only one return per function not retained p OnlyOneReturn
Detects unnecessary static public etc na p UnusedModifier
Causes each class to have an constructor unbound p AtLeastOneConstructor
Declare finalize () protected if overloaded na p FinalizeShouldBeProtected
Functionality Value Tools Name of the rule in the tool
Each package must have a package.html unselected c PackageHtml
Check that there is a javadoc for the class (or interface) na c JavadocType
Variables must be commented on) strict only c JavadocVariable
Classes must be commented on (at least: @return, @param, @throws and @see) na c JavadocMethod
Validate the comments content: complete sentence, well-formed HTML tags, ... not retained c JavadocStyle
Functionality Value Tools Name of the rule in the tool
It is necessary to use assertions with message na p JUnitAssertionsShouldIncludeMessageRule
suite () must be public AND static na p JUnitStaticSuite
Checks that setUp and tearDown are spelled correctly na p JUnitSpelling
Functionality Value Tools Name of the rule in the tool
Detects duplicate code not retained c StrictDuplicateCode
Verifies that the declaration of a table conforms to the style chosen (Java-style: public static void main (String [] args) or C-style: public static void main (String args []) ) OR Java style c ArrayTypeStyle
Restricts some tokens inside some others not retained c DescendantToken
Verifies that the parameters are final (except for interfaces) not retained c FinalParameters
grep for a given regular expression System \ .out \ .println, System \ .exit c GenericIllegalRegexp
Check indentation not retained c Indentation
Checks that all files end with a newline unselected c NewlineAtEndOfFile
Seeking TODO Comments: not retained c TodoComment
Check that the properties files of different languages ​​contain the same keys na c Translation
Detects non commented main methods unselected c UncommentedMain
Long-type constants must start with L not held c UpperEll