Names and functions Recommended
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.
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)
// 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.
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.