Codacoda
Back to Academy

coding principles

Fail Fast

The Fail Fast principle states that a system should immediately report any condition that is likely to lead to failure. Rather than continuing with invalid data and producing mysterious errors downstream, validate inputs and preconditions at the earliest possible point and throw clear, actionable errors. This makes bugs easier to locate because the error occurs close to the root cause rather than manifesting as a confusing side effect far away in the code.

Use Cases

  • Validating function arguments at the top of a function before doing any work
  • Checking configuration values at application startup rather than when they are first used at runtime
  • Using strict type checking and assertions during development to catch errors early
  • Validating API request payloads at the controller level before passing data to business logic

Visualization

BEFOREApiHandlerhandleRequest()processData()try/catch → nullsaveResult()try/catch → nullnotifyUser()try/catch → nullResponse: 200 OKdata: null
Speed:1x
Every function swallows exceptions with try/catch returning null. Errors are silently ignored and a 200 OK is returned with null data.Step 1 / 7

Implementation

Output

Click "Run Code" to see output...