]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/complexity.md
3a665491c39fc56fabf8ccc0a97518c86bff9d20
[pve-eslint.git] / eslint / docs / rules / complexity.md
1 # Limit Cyclomatic Complexity (complexity)
2
3 Cyclomatic complexity measures the number of linearly independent paths through a program's source code. This rule allows setting a cyclomatic complexity threshold.
4
5 ```js
6 function a(x) {
7 if (true) {
8 return x; // 1st path
9 } else if (false) {
10 return x+1; // 2nd path
11 } else {
12 return 4; // 3rd path
13 }
14 }
15 ```
16
17 ## Rule Details
18
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`).
20
21 Examples of **incorrect** code for a maximum of 2:
22
23 ```js
24 /*eslint complexity: ["error", 2]*/
25
26 function a(x) {
27 if (true) {
28 return x;
29 } else if (false) {
30 return x+1;
31 } else {
32 return 4; // 3rd path
33 }
34 }
35 ```
36
37 Examples of **correct** code for a maximum of 2:
38
39 ```js
40 /*eslint complexity: ["error", 2]*/
41
42 function a(x) {
43 if (true) {
44 return x;
45 } else {
46 return 4;
47 }
48 }
49 ```
50
51 ## Options
52
53 Optionally, you may specify a `max` object property:
54
55 ```json
56 "complexity": ["error", 2]
57 ```
58
59 is equivalent to
60
61 ```json
62 "complexity": ["error", { "max": 2 }]
63 ```
64
65 **Deprecated:** the object property `maximum` is deprecated. Please use the property `max` instead.
66
67 ## When Not To Use It
68
69 If you can't determine an appropriate complexity limit for your code, then it's best to disable this rule.
70
71 ## Further Reading
72
73 * [Cyclomatic Complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity)
74 * [Complexity Analysis of JavaScript Code](https://ariya.io/2012/12/complexity-analysis-of-javascript-code)
75 * [More about Complexity in JavaScript](https://craftsmanshipforsoftware.com/2015/05/25/complexity-for-javascript/)
76 * [About Complexity](https://web.archive.org/web/20160808115119/http://jscomplexity.org/complexity)
77 * [Discussion about Complexity in ESLint and more links](https://github.com/eslint/eslint/issues/4808#issuecomment-167795140)
78
79 ## Related Rules
80
81 * [max-depth](max-depth.md)
82 * [max-len](max-len.md)
83 * [max-lines](max-lines.md)
84 * [max-lines-per-function](max-lines-per-function.md)
85 * [max-nested-callbacks](max-nested-callbacks.md)
86 * [max-params](max-params.md)
87 * [max-statements](max-statements.md)