MethodLength
Since Checkstyle 3.0
Description
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 |
---|---|---|---|---|
countEmpty | Control whether to count empty lines and comments. | boolean | true |
3.2 |
max | Specify the maximum number of lines allowed. | int | 150 |
3.0 |
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 so that it accepts at most 4 lines:
<module name="Checker">
<module name="TreeWalker">
<module name="MethodLength">
<property name="max" value="4"/>
</module>
</module>
</module>
Example:
public class Example1 {
// violation below, 'Method Example1 length is 5 lines (max allowed is 4)'
public Example1() {
int var1 = 2;
int var2 = 4;
int sum = var1 + var2;
}
// ok, as it is less than 4 lines
public Example1(int a) {
int var1 = 2;
int sum = var1 + a;
}
// violation below, 'Method firstMethod length is 6 lines (max allowed is 4)'
public void firstMethod() {
int index = 0;
if (index < 5) {
index++;
}
}
public void secondMethod() {
// comments are counted by default
System.out.println("line 3");
}
// violation below, 'Method thirdMethod length is 5 lines (max allowed is 4)'
public void thirdMethod() {
// empty line above is counted by default,just like this comment
System.out.println("line 4");
}
record MyBadRecord() {
// violation below, 'Method MyBadRecord length is 5 lines (max allowed is 4)'
public MyBadRecord {
System.out.println("line3");
System.out.println("line4");
}
}
}
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 Example2 {
// ok, CTOR_DEF is not in configured tokens
public Example2() {
int var1 = 2;
int var2 = 4;
int sum = var1 + var2;
}
// ok, CTOR_DEF is not in configured tokens
public Example2(int a) {
int var1 = 2;
int sum = var1 + a;
}
// violation below, 'Method firstMethod length is 6 lines (max allowed is 4)'
public void firstMethod() {
int index = 0;
if (index < 5) {
index++;
}
}
public void secondMethod() {
// comments are counted by default
System.out.println("line 3");
}
// violation below, 'Method thirdMethod length is 5 lines (max allowed is 4)'
public void thirdMethod() {
// empty line above is counted by default,just like this comment
System.out.println("line 4");
}
record MyBadRecord() {
// ok, COMPACT_CTOR_DEF is not in configured tokens
public MyBadRecord {
System.out.println("line3");
System.out.println("line4");
}
}
}
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 Example3 {
// ok, CTOR_DEF is not in configured tokens
public Example3() {
int var1 = 2;
int var2 = 4;
int sum = var1 + var2;
}
// ok, CTOR_DEF is not in configured tokens
public Example3(int a) {
int var1 = 2;
int sum = var1 + a;
}
// violation below, 'Method firstMethod length is 6 lines (max allowed is 4)'
public void firstMethod() {
int index = 0;
if (index < 5) {
index++;
}
}
public void secondMethod() {
// countEmpty property is false,so this line doesn't count
System.out.println("line 3");
}
public void thirdMethod() {
// countEmpty property is false,so this line and the line above don't count
System.out.println("line 4");
}
record MyBadRecord() {
// ok, COMPACT_CTOR_DEF is not in configured tokens
public MyBadRecord {
System.out.println("line3");
System.out.println("line4");
}
}
}
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