← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Fail Fast Principle

quality Beginner
debt(d7/e3/b5/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list phpstan and psalm as tools, but these static analysers can flag some patterns (e.g. unhandled nulls) yet cannot reliably detect the full range of fail-slow patterns — swallowed exceptions, silent null returns used as error signals, or the @ operator suppression — without careful configuration and human review. Many violations slip through to runtime testing.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is conceptually a one-liner (throw instead of return null/false), but the common_mistakes show multiple patterns spread across constructors, catch blocks, and error-suppression sites. Each individual fix is small, but systematically replacing all fail-slow patterns across a component requires a small, targeted refactor rather than a single-call swap.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). Failing to apply fail-fast principles applies across web, cli, and queue-worker contexts and affects how errors propagate throughout the entire codebase. Silent failures and invalid state propagation slow down debugging and maintenance across many work streams, imposing an ongoing productivity tax, but it doesn't fully reshape every architectural decision.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field documents the canonical wrong belief precisely: developers conflate fail-fast with crashing aggressively on any error, when it actually means detecting and reporting invalid state close to the source. This is a documented, commonly learned gotcha — serious enough to cause misapplication, but not so perverse that it contradicts a concept from a completely different domain.

About DEBT scoring →

Also Known As

fail early early failure fast failure principle

TL;DR

Detect and report errors at the earliest possible point rather than allowing invalid state to propagate and cause confusing failures elsewhere.

Explanation

Failing fast means: validate inputs immediately on entry, throw exceptions rather than returning error codes that callers may ignore, assert invariants at the start of methods, and surface configuration errors at startup rather than at runtime. The alternative — letting invalid state propagate — produces hard-to-debug failures far from the root cause. In PHP, strict types, guard clauses, and early throws implement fail-fast behaviour at the language level.

Common Misconception

Fail fast means crashing the application at the first sign of any error. It means detecting and reporting invalid state as close to the source as possible rather than propagating bad data silently until it causes a confusing failure downstream.

Why It Matters

Failing fast surfaces errors at the point of origin rather than allowing invalid state to propagate — the further a bug travels from its source, the harder it is to diagnose and fix.

Common Mistakes

  • Returning null or empty values on failure instead of throwing an exception — callers silently pass invalid state downstream.
  • Not validating constructor arguments — allows objects to be constructed in an invalid state.
  • Catching exceptions and retrying silently without limit — turns a fast fail into a slow, silent hang.
  • Suppressing errors with @ operator — PHP's most notorious fail-slow pattern.

Code Examples

✗ Vulnerable
function process(array $data): Result {
    if (isset($data['user'])) {
        if ($data['user']['age'] >= 18) {
            if (!empty($data['user']['email'])) {
                // actual work buried in nesting
            }
        }
    }
}
✓ Fixed
function process(array $data): Result {
    if (!isset($data['user']))           throw new \InvalidArgumentException('user required');
    if ($data['user']['age'] < 18)       throw new UnderageException();
    if (empty($data['user']['email']))   throw new \InvalidArgumentException('email required');

    // happy path — no nesting
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 38
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 3 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 1 ping M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F
No pings yet today
No pings yesterday
Perplexity 9 Amazonbot 8 Ahrefs 6 Unknown AI 3 SEMrush 3 Majestic 1 ChatGPT 1
crawler 30 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Throw an exception immediately when detecting invalid state — don't return null or false and let the error silently propagate to cause a confusing failure elsewhere
📦 Applies To
any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Returning null/false on error instead of throwing; swallowed exceptions; invalid state allowed to continue execution
Auto-detectable: ✓ Yes phpstan psalm
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update

✓ schema.org compliant