Names and functions Required
Keep functions short
Keep each function at 20 lines or fewer. A longer function nearly always 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.
Example
// 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' } },
];
// 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) };
}
# pyproject.toml: Pylint counts statements, so set a limit that matches about 20 lines
[tool.pylint.design]
max-statements = 15
# 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
ESLint max-lines-per-function set to 20, PHPMD ExcessiveMethodLength with a minimum of 21, or Pylint too-many-statements, which counts statements instead of lines. The build fails above the limit, outside the excluded templates folder.
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.