Briefle words · Code & architecture
Guard clause
A check at the top of a function that exits early when an input or condition isn't acceptable, before the main logic runs.
Say it to an AI
TypeScript dispatchShipment(order) that books a courier for an order. Validate with guard clauses at the top (unpaid, no items, missing postcode), each throwing its own error, then keep the happy path flat at the bottom. No nested if/else.
How an expert would brief the stronger version of “Shipment dispatch”.
See it
Try it as a puzzle →A pull request adds the function that books a courier for a paid order in a delivery platform.
Why: A's three 'if (...) { throw ... }' blocks are guard clauses: unpaid, empty and address-less orders are turned away in the first 10 lines.
More ways experts use it
Python function book_shipment(order, weight_kg, postcode). Fail fast: guard clauses at the top validate the weight (0–1000 kg) and the UK postcode and raise ValidationError before any side effects, like reserving a truck slot or creating the carrier label.
Shipment booking · Code & architecture
Related words
Room 1 · Essentials
Learn “Guard clause” in a five-minute lesson