liveBook · Manning

Function

As explained in Data-Oriented Programming in Java

A computation that maps input values to an output value or values without requiring mutation of the input values.

A function turns known input values into an output. In data-oriented Java, that simple shape replaces updates to object identity: rather than changing a Ball, compute its next Ball and pass that result onward. Change emerges from a succession of immutable values, not mutation. section 2.5

Listing 2.27 Computing the next state from the current one
import static com.dop.chapter02.step
 
float simulatedDuration = 1.0f;
Ball ball = new Ball(new Point(0.0, 0.0), new Vector(1.0, 0.0)); #A
Ball updated = step(ball, simulatedDuration);
// [out] Ball(position=(1.0, 0.0), vector=(1.0, 0.0));
Ball updatedAgain = step(updated, simulatedDuration);
// [out] Ball(position=(2.0, 0.0), vector=(1.0, 0.0));

Each call to step receives a Ball and duration, then returns the next Ball; the previous value remains available unchanged. section 2.5

The useful semantic meaning is stricter than “any Java method.” A mathematical function deterministically assigns inputs to outputs: identical inputs produce identical outputs, the result comes entirely from those inputs, and calling it has no observable effect. Reading clocks, services, mutable state, or even logging makes behavior impure or non-deterministic in this sense. Immutable instance state is compatible with a function. section 6.1.1

Use functions to make domain behavior explicit and easier to reason about:

  • Model a relationship directly when a value alone loses its meaning. A grace period can map a due date to an adjusted date, with the chosen mapping determined by customer rating. section 6.1.6
  • Let input and output types constrain the implementation. Small, well-scoped functions often reduce to mapping each permitted case to its required result. section 6.3.2
  • Separate calculations with distinct responsibilities, such as totaling past-due invoices and computing a percentage fee, so code follows the business requirements. section 6.3.2
  • Refactor environmental dependencies into parameters, such as passing the current date and payment terms to a due-date calculation, making direct tests possible. section 6.1.1

Grace period as a returned function

A fixed number of days does not describe every grace-period rule: a customer in poor standing must pay by month end. Represent the rule as a TemporalAdjuster selected by CustomerRating. Good and acceptable ratings return date-to-date adjustments of 60 and 30 days; poor returns lastDayOfMonth. Applying the result to an invoice due date mirrors the business rule without inventing a misleading universal offset. section 6.1.6

Deterministic functions can be viewed as immutable mappings—conceptually, lookup tables of every input and its output. That makes replacing computation with precomputed data safe in principle, and lets tests or debugging substitute a known result without missing hidden state updates. But replacing an exhaustive typed switch with a Map can weaken compile-time guarantees: maps can omit keys and force runtime fallback handling for cases absent from the domain. section 6.1.5 section 6.3.1

Functions may be static helpers or methods on immutable value objects. An immutable Ball.step method still computes and returns a new Ball, allowing readable fluent chains. Static functions make the input explicit, while value methods can make transformations such as vector scaling and subtraction read left to right. The important distinction is semantic: values in, values out, without mutation. section 2.5 section 6.8

How Data-Oriented Programming in Java covers it

How deep it goes
Defines deterministic, side-effect-free functions and derives implementations from types, while treating purity pragmatically rather than formally.
Examples
Uses Ball stepping, due-date calculation, grace-period TemporalAdjusters, invoice filtering, and late-fee computations.
What it assumes
Assumes intermediate Java and builds from the distinction between ordinary methods and deterministic functions.
What you can do after
Apply it via Java methods and TemporalAdjuster to express deterministic domain rules and test them from explicit inputs.
How it connects
Connects immutable data, determinism, purity, type signatures, lookup tables, Maps, and testing.

Also explained in

Compare how 26 books explain 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.