public final class NPathComplexityCheck extends AbstractCheck
Checks the NPATH complexity against a specified limit.
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 && B, C || D, E ? F :G and their combinations).
The NPATH metric was designed base on Cyclomatic complexity to avoid problem of Cyclomatic complexity metric like nesting level within a function(method).
Metric was described at "NPATH: a measure of execution pathcomplexity and its applications". If you need detailed description of algorithm, please read that article, it is well written and have number of examples and details.
Here is some quotes:
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].
Some of the most effective methods of reducing the NPATH value include:
- distributing functionality;
- implementing multiple if statements as a switch statement;
- creating a separate function for logical expressions with a high count of variables and (&&) and or (||) operators.
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.
Structure | Complexity expression |
---|---|
if ([expr]) { [if-range] } | NP(if-range) + 1 + NP(expr) |
if ([expr]) { [if-range] } else { [else-range] } | NP(if-range)+ NP(else-range) + NP(expr) |
while ([expr]) { [while-range] } | NP(while-range) + NP(expr) + 1 |
do { [do-range] } while ([expr]) | NP(do-range) + NP(expr) + 1 |
for([expr1]; [expr2]; [expr3]) { [for-range] } | NP(for-range) + NP(expr1)+ NP(expr2) + NP(expr3) + 1 |
switch ([expr]) { case : [case-range] default: [default-range] } | S(i=1:i=n)NP(case-range[i]) + NP(default-range) + NP(expr) |
[expr1] ? [expr2] : [expr3] | NP(expr1) + NP(expr2) + NP(expr3) + 2 |
goto label | 1 |
break | 1 |
Expressions | Number of && and || operators in expression. No operators - 0 |
continue | 1 |
return | 1 |
Statement (even sequential statements) | 1 |
Empty block {} | 1 |
Function call | 1 |
Function(Method) declaration or Block | P(i=1:i=N)NP(Statement[i]) |
Rationale: 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. Please do not be fanatic with limit 200 - 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.
max
- Specify the maximum threshold allowed.
Default value is 200
.
To configure the check:
<module name="NPathComplexity"/>
To configure the check with a threshold of 1000:
<module name="NPathComplexity"> <property name="max" value="1000"/> </module>
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
NPathComplexityCheck() |
Modifier and Type | Method and Description |
---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
void |
setMax(int max)
Setter to specify the maximum threshold allowed.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final java.lang.String MSG_KEY
public NPathComplexityCheck()
public void setMax(int max)
max
- the maximum thresholdpublic int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void beginTree(DetailAST rootAST)
AbstractCheck
beginTree
in class AbstractCheck
rootAST
- the root of the treepublic void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processpublic void leaveToken(DetailAST ast)
AbstractCheck
leaveToken
in class AbstractCheck
ast
- the token leavingCopyright © 2001-2019. All Rights Reserved.