]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # enforce a maximum function length (max-lines-per-function) |
2 | ||
3 | Some people consider large functions a code smell. Large functions tend to do a lot of things and can make it hard following what's going on. Many coding style guides dictate a limit of the number of lines that a function can comprise of. This rule can help enforce that style. | |
4 | ||
5 | ## Rule Details | |
6 | ||
7 | This rule enforces a maximum number of lines per function, in order to aid in maintainability and reduce complexity. | |
8 | ||
9 | ## Why not use `max-statements` or other complexity measurement rules instead? | |
10 | ||
11 | Nested long method chains like the below example are often broken onto separate lines for readability: | |
12 | ||
34eeec05 | 13 | ```js |
eb39fafa DC |
14 | function() { |
15 | return m("div", [ | |
16 | m("table", {className: "table table-striped latest-data"}, [ | |
17 | m("tbody", | |
18 | data.map(function(db) { | |
19 | return m("tr", {key: db.dbname}, [ | |
20 | m("td", {className: "dbname"}, db.dbname), | |
21 | m("td", {className: "query-count"}, [ | |
22 | m("span", {className: db.lastSample.countClassName}, db.lastSample.nbQueries) | |
23 | ]) | |
24 | ]) | |
25 | }) | |
26 | ) | |
27 | ]) | |
28 | ]) | |
29 | } | |
30 | ``` | |
31 | ||
32 | * `max-statements` will only report this as 1 statement, despite being 16 lines of code. | |
33 | * `complexity` will only report a complexity of 1 | |
34 | * `max-nested-callbacks` will only report 1 | |
35 | * `max-depth` will report a depth of 0 | |
36 | ||
37 | ## Options | |
38 | ||
39 | This rule has the following options that can be specified using an object: | |
40 | ||
41 | * `"max"` (default `50`) enforces a maximum number of lines in a function. | |
42 | ||
43 | * `"skipBlankLines"` (default `false`) ignore lines made up purely of whitespace. | |
44 | ||
45 | * `"skipComments"` (default `false`) ignore lines containing just comments. | |
46 | ||
47 | * `"IIFEs"` (default `false`) include any code included in IIFEs. | |
48 | ||
49 | Alternatively, you may specify a single integer for the `max` option: | |
50 | ||
51 | ```json | |
52 | "max-lines-per-function": ["error", 20] | |
53 | ``` | |
54 | ||
55 | is equivalent to | |
56 | ||
57 | ```json | |
58 | "max-lines-per-function": ["error", { "max": 20 }] | |
59 | ``` | |
60 | ||
61 | ### code | |
62 | ||
63 | Examples of **incorrect** code for this rule with a max value of `2`: | |
64 | ||
65 | ```js | |
66 | /*eslint max-lines-per-function: ["error", 2]*/ | |
67 | function foo() { | |
68 | var x = 0; | |
69 | } | |
70 | ``` | |
71 | ||
72 | ```js | |
73 | /*eslint max-lines-per-function: ["error", 2]*/ | |
74 | function foo() { | |
75 | // a comment | |
76 | var x = 0; | |
77 | } | |
78 | ``` | |
79 | ||
80 | ```js | |
81 | /*eslint max-lines-per-function: ["error", 2]*/ | |
82 | function foo() { | |
83 | // a comment followed by a blank line | |
84 | ||
85 | var x = 0; | |
86 | } | |
87 | ``` | |
88 | ||
89 | Examples of **correct** code for this rule with a max value of `3`: | |
90 | ||
91 | ```js | |
92 | /*eslint max-lines-per-function: ["error", 3]*/ | |
93 | function foo() { | |
94 | var x = 0; | |
95 | } | |
96 | ``` | |
97 | ||
98 | ```js | |
99 | /*eslint max-lines-per-function: ["error", 3]*/ | |
100 | function foo() { | |
101 | // a comment | |
102 | var x = 0; | |
103 | } | |
104 | ``` | |
105 | ||
106 | ```js | |
107 | /*eslint max-lines-per-function: ["error", 3]*/ | |
108 | function foo() { | |
109 | // a comment followed by a blank line | |
110 | ||
111 | var x = 0; | |
112 | } | |
113 | ``` | |
114 | ||
115 | ### skipBlankLines | |
116 | ||
117 | Examples of **incorrect** code for this rule with the `{ "skipBlankLines": true }` option: | |
118 | ||
119 | ```js | |
120 | /*eslint max-lines-per-function: ["error", {"max": 2, "skipBlankLines": true}]*/ | |
121 | function foo() { | |
122 | ||
123 | var x = 0; | |
124 | } | |
125 | ``` | |
126 | ||
127 | Examples of **correct** code for this rule with the `{ "skipBlankLines": true }` option: | |
128 | ||
129 | ```js | |
130 | /*eslint max-lines-per-function: ["error", {"max": 3, "skipBlankLines": true}]*/ | |
131 | function foo() { | |
132 | ||
133 | var x = 0; | |
134 | } | |
135 | ``` | |
136 | ||
137 | ### skipComments | |
138 | ||
139 | Examples of **incorrect** code for this rule with the `{ "skipComments": true }` option: | |
140 | ||
141 | ```js | |
142 | /*eslint max-lines-per-function: ["error", {"max": 2, "skipComments": true}]*/ | |
143 | function foo() { | |
144 | // a comment | |
145 | var x = 0; | |
146 | } | |
147 | ``` | |
148 | ||
149 | Examples of **correct** code for this rule with the `{ "skipComments": true }` option: | |
150 | ||
151 | ```js | |
152 | /*eslint max-lines-per-function: ["error", {"max": 3, "skipComments": true}]*/ | |
153 | function foo() { | |
154 | // a comment | |
155 | var x = 0; | |
156 | } | |
157 | ``` | |
158 | ||
159 | ### IIFEs | |
160 | ||
161 | Examples of **incorrect** code for this rule with the `{ "IIFEs": true }` option: | |
162 | ||
163 | ```js | |
164 | /*eslint max-lines-per-function: ["error", {"max": 2, "IIFEs": true}]*/ | |
165 | (function(){ | |
166 | var x = 0; | |
167 | }()); | |
ebb53d86 TL |
168 | |
169 | (() => { | |
170 | var x = 0; | |
171 | })(); | |
eb39fafa DC |
172 | ``` |
173 | ||
174 | Examples of **correct** code for this rule with the `{ "IIFEs": true }` option: | |
175 | ||
176 | ```js | |
177 | /*eslint max-lines-per-function: ["error", {"max": 3, "IIFEs": true}]*/ | |
178 | (function(){ | |
179 | var x = 0; | |
180 | }()); | |
ebb53d86 TL |
181 | |
182 | (() => { | |
183 | var x = 0; | |
184 | })(); | |
eb39fafa DC |
185 | ``` |
186 | ||
187 | ## When Not To Use It | |
188 | ||
189 | You can turn this rule off if you are not concerned with the number of lines in your functions. | |
190 | ||
191 | ## Related Rules | |
192 | ||
193 | * [complexity](complexity.md) | |
194 | * [max-depth](max-depth.md) | |
195 | * [max-lines](max-lines.md) | |
196 | * [max-nested-callbacks](max-nested-callbacks.md) | |
197 | * [max-params](max-params.md) | |
198 | * [max-statements](max-statements.md) | |
199 | * [max-statements-per-line](max-statements-per-line.md) |