An impure function depends on external state or produces observable effects beyond its returned value. Unlike a pure function, it is not completely isolated: time, databases, mutable state, logging, and other environmental interactions can affect its behavior or the world around it. section 6.1.1
Recognize an impure function by the dependencies and effects hidden behind its signature:
- Reads changing state, such as the current date, a person’s mutable friends list, or a database. section 6.1.1
- Changes the environment through operations such as setting instance state or writing a log message. section 6.1.1
- Becomes harder to reason about and test because behavior depends on call order, services, identity, or other uncontrolled dependencies. section 6.1.1
- Hides an unbounded range of possible behavior behind a method signature, unlike a pure function whose behavior is constrained by its input and output types. section 6.1.3
Separating environmental effects from domain logic
A due-date method is impure when it obtains the current date and payment terms from external collaborators before calculating the result. Extracting those values as parameters leaves a small deterministic function: given a date and terms, it returns the corresponding due date. Tests can then call that function directly without mocking time or a contracts service. 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 ordinary-looking Java methods: arithmetic is isolated, while setters mutate state and date- or time-based methods consult the environment. section 6.1.1



