Skip to content

Commit

Permalink
Issue #11474: update doc for MethodLength
Browse files Browse the repository at this point in the history
  • Loading branch information
kaungmyat-htet authored and strkkk committed Apr 12, 2022
1 parent d1124ab commit fcba302
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 10 deletions.
Expand Up @@ -73,26 +73,115 @@
* <module name="MethodLength"/>
* </pre>
* <p>
* To configure the check so that it accepts methods with at most 60 lines:
* Example:
* </p>
* <pre>
* public class MyClass {
* public MyClass() { // constructor (line 1)
* /&#42; line 2
* ...
* line 150 &#42;/
* } // line 151, violation, as it is over 150
*
* public void firstExample() { // line 1
*
* // line 3
* System.out.println("line 4");
* /&#42; line 5
* line 6 &#42;/
* } // line 7, OK, as it is less than 150
*
* public void secondExample() { // line 1
* // line 2
* System.out.println("line 3");
*
* /&#42; line 5
* ...
* line 150 &#42;/
* } // line 151, violation, as it is over 150
* }
* </pre>
* <p>
* To configure the check so that it accepts methods with at most 4 lines:
* </p>
* <pre>
* &lt;module name="MethodLength"&gt;
* &lt;property name="tokens" value="METHOD_DEF"/&gt;
* &lt;property name="max" value="60"/&gt;
* &lt;property name="max" value="4"/&gt;
* &lt;/module&gt;
* </pre>
* <p>
* To configure the check so that it accepts methods with at most 60 lines,
* Example:
* </p>
* <pre>
* 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 &#60; 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
* }
* </pre>
* <p>
* To configure the check so that it accepts methods with at most 4 lines,
* not counting empty lines and comments:
* </p>
* <pre>
* &lt;module name="MethodLength"&gt;
* &lt;property name="tokens" value="METHOD_DEF"/&gt;
* &lt;property name="max" value="60"/&gt;
* &lt;property name="max" value="4"/&gt;
* &lt;property name="countEmpty" value="false"/&gt;
* &lt;/module&gt;
* </pre>
* <p>
* Example:
* </p>
* <pre>
* 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 &#60; 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
* }
* </pre>
* <p>
* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
* </p>
* <p>
Expand Down
93 changes: 87 additions & 6 deletions src/xdocs/config_sizes.xml
Expand Up @@ -960,29 +960,110 @@ public class ExampleClass {
<source>
&lt;module name="MethodLength"/&gt;
</source>

<p>Example:</p>
<source>
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
}
</source>
<p>
To configure the check so that it accepts methods with at most 60
To configure the check so that it accepts methods with at most 4
lines:
</p>
<source>
&lt;module name="MethodLength"&gt;
&lt;property name="tokens" value="METHOD_DEF"/&gt;
&lt;property name="max" value="60"/&gt;
&lt;property name="max" value="4"/&gt;
&lt;/module&gt;
</source>

<p>Example:</p>
<source>
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 &#60; 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
}
</source>
<p>
To configure the check so that it accepts methods with at most 60
To configure the check so that it accepts methods with at most 4
lines, not counting empty lines and comments:
</p>
<source>
&lt;module name="MethodLength"&gt;
&lt;property name="tokens" value="METHOD_DEF"/&gt;
&lt;property name="max" value="60"/&gt;
&lt;property name="max" value="4"/&gt;
&lt;property name="countEmpty" value="false"/&gt;
&lt;/module&gt;
</source>
<p>Example:</p>
<source>
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 &#60; 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
}
</source>
</subsection>

<subsection name="Example of Usage" id="MethodLength_Example_of_Usage">
Expand Down

0 comments on commit fcba302

Please sign in to comment.