Fail Fast

Report errors immediately and stop

“A fail-fast system is one which immediately reports at its interface any condition that is likely to lead to failure.”

When to use

Input validation, startup checks, critical assertions.

Why it matters

  • Debugging: The error happens right where the bug is (e.g., “Invalid Argument passed to function”) rather than 100 lines later (“Null Pointer Exception”).
  • Safety: Prevents the system from continuing in an unstable state.

Signs of Violation

  • returning null or false silently when something goes wrong, causing a crash much later.
  • “swallowing” exceptions.

Explanation

Problem

If you pass null to a function that expects a string, and the function just ignores it, the system might write a blank record to the database. You won’t know until users complain about blank records.

Solution

Throw an exception immediately. if (!arg) throw new Error("Arg required").

Real world analogy

Assembly line. If a car part is defective, stop the line immediately. Don’t build the whole car and then find out the engine doesn’t start at the end.

Pros and Cons

Pros Cons
  • Easier debugging
  • Data integrity
  • System might seem “fragile” (crashes often)
  • Comparison

    • Fail Safe: A different strategy where the system tries to keep running (e.g., flight control software). Fail Fast is usually better for business apps.

    Code example

    Typescript

    Bad (Violation)

    Good (Adherence)

    PHP

    Bad (Violation)

    Good (Adherence)