liveBook · Manning

Chapter 8: Business Rules as Data

What you will learn in chapter 8 of Data-Oriented Programming in Java

The chapter introduces data as a starting point for modern Java design, contrasting it with Java’s reputation for inheritance-heavy abstractions.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces data as a starting point for modern Java design, contrasting it with Java’s reputation for inheritance-heavy abstractions.

Key terms: records, pattern matching, data

8.1 Let’s start with an example

The section sets up an account-routing problem with numerous changing business rules and introduces a small, purpose-built data model for the accounts being routed.

Key terms: customer accounts, sales teams, rules, Account, Entity, plain data, domain types, enums, custom records

8.1.1 What does it mean to be correct?

Defines correctness for account-to-sales-organization rules as non-overlap, then shows how their inputs and outputs support a global invariant test.

Key terms: Rule, Account, SalesOrgId, disjoint, non-overlapping sets, Image, domain, invariants, algebraic properties

8.2 Muscling through the rule implementation

This section shows why directly coding many business rules becomes hard to debug and maintain, and motivates an interpreter that can explain rule outcomes.

Key terms: rules, if/else statements, Boolean expression, debug and monitor, runtime information, hard coded, portable, interpreter, rules engine, Domain Specific Languages (DSLs)

8.3 Capturing rules as plain data

The section represents an equality-check operation as data and narrows its attribute field to valid Account attributes.

Key terms: operations as data, equality check, Equals, Account, Enum, String

8.4 Building a simple Algebra

This section shows how to represent compound and excluding business rules as a small algebra of data, then make that algebra easier to use.

Key terms: AND, OR, closed algebra, sealed interface, sum type, inclusive style rule, negates values, algebra, static constructor methods, default methods

8.4.1 Building better interfaces into the algebra

This section shows how to build concise, composable business rules by combining simple algebraic operations and adding a more convenient interface.

Key terms: compound actions, And, Or, binary operations, stream tooling, Rules, Algebra, eq(Country, ...), composite rules

8.5 Variation I: Deciding what data means

The section shows how to interpret business rules represented as data, then explains what that representation enables and what the basic interpreter cannot yet explain.

Key terms: interpreter, sealed interface, pattern match, Attribute, Account, Equals, ordinary data, portable, serialize, debuggability

8.6 Variation II: A more powerful interpreter for the same data

This section shows how the same rule data can be interpreted to produce both a decision and an explanation, while noting a remaining type-safety limitation.

Key terms: Booleans, Result type, interpreter, provenance information, denotations, raw strings, compile time safety, non-matching rule, runtime

8.7 Variation III: Adding Type Safety

The section makes equality rules type-safe by linking each account attribute to its value type, then adapts the interpreter while keeping the rules as data.

Key terms: Equals, String, Account, Segment, Attr<A>, Attribute, Region, interpreter, evidence, wildcard

8.8 What did this cost us?

This section shows that the hand-built, type-safe interpreter takes about 60 lines of pure Java and needs no dependencies.

Key terms: interpreter, type safe, dependencies, pure Java

8.9 But It doesn’t do everything!

This section argues that representing business rules as data is useful without turning the resulting code into a general-purpose interpreter or framework.

Key terms: interpreter, DSL, business code, requirements, model with data, mini-interpreters, core ideas in your domain

8.10 Data is flexible in a way that code isn’t

The section shows how representing business rules as data makes it possible to generate documentation and visualizations from the rules themselves, rather than maintaining separate descriptions by hand.

Key terms: rules as code, rules as data, documentation pipeline, Tree, GraphViz, nodes and edges

8.11 Extending the algebra:

This section extends the business-rule algebra to compare account data and explains the Java type issue involved in interpreting those comparisons.

Key terms: GreaterThan, LessThan, algebra, interpreter, wildcards, generic types, switch expression

8.12 Wrapping up

The section concludes that modern Java makes modeling operations as data and interpreting them less costly than older pattern-heavy approaches.

Key terms: operations as data, interpreters, records, pattern matching

8.13 Summary

Concepts explained in this chapter

← Chapter 7: Reasoning About Correctness Chapter 9: Refactoring Legacy Code →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.