question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New `max-lines-per-function` rule

See original GitHub issue

Please 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:

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:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:32 (28 by maintainers)

github_iconTop GitHub Comments

4reactions
ljharbcommented, Jun 16, 2018

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.

4reactions
platinumazurecommented, Apr 6, 2018

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!)

Read more comments on GitHub >

github_iconTop Results From Across the Web

max-lines-per-function - ESLint - Pluggable JavaScript Linter
This rule enforces a maximum number of lines per function, in order to aid in maintainability and reduce complexity. Why not use max-statements...
Read more >
How to Use and Disable ESLint Max-Lines-Per-Function
In this post we will examine the max-lines-per-function options, when to use the rule, and how to disable the rule. Table of Contents...
Read more >
disable max-lines-per-function on certain function **names
My React components' render function almost always exceed my 25 lines and I was wondering, besides adding a disable comment, if there was...
Read more >
强制函数最大行数(max-lines-per-function) - ESLint
Rule Details. This rule enforces a maximum number of lines per function, in order to aid in maintainability and reduce complexity. 此规则为每个函数 ...
Read more >
max-line-length - Rule
Config · limit - number greater than 0 defining the max line length · ignore-pattern - string defining ignore pattern for this rule,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found