com.puppycrawl.tools.checkstyle.meta.checks.metrics.NPathComplexityCheck.xml Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of checkstyle Show documentation
Show all versions of checkstyle Show documentation
Checkstyle is a development tool to help programmers write Java code
that adheres to a coding standard
<?xml version="1.0" encoding="UTF-8"?> <checkstyle-metadata> <module> <check fully-qualified-name="com.puppycrawl.tools.checkstyle.checks.metrics.NPathComplexityCheck" name="NPathComplexity" parent="com.puppycrawl.tools.checkstyle.TreeWalker"> <description><p> Checks the NPATH complexity against a specified limit. </p> <p> The NPATH metric computes the number of possible execution paths through a function(method). It takes into account the nesting of conditional statements and multi-part boolean expressions (A &amp;&amp; B, C || D, E ? F :G and their combinations). </p> <p> The NPATH metric was designed base on Cyclomatic complexity to avoid problem of Cyclomatic complexity metric like nesting level within a function(method). </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 detailed 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&amp;T Bell Laboratories [1988 year]. </blockquote> <blockquote> Some of the most effective methods of reducing the NPATH value include: <ul> <li> distributing functionality; </li> <li> implementing multiple if statements as a switch statement; </li> <li> creating a separate function for logical expressions with a high count of variables and (&amp;&amp;) and or (||) operators. </li> </ul> </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> <caption>Examples</caption> <thead><tr><th>Structure</th><th>Complexity expression</th></tr></thead> <tr><td>if ([expr]) { [if-range] }</td><td>NP(if-range) + 1 + NP(expr)</td></tr> <tr><td>if ([expr]) { [if-range] } else { [else-range] }</td> <td>NP(if-range)+ NP(else-range) + NP(expr)</td></tr> <tr><td>while ([expr]) { [while-range] }</td><td>NP(while-range) + NP(expr) + 1</td></tr> <tr><td>do { [do-range] } while ([expr])</td><td>NP(do-range) + NP(expr) + 1</td></tr> <tr><td>for([expr1]; [expr2]; [expr3]) { [for-range] }</td> <td>NP(for-range) + NP(expr1)+ NP(expr2) + NP(expr3) + 1</td></tr> <tr><td>switch ([expr]) { case : [case-range] default: [default-range] }</td> <td>S(i=1:i=n)NP(case-range[i]) + NP(default-range) + NP(expr)</td></tr> <tr><td>[expr1] ? [expr2] : [expr3]</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 &amp;&amp; and || operators in expression. No operators - 0</td></tr> <tr><td>continue</td><td>1</td></tr><tr><td>return</td><td>1</td></tr> <tr><td>Statement (even sequential statements)</td><td>1</td></tr> <tr><td>Empty block {}</td><td>1</td></tr><tr><td>Function call</td><td>1</td> </tr><tr><td>Function(Method) declaration or Block</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(methods) 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&amp;T Bell Laboratories of 1988 year. </p></description> <properties> <property default-value="200" name="max" type="int"> <description>Specify the maximum threshold allowed.</description> </property> </properties> <message-keys> <message-key key="npathComplexity"/> </message-keys> </check> </module> </checkstyle-metadata>