---
title: "Keep functions short · Agent Wranglers"
url: https://agentwranglers.se/playbook/architecture/names-and-functions/small-functions
updated: 2026-10-20
licence: "All rights reserved"
---

# Keep functions short

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Names and functions](https://agentwranglers.se/playbook/architecture/names-and-functions)
- Rule: Keep each function at 20 lines or fewer. A longer function nearly always does more than one thing.
- How: When a function grows, split it into named steps. The top function then reads like a short list of what happens. A factory that returns an object of methods counts as one function. Keep its body to wiring, and move each method out as a named function. Content templates are exempt. A template only returns HTML, email or Markdown text, with at most small inline conditions. Linters cannot tell templates apart, so keep them in module-level constants or a templates folder that the rule skips.
- Check: ESLint `max-lines-per-function` set to 20, PHPMD `ExcessiveMethodLength` with a minimum of 21, or Pylint `too-many-statements` with `max-statements=20`, which counts statements instead of lines. The build fails above the limit, outside the excluded templates folder.

Example:

```js
// eslint.config.js: the limit applies everywhere except content templates
export default [
  { rules: { 'max-lines-per-function': ['error', { max: 20, skipBlankLines: true, skipComments: true }] } },
  { files: ['src/templates/**'], rules: { 'max-lines-per-function': 'off' } },
];
```

```js
// Bad: a 300-line factory, because every method is written inside it
export function createShop(db) {
  return {
    placeOrder(cart) { /* 40 lines */ },
    refund(order) { /* 35 lines */ },
  };
}

// Good: the factory only wires named functions together
export function createShop(db) {
  return { placeOrder: cart => placeOrder(db, cart), refund: order => refund(db, order) };
}
```

```toml
# pyproject.toml: Pylint counts statements, so set a limit that matches about 20 lines
[tool.pylint.design]
max-statements = 15
```

```python
# Good in Python: the class only wires named functions together
class Shop:
    def __init__(self, db):
        self.db = db

    def place_order(self, cart):
        return place_order(self.db, cart)

    def refund(self, order):
        return refund(self.db, order)
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers architecture playbook: https://agentwranglers.se/playbook/architecture/names-and-functions/small-functions`.

- Previous: [Use names you can search for and say aloud](https://agentwranglers.se/playbook/architecture/names-and-functions/searchable-names)
- Next: [Keep nesting shallow](https://agentwranglers.se/playbook/architecture/names-and-functions/shallow-nesting)
- [Names and functions](https://agentwranglers.se/playbook/architecture/names-and-functions)
