ARC-18Recommended
Split blocks into named functions
When a block of code needs a moment of thought, move it into a function whose name says what it does.
How to do it
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.
Examples
Python example
# 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)
TypeScript example
// Good
const isLoyal = (customer: Customer) =>
customer.ordersCount > 10 && customer.refundsCount === 0 && customer.joined < oneYearAgo;
if (isLoyal(customer)) applyDiscount(order);
How to 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.
Related rules
- ARC-15 Keep nesting shallow in Clean code and architecture
- ARC-20 Refactor clever code instead of explaining it 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/extract-and-name">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.