Cyclomatic complexity measures the number of linearly independent paths through a program’s source code. This rule allows setting a cyclomatic complexity threshold.
function a(x) { if (true) { return x; // 1st path } else if (false) { return x+1; // 2nd path } else { return 4; // 3rd path } }
This rule is aimed at reducing code complexity by capping the amount of cyclomatic complexity allowed in a program. As such, it will warn when the cyclomatic complexity crosses the configured threshold (default is 20
).
Examples of incorrect code for a maximum of 2:
/*eslint complexity: ["error", 2]*/ function a(x) { if (true) { return x; } else if (false) { return x+1; } else { return 4; // 3rd path } }
Examples of correct code for a maximum of 2:
/*eslint complexity: ["error", 2]*/ function a(x) { if (true) { return x; } else { return 4; } }
Optionally, you may specify a max
object property:
"complexity": ["error", 2]
is equivalent to
"complexity": ["error", { "max": 2 }]
Deprecated: the object property maximum
is deprecated. Please use the property max
instead.
If you can’t determine an appropriate complexity limit for your code, then it’s best to disable this rule.
This rule was introduced in ESLint 0.0.9.
© JS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/rules/complexity