A deterministic function maps given inputs to one consistent output. It gets data, transforms it, and returns data—without consulting changing environment, service state, identity, time, random values, or side effects. This makes behavior easier to reason about, reuse, and test. section 6.1 section 6.1.1
The useful design question is not whether every method must be deterministic, but whether a method can and should be. Reading a repository, calling an API, checking the current time, or logging couples code to conditions outside its inputs. Pass the required date, terms, or domain data in instead, then keep the decision logic focused on the mapping. section 6.1.1 section 6.1.2
Make the due-date calculation explicit
A due-date method that calls LocalDate.now() and fetches payment terms must control both dependencies in a test. Change it to accept currentDate and PaymentTerms. Its switch then has one job: map the supplied terms and date to a due date. A test can provide inputs and assert the result directly, with no mocks. section 6.1.1
Treat determinism as a practical boundary:
- Keep business transformations in a deterministic core that depends only on deterministic code. section 6.2
- Put database access, external APIs, clocks, randomness, and other effects at the boundary around that core. section 6.2
- Split mixed operations such as file loading into an impure read step and a deterministic parse-and-model step. section 10.4.3
- Use static methods as a convention that signals deterministic intent and makes instance state less convenient to reach for. section 6.1.2
Purity adds a stronger condition: a pure function is deterministic and free of side effects. A calculation can return the same value while still producing non-deterministic observations—for example, logging from a parallel reduction makes log order vary. Immutable value state is compatible with deterministic behavior when the value itself cannot change. section 6.1.1
Types limit the behavior a deterministic function can express. For a small input enum and output enum, the possible mappings are finite; an exhaustive switch makes those choices visible. Combining deterministic functions produces another deterministic function, allowing larger transformations to remain understandable as input-output pipelines. section 6.1.3
Listing 6.2 Mathematical functions deterministically assign inputs to outputs
public int increment(int x) { #A
return x + 1; #A
} #A
public double square(double x) { #A
return x * x; #A
} #A
enum Status {ERROR, WARNING, INFO}
enum Color {RED, YELLOW, GREEN}
public Color color(Status status) { #B
return switch(status) { #B
case ERROR -> Color.RED; #B
case WARNING -> Color.YELLOW; #B
case INFO -> Color.GREEN; #B
};
}
public <A> List<A> concat(List<A> xs, List<A> ys) { #B
return Stream.concat(xs.stream(), ys.stream()).toList(); #B
} #BCompare simple arithmetic, an exhaustive status-to-color mapping, and list concatenation: each receives values and deterministically returns a value. section 6.1.1
Conceptually, a deterministic function is an immutable lookup table from inputs to pre-determined outputs. Replacing its execution with a precomputed answer does not change program behavior. That equivalence is useful in tests and debugging, where a known value can stand in for computation without hiding a state update. section 6.1.5
Tests for deterministic code are small: construct domain data, invoke the function, and assert its output. Varying scenarios means varying inputs. By contrast, code that mixes decision logic with clocks and APIs requires setup, mocks, and coordination with hidden state before its actual behavior can be exercised. section 11.2











