---
title: "Split blocks into named functions · Agent Wranglers"
url: https://agentwranglers.se/playbook/architecture/names-and-functions/extract-and-name
updated: 2026-10-20
licence: "All rights reserved"
---

# Split blocks into named functions

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

- Level: Recommended
- Chapter: [Names and functions](https://agentwranglers.se/playbook/architecture/names-and-functions)
- Rule: When a block of code needs a moment of thought, move it into a function whose name says what it does.
- How: Look for blocks with a comment above them, long conditions and repeated steps. Extract each into a function and use the comment as its name. The calling code then reads like plain sentences. Methods written inside a factory are blocks too, so move them out and name them.
- Check: The review answers question 10 with each block worth extracting. No `if` joins more than two clauses without a name. Pylint `too-many-boolean-expressions` with `max-bool-expr=2` enforces this in Python, and eslint-plugin-sonarjs `expression-complexity` gives a similar limit in JavaScript.

Example:

```python
# Bad
if customer.orders_count > 10 and customer.refunds_count == 0 and customer.joined < one_year_ago:
    apply_discount(order)

# Good
def is_loyal(customer):
    return customer.orders_count > 10 and customer.refunds_count == 0 and customer.joined < one_year_ago

if is_loyal(customer):
    apply_discount(order)
```

```ts
// Good
const isLoyal = (customer: Customer) =>
  customer.ordersCount > 10 && customer.refundsCount === 0 && customer.joined < oneYearAgo;

if (isLoyal(customer)) applyDiscount(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/extract-and-name`.

- Previous: [Make each function do one thing](https://agentwranglers.se/playbook/architecture/names-and-functions/one-thing-per-function)
- [Names and functions](https://agentwranglers.se/playbook/architecture/names-and-functions)
