Defensive Programming Vs Fail Fast · TL;DR
1 min readRapid overview
TL;DR
| Layer | Strategy | Example |
|---|---|---|
| API/Controllers | Defensive | Validate input, return user-friendly errors |
| Application Services | Mixed | Transform external data (defensive), enforce business rules (fail-fast) |
| Domain Entities | Fail-Fast | Enforce invariants strictly |
| External Integrations | Defensive | Handle unreliable data, provide fallbacks |
| Critical Calculations | Fail-Fast | Ensure correctness, fail loudly on errors |
Key Takeaway: Balance resilience (defensive) with correctness (fail-fast) based on the layer and criticality of the operation.