Agent Wranglers

ARC-17Recommended

Make each function do one thing

Each function does one thing at one level of detail. A function either changes something or answers a question, never both.

How to do it

If you can describe a function only with "and", split it. Keep high-level steps and low-level details in separate functions. Commands return nothing, and queries change nothing. A command that creates a record may return its new ID.

Examples

JavaScript example

// Bad: checks, saves and emails in one place
function handleSignup(form) { /* validate, insert row, send welcome email */ }

// Good
function handleSignup(form) {
  const account = createAccount(validateSignup(form));
  sendWelcomeEmail(account);
}

Python example

# Good in Python: each step has its own named function
def handle_signup(form):
    account = create_account(validate_signup(form))
    send_welcome_email(account)

How to check

A review question asks whether each function's name describes everything it does. A function named as a query, starting with get, find, is or has, returns a value and writes nothing. Tests for a query never need to check a side effect.

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/one-thing-per-function">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.