FileLength

Since Checkstyle 5.0

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 since
fileExtensions Specify the file extensions of the files to process. String[] all files 5.0
max Specify the maximum number of lines allowed. int 2000 3.2

Examples

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

<module name="Checker">
  <module name="FileLength"/>
</module>
        

Example:

public class Example1 {
  public void myTest() {
    // small class less than 2000 lines
    String test = "Some content";
  }
}
        

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

<module name="Checker">
  <module name="FileLength">
    <property name="max" value="5"/>
  </module>
</module>
        

Example:

public class Example2 {
  public void myTest() {
    // small class with more than 5 lines
    String test = "Some content"; // there is violation
  }
}
        

To configure the check to accept files with up to 5 lines and with txt extension only:

<module name="Checker">
  <module name="FileLength">
    <property name="max" value="5"/>
    <property name="fileExtensions" value="txt"/>
  </module>
</module>
        

Example:

public class Example3 {
  public void myTest() {
    // small class with more than 5 lines
    String test = "Some content"; // no violation as only txt files are validated
  }
}
        

Example of Usage

Violation Messages

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

Package

com.puppycrawl.tools.checkstyle.checks.sizes

Parent Module

Checker