Decide Guard Vs Exception

When to return early vs throw. Control flow clarity.

attac-t 4e7ec2b 2 files · 5.0 KB Updated

File contents

Skill: Guard vs Exception

"Guards return. Exceptions signal failure."

The Decision

Return early when:

  • Nothing to do is acceptable (empty collection, already processed)
  • Caller can continue normally with the return value
  • It's a business rule, not an error

Throw exception when:

  • Caller must handle the failure
  • Invalid state that shouldn't exist
  • Programmer error (null where null shouldn't be)

The Heuristic

Ask: "Can the caller proceed normally with a return value?"

  • Yes → Return (Result object, empty collection, early exit)
  • No → Throw (caller must handle or propagate)

The Quick Test

Scenario Answer Use
Empty input, nothing to do? Ok Return
Already processed? Ok Return
Rate limited, retry later? Maybe Return Result
Missing required data? Not ok Throw
Unauthorized access? Not ok Throw
Invalid state (bug)? Not ok Throw

Real-World Examples

See examples.md.

attac-t/the-foundry/tree/main/plugins/laravel-ddd/skills/decide-guard-vs-exception commit 4e7ec2b20a

Frequently asked questions

npx skillmds@latest add attac-t/decide-guard-vs-exception