774 lines
29 KiB
XML
774 lines
29 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<document xmlns="http://maven.apache.org/XDOC/2.0"
|
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">
|
|
|
|
<head>
|
|
<title>Metrics</title>
|
|
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"/>
|
|
<script type="text/javascript" src="js/anchors.js"/>
|
|
<script type="text/javascript" src="js/google-analytics.js"/>
|
|
<link rel="icon" href="images/favicon.png" type="image/x-icon" />
|
|
<link rel="shortcut icon" href="images/favicon.ico" type="image/ico" />
|
|
</head>
|
|
|
|
<body>
|
|
<section name="Content">
|
|
<macro name="toc">
|
|
<param name="fromDepth" value="1"/>
|
|
<param name="toDepth" value="1"/>
|
|
</macro>
|
|
</section>
|
|
|
|
<section name="BooleanExpressionComplexity">
|
|
<subsection name="Description">
|
|
<p>
|
|
Restrict the number of number of <code>&&</code>, <code>||</code>,
|
|
<code>&</code>, <code>|</code>
|
|
and <code>^</code> in an expression.
|
|
</p>
|
|
|
|
<p>
|
|
Rationale: Too many conditions leads to code that is difficult
|
|
to read and hence debug and maintain.
|
|
</p>
|
|
|
|
<p>
|
|
Note that the operators <code>&</code> and
|
|
<code>|</code> are not only integer bitwise operators, they are also the
|
|
<a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.22.2">
|
|
non-shortcut versions</a> of the boolean operators.
|
|
<code>&&</code> and <code>||</code>.
|
|
</p>
|
|
<p>
|
|
Note that <code>&</code>, <code>|</code> and <code>^</code> are not checked
|
|
if they are part of constructor or method call
|
|
because they can be applied to non boolean variables and
|
|
Checkstyle does not know types of methods from different classes.
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Properties">
|
|
<table>
|
|
<tr>
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>max</td>
|
|
<td>
|
|
the maximum allowed number of boolean operations in one
|
|
expression.
|
|
</td>
|
|
<td><a href="property_types.html#integer">integer</a></td>
|
|
<td><code>3</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>tokens</td>
|
|
<td>tokens to check</td>
|
|
<td>
|
|
subset of tokens
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAND">LAND</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BAND">BAND</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LOR">LOR</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BOR">BOR</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BXOR">BXOR</a>.
|
|
</td>
|
|
<td>
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAND">LAND</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BAND">BAND</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LOR">LOR</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BOR">BOR</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BXOR">BXOR</a>.
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</subsection>
|
|
|
|
<subsection name="Examples">
|
|
<p>
|
|
To configure the check:
|
|
</p>
|
|
<source>
|
|
<module name="BooleanExpressionComplexity"/>
|
|
</source>
|
|
|
|
<p>
|
|
To configure the check with 7 allowed operation in boolean
|
|
expression:
|
|
</p>
|
|
<source>
|
|
<module name="BooleanExpressionComplexity">
|
|
<property name="max" value="7"/>
|
|
</module>
|
|
</source>
|
|
|
|
<p>
|
|
To configure the check to ignore <code>&</code> and
|
|
<code>|</code>:
|
|
</p>
|
|
<source>
|
|
<module name="BooleanExpressionComplexity">
|
|
<property name="tokens" value="BXOR,LAND,LOR"/>
|
|
</module>
|
|
</source>
|
|
|
|
</subsection>
|
|
|
|
<subsection name="Example of Usage">
|
|
<ul>
|
|
<li>
|
|
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+BooleanExpressionComplexity">
|
|
Checkstyle Style</a>
|
|
</li>
|
|
</ul>
|
|
</subsection>
|
|
|
|
<subsection name="Package">
|
|
<p>
|
|
com.puppycrawl.tools.checkstyle.checks.metrics
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Parent Module">
|
|
<p>
|
|
<a href="config.html#TreeWalker">TreeWalker</a>
|
|
</p>
|
|
</subsection>
|
|
</section>
|
|
|
|
<section name="ClassDataAbstractionCoupling">
|
|
<subsection name="Description">
|
|
<p>
|
|
This metric measures the number of instantiations of other
|
|
classes within the given class. This type of coupling is not
|
|
caused by inheritance or the object oriented
|
|
paradigm. Generally speaking, any data type with other data
|
|
types as members or local variable that is an instantiation
|
|
(object) of another class has data abstraction coupling (DAC).
|
|
The higher the DAC, the more complex the structure of the class.
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Properties">
|
|
<table>
|
|
<tr>
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>max</td>
|
|
<td>the maximum threshold allowed</td>
|
|
<td><a href="property_types.html#integer">integer</a></td>
|
|
<td><code>7</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>excludedClasses</td>
|
|
<td>User-configured class names to ignore</td>
|
|
<td><a href="property_types.html#stringSet">String Set</a></td>
|
|
<td>boolean, byte, char, double, float, int,
|
|
long, short, void, Boolean, Byte, Character, Double, Float,
|
|
Integer, Long, Short, Void,
|
|
Object, Class, String, StringBuffer, StringBuilder,
|
|
ArrayIndexOutOfBoundsException, Exception,
|
|
RuntimeException, IllegalArgumentException,
|
|
IllegalStateException, IndexOutOfBoundsException,
|
|
NullPointerException, Throwable, SecurityException,
|
|
UnsupportedOperationException,
|
|
List, ArrayList, Deque, Queue, LinkedList,
|
|
Set, HashSet, SortedSet, TreeSet,
|
|
Map, HashMap, SortedMap, TreeMap</td>
|
|
</tr>
|
|
</table>
|
|
</subsection>
|
|
|
|
<subsection name="Examples">
|
|
<p>
|
|
To configure the check:
|
|
</p>
|
|
<source>
|
|
<module name="ClassDataAbstractionCoupling"/>
|
|
</source>
|
|
|
|
<p>
|
|
To configure the check with a threshold of 5:
|
|
</p>
|
|
<source>
|
|
<module name="ClassDataAbstractionCoupling">
|
|
<property name="max" value="5"/>
|
|
</module>
|
|
</source>
|
|
</subsection>
|
|
|
|
<subsection name="Example of Usage">
|
|
<ul>
|
|
<li>
|
|
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+ClassDataAbstractionCoupling">
|
|
Checkstyle Style</a>
|
|
</li>
|
|
</ul>
|
|
</subsection>
|
|
|
|
<subsection name="Package">
|
|
<p>
|
|
com.puppycrawl.tools.checkstyle.checks.metrics
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Parent Module">
|
|
<p>
|
|
<a href="config.html#TreeWalker">TreeWalker</a>
|
|
</p>
|
|
</subsection>
|
|
</section>
|
|
|
|
<section name="ClassFanOutComplexity">
|
|
<subsection name="Description">
|
|
<p>
|
|
The number of other classes a given class relies on. Also the
|
|
square of this has been shown to indicate the amount of
|
|
maintenance required in functional programs (on a file basis)
|
|
at least.
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Properties">
|
|
<table>
|
|
<tr>
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>max</td>
|
|
<td>the maximum threshold allowed</td>
|
|
<td><a href="property_types.html#integer">integer</a></td>
|
|
<td><code>20</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>excludedClasses</td>
|
|
<td>User-configured class names to ignore</td>
|
|
<td><a href="property_types.html#stringSet">String Set</a></td>
|
|
<td>boolean, byte, char, double, float, int,
|
|
long, short, void, Boolean, Byte, Character, Double, Float,
|
|
Integer, Long, Short, Void,
|
|
Object, Class, String, StringBuffer, StringBuilder,
|
|
ArrayIndexOutOfBoundsException, Exception,
|
|
RuntimeException, IllegalArgumentException,
|
|
IllegalStateException, IndexOutOfBoundsException,
|
|
NullPointerException, Throwable, SecurityException,
|
|
UnsupportedOperationException,
|
|
List, ArrayList, Deque, Queue, LinkedList,
|
|
Set, HashSet, SortedSet, TreeSet,
|
|
Map, HashMap, SortedMap, TreeMap</td>
|
|
</tr>
|
|
</table>
|
|
</subsection>
|
|
|
|
<subsection name="Examples">
|
|
<p>
|
|
To configure the check:
|
|
</p>
|
|
<source>
|
|
<module name="ClassFanOutComplexity"/>
|
|
</source>
|
|
|
|
<p>
|
|
To configure the check with a threshold of 10:
|
|
</p>
|
|
<source>
|
|
<module name="ClassFanOutComplexity">
|
|
<property name="max" value="10"/>
|
|
</module>
|
|
</source>
|
|
</subsection>
|
|
|
|
<subsection name="Example of Usage">
|
|
<ul>
|
|
<li>
|
|
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+ClassFanOutComplexity">
|
|
Checkstyle Style</a>
|
|
</li>
|
|
</ul>
|
|
</subsection>
|
|
|
|
<subsection name="Package">
|
|
<p>
|
|
com.puppycrawl.tools.checkstyle.checks.metrics
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Parent Module">
|
|
<p>
|
|
<a href="config.html#TreeWalker">TreeWalker</a>
|
|
</p>
|
|
</subsection>
|
|
</section>
|
|
|
|
<section name="CyclomaticComplexity">
|
|
<subsection name="Description">
|
|
<p>
|
|
Checks cyclomatic complexity against a specified limit.
|
|
It is a measure of the minimum number of
|
|
possible paths through the source and therefore the number of
|
|
required tests, it is not a about quality of code!
|
|
It is only applied to methods, c-tors,
|
|
<a href="https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html">static initializers and instance initializers</a>.
|
|
<br/>
|
|
The complexity is equal to the number of decision points <code> + 1</code>
|
|
Decision points: <code>if</code>, <code>while</code>
|
|
, <code>do</code>, <code>for</code>, <code>?:</code>, <code>catch</code>
|
|
, <code>switch</code>, <code>case</code>
|
|
statements, and operators <code>&&</code> and <code>||</code>
|
|
in the body of target.
|
|
|
|
<br/>By pure theory level 1-4 is considered easy to test, 5-7 OK, 8-10
|
|
consider re-factoring to ease testing, and 11+ re-factor now as testing will be painful.
|
|
|
|
<br/>When it comes to code quality measurement by this metric
|
|
level 10 is very good level as a ultimate target (that is hard to archive).
|
|
Do not be ashamed to have complexity level 15 or even higher,
|
|
but keep it below 20 to catch really bad designed code automatically.
|
|
<br/>Please use Suppression to avoid violations on cases that could not be split in few
|
|
methods without damaging readability of code or encapsulation.
|
|
<br/>
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Properties">
|
|
<table>
|
|
<tr>
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>max</td>
|
|
<td>the maximum threshold allowed</td>
|
|
<td><a href="property_types.html#integer">integer</a></td>
|
|
<td><code>10</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>switchBlockAsSingleDecisionPoint</td>
|
|
<td>whether to treat the whole switch block as a single decision point</td>
|
|
<td><a href="property_types.html#integer">boolean</a></td>
|
|
<td><code>false</code></td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>tokens</td>
|
|
<td>tokens to check</td>
|
|
|
|
<td>
|
|
subset of tokens
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_WHILE">LITERAL_WHILE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO">LITERAL_DO</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FOR">LITERAL_FOR</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_IF">LITERAL_IF</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SWITCH">LITERAL_SWITCH</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CASE">LITERAL_CASE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CATCH">LITERAL_CATCH</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#QUESTION">QUESTION</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAND">LAND</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LOR">LOR</a>.
|
|
</td>
|
|
|
|
<td>
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_WHILE">LITERAL_WHILE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO">LITERAL_DO</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FOR">LITERAL_FOR</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_IF">LITERAL_IF</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SWITCH">LITERAL_SWITCH</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CASE">LITERAL_CASE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CATCH">LITERAL_CATCH</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#QUESTION">QUESTION</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAND">LAND</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LOR">LOR</a>.
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</subsection>
|
|
|
|
<subsection name="Examples">
|
|
<p>
|
|
To configure the check:
|
|
</p>
|
|
<source>
|
|
<module name="CyclomaticComplexity"/>
|
|
</source>
|
|
|
|
<p>
|
|
To configure the check with a threshold of 15:
|
|
</p>
|
|
<source>
|
|
<module name="CyclomaticComplexity">
|
|
<property name="max" value="15"/>
|
|
</module>
|
|
</source>
|
|
<p>
|
|
Explanation on how complexity is calculated (switchBlockAsSingleDecisionPoint is set to false):
|
|
</p>
|
|
<source>
|
|
class CC {
|
|
// Cyclomatic Complexity = 12
|
|
public void doSmth() { // 1
|
|
if (a == b) { // 2
|
|
if (a1 == b1 // 3
|
|
&& c1 == d1) { // 4
|
|
fiddle();
|
|
}
|
|
else if (a2 == b2 // 5
|
|
|| c1 < d1) { // 6
|
|
fiddle();
|
|
}
|
|
else {
|
|
fiddle();
|
|
}
|
|
}
|
|
else if (c == d) { // 7
|
|
while (c == d) { // 8
|
|
fiddle();
|
|
}
|
|
}
|
|
else if (e == f) {
|
|
for (n = 0; n < h // 9
|
|
|| n < 6; n++) { // 10
|
|
fiddle();
|
|
}
|
|
}
|
|
else {
|
|
switch (z) {
|
|
case 1: // 11
|
|
fiddle();
|
|
break;
|
|
case 2: // 12
|
|
fiddle();
|
|
break;
|
|
default:
|
|
fiddle();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} </source>
|
|
<p>
|
|
Explanation on how complexity is calculated (switchBlockAsSingleDecisionPoint is set to true):
|
|
</p>
|
|
<source>
|
|
class SwitchExample {
|
|
// Cyclomatic Complexity = 2
|
|
public void doSmth() { // 1
|
|
int z = 1;
|
|
switch (z) { // 2
|
|
case 1:
|
|
foo1();
|
|
break;
|
|
case 2:
|
|
foo2();
|
|
break;
|
|
default:
|
|
fooDefault();
|
|
break;
|
|
}
|
|
}
|
|
} </source>
|
|
</subsection>
|
|
|
|
<subsection name="Example of Usage">
|
|
<ul>
|
|
<li>
|
|
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+CyclomaticComplexity">
|
|
Checkstyle Style</a>
|
|
</li>
|
|
</ul>
|
|
</subsection>
|
|
|
|
<subsection name="Package">
|
|
<p>
|
|
com.puppycrawl.tools.checkstyle.checks.metrics
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Parent Module">
|
|
<p>
|
|
<a href="config.html#TreeWalker">TreeWalker</a>
|
|
</p>
|
|
</subsection>
|
|
</section>
|
|
|
|
<section name="JavaNCSS">
|
|
<subsection name="Description">
|
|
<p>
|
|
Determines complexity of methods, classes and files by
|
|
counting the Non Commenting Source Statements (NCSS). This
|
|
check adheres to the <a
|
|
href="http://www.kclee.de/clemens/java/javancss/#specification">
|
|
specification</a> for the
|
|
<a href="http://www.kclee.de/clemens/java/javancss/">JavaNCSS-Tool</a>
|
|
written by <b>Chr. Clemens Lee</b>.<br/>
|
|
Roughly said the NCSS metric is calculated by
|
|
counting the source lines which are not comments, (nearly)
|
|
equivalent to counting the semicolons and opening curly
|
|
braces.<br/> The NCSS for a class is summarized from the NCSS
|
|
of all its methods, the NCSS of its nested classes and the
|
|
number of member variable declarations.<br/> The NCSS for a
|
|
file is summarized from the ncss of all its top level classes,
|
|
the number of imports and the package declaration.
|
|
</p>
|
|
|
|
<p>
|
|
Rationale: Too large methods and classes are hard to read and
|
|
costly to maintain. A large NCSS number often means that a
|
|
method or class has too many responsibilities and/or
|
|
functionalities which should be decomposed into smaller units.
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Properties">
|
|
<table>
|
|
<tr>
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>methodMaximum</td>
|
|
<td>
|
|
the maximum allowed number of non commenting lines in a
|
|
method.
|
|
</td>
|
|
<td><a href="property_types.html#integer">integer</a></td>
|
|
<td><code>50</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>classMaximum</td>
|
|
<td>
|
|
the maximum allowed number of non commenting lines in a
|
|
class.
|
|
</td>
|
|
<td><a href="property_types.html#integer">integer</a></td>
|
|
<td><code>1500</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td>fileMaximum</td>
|
|
<td>
|
|
the maximum allowed number of non commenting lines in a
|
|
file including all top level and nested classes.
|
|
</td>
|
|
<td><a href="property_types.html#integer">integer</a></td>
|
|
<td><code>2000</code></td>
|
|
</tr>
|
|
</table>
|
|
</subsection>
|
|
|
|
<subsection name="Examples">
|
|
<p>
|
|
To configure the check:
|
|
</p>
|
|
<source>
|
|
<module name="JavaNCSS"/>
|
|
</source>
|
|
|
|
<p>
|
|
To configure the check with 40 allowed non commenting lines
|
|
for a method:
|
|
</p>
|
|
<source>
|
|
<module name="JavaNCSS">
|
|
<property name="methodMaximum" value="40"/>
|
|
</module>
|
|
</source>
|
|
</subsection>
|
|
|
|
<subsection name="Example of Usage">
|
|
<ul>
|
|
<li>
|
|
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+JavaNCSS">
|
|
Checkstyle Style</a>
|
|
</li>
|
|
</ul>
|
|
</subsection>
|
|
|
|
<subsection name="Package">
|
|
<p>
|
|
com.puppycrawl.tools.checkstyle.checks.metrics
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Parent Module">
|
|
<p>
|
|
<a href="config.html#TreeWalker">TreeWalker</a>
|
|
</p>
|
|
</subsection>
|
|
</section>
|
|
|
|
<section name="NPathComplexity">
|
|
<subsection name="Description">
|
|
<p>
|
|
The NPATH metric computes the number of possible execution
|
|
paths through a function. It takes into account the nesting of
|
|
conditional statements and multi-part boolean expressions
|
|
(e.g., A && B, C || D, etc.).
|
|
<br/>
|
|
The NPATH metric was designed base on Cyclomatic complexity to
|
|
avoid problem of Cyclomatic complexity metric like nesting level within a function.
|
|
</p>
|
|
<p>
|
|
Metric was described at <a href="http://dl.acm.org/citation.cfm?id=42379">"NPATH: a measure of execution pathcomplexity and its applications"</a>. If you need detaled description of algorithm, please read that article,
|
|
it is well written and have number of examples and details.
|
|
</p>
|
|
|
|
<p>Here is some quotes:</p>
|
|
|
|
<blockquote>An NPATH threshold value of 200 has been established
|
|
for a function. The value 200 is based on studies done
|
|
at AT&T Bell Laboratories [1988 year].
|
|
</blockquote>
|
|
|
|
<blockquote>
|
|
Some of the most effective methods of reducing the NPATH value include<br/>
|
|
- distributing functionality,<br/>
|
|
- implementing multiple if statements as a switch statement<br/>
|
|
- creating a separate function for logical expressions with a high count of and (&&) and or (||) operators.
|
|
</blockquote>
|
|
|
|
<blockquote>
|
|
Although strategies to reduce the NPATH complexity
|
|
of functions are important, care must be taken not to
|
|
distort the logical clarity of the software by applying a
|
|
strategy to reduce the complexity of functions. That is,
|
|
there is a point of diminishing return beyond which a
|
|
further attempt at reduction of complexity distorts the
|
|
logical clarity of the system structure.
|
|
</blockquote>
|
|
|
|
<table>
|
|
<thead><tr><th>Structure</th><th> Complexity expression </th></tr></thead>
|
|
<tr><td>if</td><td>NP(if-range) + NP(expr) + 1</td></tr>
|
|
<tr><td>if-else</td><td>NP(if-range) + NP(else-range) + NP(expr)</td></tr>
|
|
<tr><td>while</td><td>NP(while-range) + NP(expr) + 1</td></tr>
|
|
<tr><td>do while</td><td>NP(do-range) + NP(expr) + 1</td></tr>
|
|
<tr><td>for</td><td>NP(for-range) + NP(exprl) + NP(expr2) + NP(expr3) + 1</td></tr>
|
|
<tr><td>switch</td><td>NP(expr) + S(i=1:i=n)NP(case-range(i)) + NP(default-range)</td></tr>
|
|
<tr><td>?</td><td>NP(expr1) + NP(expr2) + NP(expr3) + 2</td></tr>
|
|
<tr><td>goto label</td><td>1</td></tr>
|
|
<tr><td>break</td><td>1</td></tr>
|
|
<tr><td>Expressions</td><td>Number of && and || operators in expression</td></tr>
|
|
<tr><td>continue</td><td>1</td></tr>
|
|
<tr><td>return</td><td>1</td></tr>
|
|
<tr><td>sequential</td><td>1</td></tr>
|
|
<tr><td>Function call</td><td>1</td></tr>
|
|
<tr><td>C function</td><td>P(i=1:i=N)NP(Statement(i))</td></tr>
|
|
</table>
|
|
|
|
<p>
|
|
<b>Rationale:</b> Nejmeh says that his group had an informal NPATH
|
|
limit of 200 on individual routines; functions that exceeded
|
|
this value were candidates for further decomposition - or at
|
|
least a closer look.
|
|
<b>Please do not be fanatic with limit 200</b>
|
|
- choose number that suites your project style. Limit 200 is
|
|
empirical number base on some sources of at AT&T Bell Laboratories
|
|
of 1988 year.
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Properties">
|
|
<table>
|
|
<tr>
|
|
<th>name</th>
|
|
<th>description</th>
|
|
<th>type</th>
|
|
<th>default value</th>
|
|
</tr>
|
|
<tr>
|
|
<td>max</td>
|
|
<td>the maximum threshold allowed</td>
|
|
<td><a href="property_types.html#integer">integer</a></td>
|
|
<td><code>200</code></td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>tokens</td>
|
|
<td>tokens to check</td>
|
|
|
|
<td>
|
|
subset of tokens
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_WHILE">LITERAL_WHILE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO">LITERAL_DO</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FOR">LITERAL_FOR</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_IF">LITERAL_IF</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_ELSE">LITERAL_ELSE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SWITCH">LITERAL_SWITCH</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CASE">LITERAL_CASE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_TRY">LITERAL_TRY</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CATCH">LITERAL_CATCH</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#QUESTION">QUESTION</a>.
|
|
</td>
|
|
|
|
<td>
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_WHILE">LITERAL_WHILE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO">LITERAL_DO</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FOR">LITERAL_FOR</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_IF">LITERAL_IF</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_ELSE">LITERAL_ELSE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SWITCH">LITERAL_SWITCH</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CASE">LITERAL_CASE</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_TRY">LITERAL_TRY</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_CATCH">LITERAL_CATCH</a>,
|
|
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#QUESTION">QUESTION</a>.
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</subsection>
|
|
|
|
<subsection name="Examples">
|
|
<p>
|
|
To configure the check:
|
|
</p>
|
|
<source>
|
|
<module name="NPathComplexity"/>
|
|
</source>
|
|
|
|
<p>
|
|
To configure the check with a threshold of 1000:
|
|
</p>
|
|
<source>
|
|
<module name="NPathComplexity">
|
|
<property name="max" value="1000"/>
|
|
</module>
|
|
</source>
|
|
</subsection>
|
|
|
|
<subsection name="Example of Usage">
|
|
<ul>
|
|
<li>
|
|
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+NPathComplexity">
|
|
Checkstyle Style</a>
|
|
</li>
|
|
</ul>
|
|
</subsection>
|
|
|
|
<subsection name="Package">
|
|
<p>
|
|
com.puppycrawl.tools.checkstyle.checks.metrics
|
|
</p>
|
|
</subsection>
|
|
|
|
<subsection name="Parent Module">
|
|
<p>
|
|
<a href="config.html#TreeWalker">TreeWalker</a>
|
|
</p>
|
|
</subsection>
|
|
</section>
|
|
</body>
|
|
</document>
|