liveBook · Manning

Decision making

As explained in Data-Oriented Programming in Java

A decision is a modeled choice among alternative outcomes or actions based on available information.

Decision making becomes clearer when a program represents the choice itself, rather than hiding it across field assignments and nested conditionals. After a scheduled task fails, “retry immediately,” “retry later,” and “abandon” are distinct domain decisions—not merely different combinations of attempt counts, timestamps, and null values. Giving each outcome an explicit data type makes the allowed choices visible to readers and keeps their meanings from being inferred indirectly. section 1.3.2

Figure 1.2 Representing each decision as a piece of standalone data

Each possible post-failure outcome is named as standalone data, exposing meanings that implicit state changes conceal. section 1.3.2

A runtime if/else can be modeled as a fixed algebraic choice: a sealed Decision interface with one record for each option. Producing that value answers what should happen; interpreting it later determines how a particular part of the program responds. This separates a decision from its action, so multiple interpreters can use the same choice for different purposes. section 5.3.2

Listing 5.12 Runtime if/else as Compile time data
void example() {                       #A
    if (/* option #1 */) {...}         #A
    else if(/* option #2 */) {...}     #A
    else {...}
}
 
sealed interface Decision {                     #B
    record Option1() implements Decision {}     #B
    record Option2() implements Decision {}     #B
    record Option3() implements Decision {}     #B
 
}

The listing contrasts an if/else chain with a sealed decision type whose alternatives are explicit at compile time. section 5.3.2

Model decisions as data when their alternatives carry domain meaning:

  • Name every meaningful case, including an otherwise vague else branch. An assessment can be ABOVE_MAXIMUM, BELOW_MINIMUM, or WITHIN_RANGE rather than two named cases plus “anything else.” section 6.3.6
  • Attach the attributes that belong to a particular outcome. RetryImmediately carries a schedule time and attempt count, while ReattemptLater and Abandon express different facts. section 1.3.2
  • Keep the decision near the domain story. A late-fee draft can become BillableFee, NotBillable, or NeedsApproval before later steps handle each path. section 5.3.1 section 5.3.2
  • Interpret the same decision where needed. A switch can compute a value in one place and send reminders, start probation, or suspend an account in another. section 5.3.2

Late-fee assessment

A late-fee pipeline first collects past-due invoices and builds a draft. Assessment then returns one of three explicit decisions: BillableFee, NeedsApproval, or NotBillable. Billable fees may lead to BilledLateFee or RejectedLateFee; approval-needed fees start approval; non-billable fees become rejected late fees. The resulting types describe the lifecycle without requiring readers to decode a wall of nested conditions. section 5.3.1 section 5.3.2

This is a clarity tool, not a demand to replace every object or conditional. Introduce additional types where case names matter to the domain and where exposing the choice helps readers focus on the program’s real behavior. Objects can still provide behavior around the explicit decision data, such as asking a scheduled task whether it is abandoned. section 1.3.2 section 6.3.6

How Data-Oriented Programming in Java covers it

How deep it goes
Derives sealed decision types from if/else branches and traces their later interpretation through a lifecycle pipeline.
Examples
Uses failed scheduled-task retries, late-fee assessment, and account actions selected by decision interpreters.
What you can do after
Build it from scratch by naming decision cases as data and separating their production from later actions.
How it connects
Connects conditional expressions, algebraic data types, sealed interfaces, records, switch expressions, domain modeling, and lifecycle states.

Also explained in

Compare how 9 books explain decision making →

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.