New `max-lines-per-function` rule
See original GitHub issuePlease describe what the rule should do: It should allow a configurable limit of the max number of lines per function, much like how max-statements
limits the number of statements. Also, there should be an "ignoreComments"
boolean option, on by default, that skips comments when counting method length.
What category of rule is this? (place an “X” next to just one item)
[x] Enforces code style [ ] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about:
// eslint max-lines-per-function: ["error", 1]
// Good
function foo() { return 1 }
const bar = () => 2
// Bad
function foo() {
return 1
}
const bar = () =>
2
// eslint max-lines-per-function: ["error", 5, {ignoreComments: false}]
// Good
function foo() {
// This is a multi-line
// comment.
doSomething();
}
// Bad
function foo() {
// This is a multi-line
// comment.
doSomething();
doSomethingElse();
}
function foo() {
// This is a multi-line
// comment that takes
// three lines.
doSomething();
}
// eslint max-lines-per-function: ["error", 3, {ignoreComments: true}]
// Good
function foo() {
// This is a multi-line
// comment.
doSomething();
}
// Bad
function foo() {
// This is a multi-line
// comment.
doSomething();
doSomethingElse();
}
function foo() {
doSomething();
doSomethingElse();
}
Why should this rule be included in ESLint (instead of a plugin)?
It is a simple, basic rule similar to what already exists, and is a natural extension of a few of them. It’s also very widely generic, and although it could replace max-statements
in some uses, it checks a different metric. It also does not conflict with any other rules, and it’s pretty much independent from everything short of the language itself.
Other linters and style guides in various languages already do similar:
- Rubocop has
Metrics/MethodLength
which does exactly what I propose here, including the proposed option. - Google’s JS style guide (by proxy of their C++ style guide), recommends 40 lines or fewer per function.
- The Linux Kernel’s style guide recommends 48 lines or fewer.
This is the original bug I filed
It should basically allow a configurable limit of the max number of lines per function, much like how max-statements
limits the number of statements. This has precedent: Google’s JS style guide (by proxy of their C++ style guide), recommends 40 lines or fewer per function. Also, to draw another language’s style guide, the Linux Kernel recommends 48 lines or fewer.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:32 (28 by maintainers)
Top GitHub Comments
The rule is “per function”, and i suspect the intent - and only thing that makes sense to me - is that every function is considered entirely on its own. How nested a function is doesn’t make it more or less OK to have a certain number of lines, and shouldn’t change the allowed amount.
Alright, thanks, I’m sold. TBH I’m not going to push hard for this with the team right now since we’re doing major release candidates, but I’ll champion this after we finish that off. (That’s a promise but real life does get in the way- feel free to ping me if you think I might need a reminder. Thanks!)