liveBook · Manning

Functional, stateless philosophy

As explained in Unit Testing Principles, Practices, and Patterns

Functional programming is a programming paradigm that expresses computation through mathematical functions and minimizes hidden inputs, hidden outputs, and mutable state.

Functional programming expresses computation through mathematical functions: every input and output is explicit, and repeated calls with the same input produce the same result. Hidden inputs include internal or external state; hidden outputs include side effects and exceptions. section 6.3 section 6.6

The practical goal is to separate business logic from side effects. Functional architecture concentrates decisions in a functional core and moves state changes and external interactions into a mutable shell. section 6.6

Use these distinctions when evaluating a function:

  • An explicit function such as CalculateDiscount receives products and returns a discount without hidden inputs or outputs. section 6.3
  • Increment(int x) is predictable because its result depends on the explicit argument; an Increment() method that changes internal x is not. section 6.3
  • AddComment exposes text and a Comment result but also mutates _comments, making that mutation a hidden output. section 6.3
public decimal CalculateDiscount(Product[] products)
{
    decimal discount = products.Length * 0.01m;
    return Math.Min(discount, 0.2m);
}

Inspect CalculateDiscount as a compact example of explicit inputs, an explicit return value, and deterministic calculation. section 6.3

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines pure functions and functional architecture, but stops short of a deep functional-programming treatment.
Examples
Uses CalculateDiscount, Increment, and AddComment to contrast explicit computation with internal state and side effects.
What it assumes
Assumes unit-testing context while teaching the basic meaning of mathematical functions and hidden inputs and outputs.
What you can do after
Apply functional principles to separate business decisions from side effects and write more output-based tests.
How it connects
Connects mathematical functions with output-based testing, side effects, functional architecture, and hexagonal architecture.

Also explained in

Compare how 19 books explain functional, stateless philosophy →

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.