liveBook · Manning

Side effect

As explained in Data-Oriented Programming in Java

A side effect is an observable change or interaction caused by a computation beyond producing its returned result.

A side effect is an observable change or interaction caused by a computation beyond producing its returned result. Updating state, reading the current time, calling a repository, logging, and notifying an external service all connect computation to the surrounding environment. A function is pure when it deterministically maps inputs to outputs without such effects; otherwise, it is impure. section 6.1.1 section 6.0

Treat side effects as design and testing concerns:

  • Separate data acquisition from domain calculation where possible, turning non-deterministic behavior into a deterministic function that is easier to reason about. section 6.1.1
  • Describe expected effects with data, allowing tests to state what should happen and assert that it happened without hiding behavior in shared setup. section 11.3.3
  • Treat mocking as an implementation detail driven by test data rather than as the structure of the test itself. section 11.3.3

Notification as a modeled effect

A service may inspect accounts and call a notifier when an account qualifies for a discount. A data-oriented test varies thresholds and account states, derives which accounts should be processed, and verifies the resulting notifications. The external interaction remains observable, but its expected behavior is explicit test data. section 11.3.3

Listing 11.26 Assume something like…
class MyService {
  private USD threshold; 
  private NotificationService notifier; 
 
  public void notifyEligibleAccounts(List<Account> accounts) {
      for (Account account : accounts) {
        if (/* complicated discount logic here */) {
            this.notifier.notify(
                account.id(), 
                "A discount is available!!!"
            );
        }
     } 
  }
}

Use the listing to see how an otherwise ordinary service method hides an external notification side effect behind complicated discount logic. section 11.3.3

How Data-Oriented Programming in Java covers it

How deep it goes
Defines side effects, contrasts pure and impure functions, and demonstrates deterministic refactoring and data-driven verification.
Examples
Uses state mutation, current time, logging, database access, parallel reduction, due-date calculation, and account notifications.
What it assumes
Assumes Java and testing familiarity while establishing purity, impurity, and determinism through concrete examples.
What you can do after
Diagnose side effects, isolate deterministic calculations, and test external interactions through explicit data and assertions.
How it connects
Connects side effects with pure functions, determinism, immutable values, mocking, and data-oriented testing.

Also explained in

Compare how 28 books explain side effect →

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.