Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Functional Programming Principles

93. Immutability

Definition. Avoid modifying existing data; create new values instead. Immutable data simplifies reasoning and sharing.

Rationale. Mutation makes state changes hard to track and leads to bugs, especially with concurrency. Immutable values are safe to share and easy to reason about.

Example. Update a list by returning a new list with the appended item rather than pushing in place.

Anti-pattern / failure mode. In-place mutation of shared state; “spooky action at a distance.” Review signal: bugs that depend on call order.

Related. Pure Functions (#94), Side Effect Isolation (#99).

94. Pure Functions

Definition. The same input produces the same output without side effects; pure functions are easy to test and compose.

Rationale. Purity makes functions predictable and testable in isolation. It also enables memoization, parallelization, and refactoring.

Example. A function that computes a total from inputs, with no I/O or mutation.

Anti-pattern / failure mode. Functions that read global state, mutate inputs, or perform I/O while claiming to compute. Review signal: tests that need elaborate setup or mocking.

Related. Referential Transparency (#95), Function Composition (#96).

95. Referential Transparency

Definition. An expression can be replaced by its value without changing program behavior, enabling safe refactoring and optimization.

Rationale. When expressions are referentially transparent, you can substitute, reorder, or cache them freely. This unlocks equational reasoning and optimization.

Example. Replacing f(x) with its result anywhere without changing meaning.

Anti-pattern / failure mode. Functions whose result depends on hidden state or time. Review signal: cannot simplify or inline an expression safely.

Related. Pure Functions (#94), Immutability (#93).

96. Function Composition

Definition. Build complex behavior by combining smaller functions; compose simple pieces into larger pipelines.

Rationale. Small functions are easy to test and reuse; composition assembles them into complex behavior without new code. It keeps logic declarative.

Example. process = validate |> transform |> store, each step a small function.

Anti-pattern / failure mode. Large monolithic functions that do many things; copy-pasted pipelines. Review signal: hard-to-test, hard-to-reuse blocks.

Related. Higher-Order Functions (#97), Declarative Programming (#98).

97. Higher-Order Functions

Definition. Functions can accept or return other functions; pass behavior as data to generalize operations.

Rationale. Treating functions as values lets you abstract control flow and customize behavior without subclassing or conditionals.

Example. map, filter, and reduce accept a function and apply it uniformly.

Anti-pattern / failure mode. Duplicating loop logic with slight variations instead of parameterizing the action. Review signal: many near-identical loops.

Related. Function Composition (#96), Declarative Programming (#98).

98. Declarative Programming

Definition. Describe what should happen rather than how to execute every step; let the runtime handle execution details.

Rationale. Declarative code states intent and lets the system choose an efficient execution. It is often clearer and less error-prone than imperative steps.

Example. A SQL query describes the desired result; the database plans execution.

Anti-pattern / failure mode. Over-specifying execution order for things that could be optimized or parallelized. Review signal: code that micromanages steps the runtime could handle.

Related. Function Composition (#96), Side Effect Isolation (#99).

99. Side Effect Isolation

Definition. Keep side effects at explicit boundaries; separate pure logic from I/O and mutation.

Rationale. Mixing side effects with logic makes code hard to test and reason about. Isolating them keeps the core pure and effects auditable.

Example. Compute a result in a pure function, then perform I/O in a thin shell.

Anti-pattern / failure mode. Business logic that reads/writes databases or files inline. Review signal: tests that require real I/O or mocking frameworks.

Related. Pure Functions (#94), Immutability (#93).

100. Recursion over Mutable Iteration

Definition. Use recursion or functional iteration constructs rather than mutable loop state where appropriate; prefer immutable traversals.

Rationale. Mutable loop state is a common source of bugs and is hard to reason about. Functional iteration expresses intent clearly and avoids accidental mutation.

Example. Use map/reduce or recursive functions instead of for loops with mutable accumulators.

Anti-pattern / failure mode. Complex loops with multiple mutable variables and early exits. Review signal: off-by-one and state bugs in loops.

Related. Immutability (#93), Higher-Order Functions (#97).