redline
MediumDesignlong-function

Long function

Long function. big is 72 lines long. Past 60, a function usually does more than one job.

This is how the comment reads inline, under the flagged line.

Why it matters

Long functions mix several jobs. They're hard to name, test and change without breaking a part you didn't read.

What to do

Split along the jobs it does. Each piece gets a name that says what it returns.

Examples

Flagged
function checkout(cart) {
  // 140 lines: validate, price, tax, charge, email
}
Better
function checkout(cart) {
  const order = price(validate(cart));
  return charge(order).then(sendReceipt);
}

What it catches

It reads the shape of the code: functions, blocks, loops and calls. Languages: TypeScript and JavaScript, Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

function big() {
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
  step();
}
src/app.py
def big():
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()
    step()

Left alone

function small() {
  step();
}

Research

Long functions, and long files that keep growing. Past a certain size a reader cannot hold the unit in mind.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "long-function"
    ],
    "severity": {
      "long-function": "low"
    }
  }
}

Read the source or report a false positive.