Class CyclomaticComplexityCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.metrics.CyclomaticComplexityCheck
- All Implemented Interfaces:
Configurable,Contextualizable
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 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,casestatements. -
Operators
&&and||in the body of target. -
whenexpression in case labels, also known as guards.
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.
-
Property
max- Specify the maximum threshold allowed. Type isint. Default value is10. -
Property
switchBlockAsSingleDecisionPoint- Control whether to treat the whole switch block as a single decision point. Type isboolean. Default value isfalse. -
Property
tokens- tokens to check Type isjava.lang.String[]. Validation type istokenSet. Default value is: LITERAL_WHILE, LITERAL_DO, LITERAL_FOR, LITERAL_IF, LITERAL_SWITCH, LITERAL_CASE, LITERAL_CATCH, QUESTION, LAND, LOR, LITERAL_WHEN.
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
cyclomaticComplexity
- Since:
- 3.2
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate BigIntegerThe current value.private static final intDefault allowed complexity.private static final BigIntegerThe initial current value.private intSpecify the maximum threshold allowed.static final StringA key is pointing to the warning message text in "messages.properties" file.private booleanControl whether to treat the whole switch block as a single decision point.private final Deque<BigInteger>Stack of values - all but the current value. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint[]The configurable token set.int[]Returns the default token a check is interested in.final int[]The tokens that this check must be registered for.private voidincrementCurrentValue(BigInteger amount) Increments the current value by a specified amount.private voidleaveMethodDef(DetailAST ast) Process the end of a method definition.voidleaveToken(DetailAST ast) Called after all the child nodes have been process.private voidpopValue()Pops a value off the stack and makes it the current value.private voidPush the current value on the stack.final voidsetMax(int max) Setter to specify the maximum threshold allowed.voidsetSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint) Setter to control whether to treat the whole switch block as a single decision point.private voidProcess the start of the method definition.voidvisitToken(DetailAST ast) Called to process a token.private voidvisitTokenHook(DetailAST ast) Hook called when visiting a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
INITIAL_VALUE
The initial current value. -
DEFAULT_COMPLEXITY_VALUE
Default allowed complexity.- See Also:
-
valueStack
Stack of values - all but the current value. -
switchBlockAsSingleDecisionPoint
Control whether to treat the whole switch block as a single decision point. -
currentValue
The current value. -
max
Specify the maximum threshold allowed.
-
-
Constructor Details
-
CyclomaticComplexityCheck
public CyclomaticComplexityCheck()
-
-
Method Details
-
setSwitchBlockAsSingleDecisionPoint
Setter to control whether to treat the whole switch block as a single decision point.- Parameters:
switchBlockAsSingleDecisionPoint- whether to treat the whole switch block as a single decision point.- Since:
- 6.11
-
setMax
Setter to specify the maximum threshold allowed.- Parameters:
max- the maximum threshold- Since:
- 3.2
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
leaveToken
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
visitTokenHook
Hook called when visiting a token. Will not be called the method definition tokens.- Parameters:
ast- the token being visited
-
leaveMethodDef
Process the end of a method definition.- Parameters:
ast- the token representing the method definition
-
incrementCurrentValue
Increments the current value by a specified amount.- Parameters:
amount- the amount to increment by
-
pushValue
Push the current value on the stack. -
popValue
Pops a value off the stack and makes it the current value. -
visitMethodDef
Process the start of the method definition.
-