A pure function maps inputs to outputs deterministically and leaves no observable trace outside that mapping. Given the same inputs, it returns the same result; it does not read mutable external state, call services, consult the clock, mutate state, or log. Immutable value state is compatible with purity: adding two USD values can read their immutable fields and return a new value. section 6.1.1
Listing 6.1 We might call all of these “functions”
class Example {
private int value;
public static int increment(int x) {
return x + 1;
}
public void setValue(int value) {
this.value = value;
}
public LocalDate getEndOfMonth() {
return LocalDate.now().with(lastDayOfMonth());
}
public String sayHello(String name) {
return LocalDateTime.now().getHour() < 12
? "Good morning, " + name
: "Howdy, " + name;
}
}Compare methods that merely calculate with methods that change state or consult the current time. Similar method syntax can conceal radically different dependencies. section 6.1.1
Purity is a design criterion, not a claim that every method must be isolated. When a calculation currently obtains data itself, separate how the data is acquired from what is done with it. Pass the current date and payment terms into a due-date calculation rather than reading the clock or calling a contracts service inside it. The resulting function can be tested with direct inputs and an expected output. section 6.1.1 section 11.2
Use pure functions to make business logic easier to inspect and change:
- Put every required value in the parameters and return the computed result, so behavior can be understood locally rather than by tracking surrounding state. section 6.1.4
- Treat logging, clocks, databases, APIs, mutable instance fields, and identity-dependent behavior as boundaries that make a function impure. section 6.1.1
- Use static methods as a team convention to signal deterministic intent and block casual access to instance state; review still must prevent access to global or environmental state. section 6.1.2
- Let narrow input and output types constrain the behaviors a function can express, rather than relying only on a method name. section 6.1.3
Model a grace period as a relationship
A grace period is not always a fixed number of days. Good and acceptable customers receive offsets, while poor-standing customers pay by the end of the month. Model that meaning as a deterministic mapping from CustomerRating to a date-adjusting function. Applying the returned TemporalAdjuster to a due date keeps each case explicit and lets the past-due rule read as a direct expression of the policy. section 6.1.6
Pure functions compose: combining deterministic transformations produces another deterministic transformation. Their isolation also changes tests from environment orchestration into scenarios: construct an invoice, rating, and date; invoke isPastDue; assert the result. This avoids mocks, fixed clocks, setup state, and side-effect control when testing the decision itself. section 6.1.3 section 11.2






















