liveBook · Manning

Nondeterminism

As explained in Data-Oriented Programming in Java

Non-determinism is the property that identical inputs can produce different outputs because of hidden mutable state or other varying conditions.

Nondeterminism means identical inputs can produce different outputs because execution depends on hidden mutable state, time, randomness, identity, external services, or other changing conditions. A function that reads the current date or calls a database is not determined by its visible arguments alone. section 6.1.1

Nondeterminism makes code harder to reason about and test because behavior depends on dependencies outside the function. Separate obtaining data from transforming it: pass values such as the current date and payment terms into a deterministic function, then test that function directly. section 6.1.1

Making a due-date calculation deterministic

A due-date method that calls LocalDate.now() and contractsClient.getTerms(...) mixes calculation with environmental access. Refactoring it to accept LocalDate and PaymentTerms makes the result depend only on explicit inputs, allowing a small test with a fixed date and NET_30 terms. section 6.1.1

Use these distinctions when organizing implementation:

  • Treat deterministic functions as a core that depends only on other deterministic functions. section 6.2
  • Place database calls, random-number generation, service calls, and environment access outside that core. section 6.2
  • Accept that practical code may contain theoretical nondeterminism through mutable identity objects; treat them as data when rare mutation risks are negligible. section 6.1.7
Listing 6.28 The simplified view of how data flows through our program
                            NOTES.md
-------------------------------------------------------------------
// step 1: collect the past due
List<Invoice> -> List<PastDue>   
 
// Step 2: use those to build the draft
List<PastDue> -> LateFee<Draft>           
 
// Step 3: decide what to do with the draft
LateFee<Draft> -> (Billable OR NotBillable OR NeedsReview
 
// then depending on what we decided, either:
// Step 4.1: submit billable items 
Billable -> (LateFee<Billed> OR LateFee<Rejected>) 
 
// OR Step 4.2: Start the approval process for those that need it
NeedsReviewed -> LateFee<InReview>
 
// OR Step 4.3: keep the non-billable data for posterity  
NotBillable -> LateFee<Rejected> 

Trace a late-fee workflow from collected invoices through draft construction, assessment, billing, approval, or rejection, and identify where deterministic transformations meet external effects. section 6.2

How Data-Oriented Programming in Java covers it

How deep it goes
Explains causes, testing consequences, refactoring steps, and architectural boundaries, while allowing practically negligible theoretical nondeterminism.
Examples
Uses current dates, database lookups, service calls, mutable person state, parallel reduction, and late-fee processing as examples.
What it assumes
Assumes intermediate Java and teaches the purity–impurity distinction through functions, inputs, outputs, and side effects.
What you can do after
Diagnose/debug with it by locating hidden dependencies, extracting deterministic functions, and testing them without extensive mocks.
How it connects
Connects nondeterminism to determinism, purity, side effects, dependency direction, functional core, and imperative shell.

Also explained in

Compare how 5 books explain nondeterminism →

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.