1 # Limit Cyclomatic Complexity (complexity)
3 Cyclomatic complexity measures the number of linearly independent paths through a program's source code. This rule allows setting a cyclomatic complexity threshold.
10 return x+1; // 2nd path
19 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`).
21 Examples of **incorrect** code for a maximum of 2:
24 /*eslint complexity: ["error", 2]*/
42 Examples of **correct** code for a maximum of 2:
45 /*eslint complexity: ["error", 2]*/
60 Class field initializers and class static blocks are implicit functions. Therefore, their complexity is calculated separately for each initializer and each static block, and it doesn't contribute to the complexity of the enclosing code.
62 Examples of additional **incorrect** code for a maximum of 2:
65 /*eslint complexity: ["error", 2]*/
68 x = a || b || c; // this initializer has complexity = 3
71 class D { // this static block has complexity = 3
80 Examples of additional **correct** code for a maximum of 2:
83 /*eslint complexity: ["error", 2]*/
85 function foo() { // this function has complexity = 1
87 x = a + b; // this initializer has complexity = 1
88 y = c || d; // this initializer has complexity = 2
89 z = e && f; // this initializer has complexity = 2
91 static p = g || h; // this initializer has complexity = 2
92 static q = i ? j : k; // this initializer has complexity = 2
94 static { // this static block has complexity = 2
100 static { // this static block has complexity = 2
109 Optionally, you may specify a `max` object property:
112 "complexity": ["error", 2]
118 "complexity": ["error", { "max": 2 }]
121 **Deprecated:** the object property `maximum` is deprecated. Please use the property `max` instead.
123 ## When Not To Use It
125 If you can't determine an appropriate complexity limit for your code, then it's best to disable this rule.
129 * [Cyclomatic Complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity)
130 * [Complexity Analysis of JavaScript Code](https://ariya.io/2012/12/complexity-analysis-of-javascript-code)
131 * [More about Complexity in JavaScript](https://craftsmanshipforsoftware.com/2015/05/25/complexity-for-javascript/)
132 * [About Complexity](https://web.archive.org/web/20160808115119/http://jscomplexity.org/complexity)
133 * [Discussion about Complexity in ESLint and more links](https://github.com/eslint/eslint/issues/4808#issuecomment-167795140)
137 * [max-depth](max-depth.md)
138 * [max-len](max-len.md)
139 * [max-lines](max-lines.md)
140 * [max-lines-per-function](max-lines-per-function.md)
141 * [max-nested-callbacks](max-nested-callbacks.md)
142 * [max-params](max-params.md)
143 * [max-statements](max-statements.md)