# max-lines-per-function

> Enforce a maximum number of lines in a function

A function that doesn't fit on the screen is hard to read in one go, and it usually does several jobs that could each have a name. This rule reports any function with more lines than the maximum, which is 50 by default.

The count runs from the line where the function starts to the line where it ends.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"max-lines-per-function" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"max-lines-per-function" = ["error", {
  max = 50,
  skipBlankLines = false,
  skipComments = false,
  IIFEs = false,
}]
```

{/* End of the generated section. */}

## Examples

With a maximum of 5, this 7-line function is reported:

{/* bad {"options": [5]} */}
```js
function checkout(cart) {
  let total = 0;
  for (const item of cart.items) {
    total += item.price * item.count;
  }
  charge(cart.user, total);
}
```

Not reported:

{/* good {"options": [5]} */}
```js
function totalOf(cart) {
  let total = 0;
  for (const item of cart.items) total += item.price * item.count;
  return total;
}

function checkout(cart) {
  charge(cart.user, totalOf(cart));
}
```

## Options

```toml bunfig.toml
[lint.rules]
max-lines-per-function = ["error", {
  max = 80,
  skipBlankLines = true,
  skipComments = true,
}]
```

| Option | Default | What it does |
| --- | --- | --- |
| `max` | `50` | The number of lines a function can have. |
| `skipBlankLines` | `false` | Don't count lines that are empty or only have whitespace. |
| `skipComments` | `false` | Don't count lines that only have a comment. |
| `IIFEs` | `false` | Also check a function that's called where it's written, like `(function () { ... })()`. |

A number alone is short for `{ "max": number }`.

This function has 5 lines, so a maximum of 3 reports it:

{/* bad {"options": [3]} */}
```js
function greet(user) {
  // Fall back to the login name.

  return `Hello, ${user.name ?? user.login}`;
}
```

Without the comment and the blank line it has 3:

{/* good {"options": [{"max": 3, "skipBlankLines": true, "skipComments": true}]} */}
```js
function greet(user) {
  // Fall back to the login name.

  return `Hello, ${user.name ?? user.login}`;
}
```

A function that's called where it's written is skipped by default. This passes with a maximum of 2:

{/* good {"options": [2]} */}
```js
(function () {
  setup();
  start();
})();
```

With `IIFEs` it's reported:

{/* bad {"options": [{"max": 2, "IIFEs": true}]} */}
```js
(function () {
  setup();
  start();
})();
```
