ARC-14Recommended
Keep functions short
Keep each function at 20 lines or fewer, or write down why a longer one reads better. A longer function usually does more than one thing.
How to do it
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. The limit is a rule of thumb, not a measured law. Splitting a function only to pass it makes the code worse, so a stated reason beats a forced split. Short functions also help agents, as Keep code small enough for an agent to read explains.
Examples
JavaScript example
// eslint.config.js: the limit applies everywhere except content templates
import comments from '@eslint-community/eslint-plugin-eslint-comments';
export default [
{ rules: { 'max-lines-per-function': ['error', { max: 20, skipBlankLines: true, skipComments: true }] } },
{ files: ['src/templates/**'], rules: { 'max-lines-per-function': 'off' } },
// A disable comment must say why
{ plugins: { '@eslint-community/eslint-comments': comments }, rules: { '@eslint-community/eslint-comments/require-description': 'error' } },
];
// eslint-disable-next-line max-lines-per-function -- the tax steps read best as one list
export function vatForInvoice(invoice) { /* 26 lines */ }
JavaScript example 2
// 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 example
# pyproject.toml: Pylint counts statements, so set a limit that matches about 20 lines
[tool.pylint.design]
max-statements = 15
Shell example
# Fails on a Pylint size disable comment that gives no reason
! grep -rnE 'pylint: disable=(too-many-statements|too-many-nested-blocks|too-many-arguments)' src | grep -v '# reason:'
Python example
# 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)
How to check
A linter flags every function over the limit, outside the excluded templates folder. Examples are ESLint max-lines-per-function set to 20, PHPMD ExcessiveMethodLength with a minimum of 21, or Pylint too-many-statements, which counts statements. The build passes only when each flagged function has a disable comment that gives its reason.
Related rules
- ARC-8 Write code that agents can parse in Clean code and architecture
- ARC-19 Let the code explain itself in Clean code and architecture
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/names-and-functions/small-functions">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.