MethodLength

Since Checkstyle 3.0

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 since
max Specify the maximum number of lines allowed. int 150 3.0
countEmpty Control whether to count empty lines and comments. boolean true 3.2
tokens tokens to check subset of tokens METHOD_DEF , CTOR_DEF , COMPACT_CTOR_DEF . METHOD_DEF , CTOR_DEF , COMPACT_CTOR_DEF . 3.0

Examples

To configure the check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="MethodLength"/>
  </module>
</module>
        

Example:

public class MyClass {
  public MyClass() {  // constructor (line 1)
      /* line 2
          ...
         line 150 */
  } // line 151, violation, as it is over 150

  public void firstExample() { // line 1

      // line 3
      System.out.println("line 4");
      /* line 5
         line 6 */
  } // line 7, OK, as it is less than 150

  public void secondExample() { // line 1
      // line 2
      System.out.println("line 3");

      /* line 5
          ...
         line 150 */
  } // line 151, violation, as it is over 150
}
        

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

<module name="Checker">
  <module name="TreeWalker">
    <module name="MethodLength">
      <property name="tokens" value="METHOD_DEF"/>
      <property name="max" value="4"/>
    </module>
  </module>
</module>
        

Example:

public class MyTest {
  public MyTest()  {          // constructor (line 1)
      int var1 = 2;           // line 2
      int var2 = 4;           // line 3
      int sum = var1 + var2;  // line 4
  } // line 5, OK, constructor is not mentioned in the tokens

  public void firstMethod() { // line 1
      // comment (line 2)
      System.out.println("line 3");
  } // line 4, OK, as it allows at most 4 lines

  public void secondMethod() { // line 1
      int index = 0;   // line 2
      if (index < 5) { // line 3
          index++;     // line 4
      }                // line 5
  } // line 6, violation, as it is over 4 lines

  public void thirdMethod() { // line 1

      // comment (line 3)
      System.out.println("line 4");
  } // line 5, violation, as it is over 4 lines
}
        

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

<module name="Checker">
  <module name="TreeWalker">
    <module name="MethodLength">
      <property name="tokens" value="METHOD_DEF"/>
      <property name="max" value="4"/>
      <property name="countEmpty" value="false"/>
    </module>
  </module>
</module>
        

Example:

public class MyTest {
  public MyTest()  {          // constructor (line 1)
      int var1 = 2;           // line 2
      int var2 = 4;           // line 3
      int sum = var1 + var2;  // line 4
  } // line 5, OK, constructor is not mentioned in the tokens

  public void firstMethod() { // line 1
      // comment - not counted as line
      System.out.println("line 2");
  } // line 3, OK, as it allows at most 4 lines

  public void secondMethod() { // line 1
      int index = 0;   // line 2
      if (index < 5) { // line 3
          index++;     // line 4
      }                // line 5
  } // line 6, violation, as it is over 4 lines

  public void thirdMethod() { // line 1

      // comment - not counted as line
      System.out.println("line 2");
  } // line 3, OK, as it allows at most 4 lines
}
        

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

TreeWalker