liveBook · Manning

Impure function

As explained in Data-Oriented Programming in Java

An impure function is a function whose behavior can depend on external state or produce observable side effects beyond its returned result.

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

How Data-Oriented Programming in Java covers it

How deep it goes
Explains external dependencies, observable effects, non-determinism, testing consequences, and refactoring, but stops short of formal semantics.
Examples
Uses mutable setters, current-time queries, logging, database access, parallel reduction, and due-date calculation.
What it assumes
Assumes intermediate Java knowledge while introducing purity, determinism, and their role in software design.
What you can do after
Diagnose impure boundaries and refactor environmental inputs into parameters for simpler deterministic tests.
How it connects
Connects impure functions with pure functions, deterministic functions, side effects, dependency control, and testing.

Also explained in

Compare how 4 books explain impure function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.