Checks for Size Violations

Checkstyle Logo

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

Examples

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

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

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker

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.
  • Support for the special handling of imports in CheckStyle Version 2 has been dropped as it is a special case of regexp: The user can set property ignorePattern to ^import and achieve the same effect.

Package

com.puppycrawl.tools.checkstyle.checks

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
tokens blocks 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>
        

Package

com.puppycrawl.tools.checkstyle.checks

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
tokens declarations 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>
      

Package

com.puppycrawl.tools.checkstyle.checks

Parent Module

TreeWalker


Back to the Checkstyle Home Page