liveBook · Manning

Fail-fast

As explained in Unit Testing Principles, Practices, and Patterns

A software design principle that detects and reports invalid conditions as early as possible during execution.

Fail-fast means stopping an operation as soon as an unexpected error appears. Early failure shortens the feedback loop, reduces repair cost, and prevents corrupted state from spreading into persistence. section 8.1

Exceptions commonly implement fail-fast behavior: they interrupt normal control flow, reach the highest execution level, and allow the application to log, shut down, or restart the operation. section 8.1

Reject an invalid state before changing it

A User method can require CanChangeEmail() to succeed before continuing. If a controller skips that precondition, the application crashes on its first execution rather than silently producing a corrupted result. section 8.1

Apply the principle where incorrect assumptions must become visible immediately:

  • Validate preconditions; a failed precondition signals a bug in the assumed application state. section 8.1
  • Validate configuration during startup and throw when required data is incomplete or incorrect. section 8.1
  • Prefer fast failure over a low-value integration test when the defect will manifest immediately and cannot corrupt data. section 8.1

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Explains operation interruption, exceptions, preconditions, configuration validation, and the trade-off with integration tests.
Examples
Uses email-change preconditions, controller behavior, and startup configuration validation as concrete examples.
What it assumes
Assumes familiarity with execution flow, exceptions, application state, and the distinction between unit and integration tests.
What you can do after
Diagnose invalid assumptions earlier and prevent faulty operations from continuing into persistent state.
How it connects
Connects preconditions, exceptions, integration testing, unit testing, configuration validation, and the Test Pyramid.

Also explained in

Compare how 2 books explain fail-fast →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.