public class CyclomaticComplexityCheck extends AbstractCheck
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, static initializers and instance initializers.
The complexity is equal to the number of decision points + 1.
Decision points: if, while, do, for,
?:, catch, switch, case statements and
operators && and || in the body of target.
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.
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.
Please use Suppression to avoid violations on cases that could not be split in few methods without damaging readability of code or encapsulation.
max - Specify the maximum threshold allowed.
Default value is 10.
switchBlockAsSingleDecisionPoint - Control whether to treat
the whole switch block as a single decision point.
Default value is false.
tokens - tokens to check
Default value is:
LITERAL_WHILE,
LITERAL_DO,
LITERAL_FOR,
LITERAL_IF,
LITERAL_SWITCH,
LITERAL_CASE,
LITERAL_CATCH,
QUESTION,
LAND,
LOR.
To configure the check:
<module name="CyclomaticComplexity"/>
To configure the check with a threshold of 15:
<module name="CyclomaticComplexity"> <property name="max" value="15"/> </module>
Explanation on how complexity is calculated (switchBlockAsSingleDecisionPoint is set to false):
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;
}
}
}
}
Explanation on how complexity is calculated (switchBlockAsSingleDecisionPoint is set to true):
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;
}
}
}
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 |
|---|
CyclomaticComplexityCheck() |
| Modifier and Type | Method and Description |
|---|---|
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 |
setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint)
Setter to control whether to treat the whole switch block as a single decision point.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
public CyclomaticComplexityCheck()
public void setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint)
switchBlockAsSingleDecisionPoint - whether to treat the whole switch
block as a single decision point.public final void setMax(int max)
max - the maximum thresholdpublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic final int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processpublic void leaveToken(DetailAST ast)
AbstractCheckleaveToken in class AbstractCheckast - the token leavingCopyright © 2001-2020. All Rights Reserved.