AnonInnerLength

Description

Checks for long anonymous inner classes.

Rationale: If an anonymous inner class becomes very long it is hard to understand and to see the flow of the method where the class is defined. Therefore long anonymous inner classes should usually be refactored into a named inner class. See also Bloch, Effective Java, p. 93.

Properties

name description type default value
max maximum allowable number of lines integer 20

Examples

To configure the check to accept files with up to 60 lines:

<module name="AnonInnerLength">
      <property name="max" value="60"/>
</module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

ExecutableStatementCount

Description

Restricts the number of executable statements to a specified limit.

Properties

name description type default value
max the maximum threshold allowed integer 30
tokens tokens to check subset of tokens CTOR_DEF, METHOD_DEF, INSTANCE_INIT, STATIC_INIT. CTOR_DEF, METHOD_DEF, INSTANCE_INIT, STATIC_INIT.

Examples

To configure the check:

<module name="ExecutableStatementCount"/>
        

To configure the check with a threshold of 20 for constructor and method definitions:

<module name="ExecutableStatementCount">
    <property name="max" value="20"/>
    <property name="tokens" value="CTOR_DEF,METHOD_DEF"/>
</module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

FileLength

Description

Checks for long source files.

Rationale: If a source file becomes very long it is hard to understand. Therefore long classes should usually be refactored into several individual classes that focus on a specific task.

Properties

name description type default value
max maximum allowable number of lines integer 2000
fileExtensions file type extension of files to process String Set {}

Examples

To configure the check to accept files with up to 1500 lines:

<module name="FileLength">
      <property name="max" value="1500"/>
</module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

Checker

LineLength

Description

Checks for long lines.

Rationale: Long lines are hard to read in printouts or if developers have limited screen space for the source code, e.g. if the IDE displays additional information like project tree, class hierarchy, etc.

Properties

name description type default value
ignorePattern pattern for lines to ignore regular expression ^$
max maximum allowable line length integer 80

Examples

To configure the check to accept lines up to 120 characters long:

<module name="LineLength">
    <property name="max" value="120"/>
</module>
        

To configure the check to ignore lines that begin with " * ", followed by just one word, such as within a Javadoc comment:

<module name="LineLength">
   <property name="ignorePattern" value="^ *\* *[^ ]+$"/>
</module>
        

Notes

  • The calculation of the length of a line takes into account the number of expanded spaces for a tab character ('\t'). The default number of spaces is 8. To specify a different number of spaces, the user can set TreeWalker property tabWidth which applies to all Checks, including LineLength; or can set property tabWidth for LineLength alone.
  • Package and import statements (lines matching pattern ^(package|import) .*) are not verified by this check.

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

MethodCount

Description

Checks the number of methods declared in each type. This includes the number of each scope (private, package, protected and public) as well as an overall total.

Properties

name description type default value
maxTotal maximum allowable number of methods at all scope levels integer 100
maxPrivate maximum allowable number of private methods integer 100
maxPackage maximum allowable number of package methods integer 100
maxProtected maximum allowable number of protected methods integer 100
maxPublic maximum allowable number of public methods integer 100
tokens tokens to check subset of tokens CLASS_DEF, ENUM_CONSTANT_DEF, ENUM_DEF, INTERFACE_DEF. CLASS_DEF, ENUM_CONSTANT_DEF, ENUM_DEF, INTERFACE_DEF.

Examples

To configure the check with defaults:

<module name="MethodCount"/>
        

To configure the check to allow at most 30 methods per type:

<module name="MethodCount">
      <property name="maxTotal" value="30"/>
</module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

MethodLength

Description

Checks for long methods and constructors.

Rationale: If a method becomes very long it is hard to understand. Therefore long methods should usually be refactored into several individual methods that focus on a specific task.

Properties

name description type default value
max maximum allowable number of lines integer 150
countEmpty whether to count empty lines and single line comments of the form // boolean true
tokens tokens to check subset of tokens METHOD_DEF, CTOR_DEF. METHOD_DEF, CTOR_DEF.

Examples

To configure the check:

<module name="MethodLength"/>
        

To configure the check so that it accepts methods with at most 60 lines:

<module name="MethodLength">
   <property name="tokens" value="METHOD_DEF"/>
   <property name="max" value="60"/>
</module>
        

To configure the check so that it accepts methods with at most 60 lines, not counting empty lines and single line comments:

<module name="MethodLength">
   <property name="tokens" value="METHOD_DEF"/>
   <property name="max" value="60"/>
   <property name="countEmpty" value="false"/>
</module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

OuterTypeNumber

Description

Checks for the number of types declared at the outer (or root) level in a file.

Rationale: It is considered good practice to only define one outer type per file.

Properties

name description type default value
max maximum allowable number of outer types integer 1

Examples

To configure the check to accept 1 outer type per file:

<module name="OuterTypeNumber"/>
        

To configure the check to accept 2 outer types per file:

<module name="OuterTypeNumber">
      <property name="max" value="2"/>
</module>
        

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker

ParameterNumber

Description

Checks the number of parameters of a method or constructor.

Properties

name description type default value
max maximum allowable number of parameters integer 7
ignoreOverriddenMethods Ignore number of parameters for methods with @Override annotation boolean false
tokens tokens to check subset of tokens METHOD_DEF, CTOR_DEF. METHOD_DEF, CTOR_DEF.

Examples

To configure the check:

<module name="ParameterNumber"/>
        

To configure the check to allow 10 parameters for a method:

<module name="ParameterNumber">
   <property name="max" value="10"/>
   <property name="tokens" value="METHOD_DEF"/>
</module>
        

To configure the check to ignore number of parameters for methods with @Override or @java.lang.Override annotation.

Rationale: developer may need to override method with many parameters from 3-rd party library. In this case developer has no control over number of parameters.

<module name="ParameterNumber">
    <property name="ignoreOverriddenMethods" value="true"/>
    <property name="tokens" value="METHOD_DEF"/>
</module>
          

Java code example

@Override
public void needsLotsOfParameters(int a, int b, int c, int d, int e, int f, int g, int h) {
    ...
}
          

Example of Usage

Error Messages

All messages can be customized if the default message doesn't suite you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

TreeWalker