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



























