Overview

8 Business Rules as Data

In this chapter, business rules are treated as data instead of hard-coded control flow, which makes them easier to model, inspect, reuse, and evolve. The example centers on routing customer accounts to sales organizations based on a messy set of human-defined conditions, and shows how plain records and enums can capture just enough of the domain to express those rules without dragging in heavyweight abstractions.

The core idea is to define a small algebra of rule data, including equality, conjunction, disjunction, and negation, and then interpret that data in different ways. One interpreter evaluates a rule against an account to produce a Boolean result, while another returns a richer result that explains why a rule matched or failed. By separating the representation of the rule from its meaning, the same data can drive execution, debugging, documentation, and even visualization.

The chapter then improves safety and expressiveness by adding typed attributes, generic equality, comparison operators, and helper combinators such as contains, any, and all. Java records, sealed interfaces, and pattern matching make it practical to build this tiny rules language in only a small amount of code, while still preserving portability and enabling future tooling. The main takeaway is that modeling operations as data gives Java a flexible, inspectable, and extensible foundation for solving real business-rule problems without overengineering.

The challenge of routing a customer account to the right sales organization
What we’re working with
Good modeling hides complexity we don’t care about.
Rules must each map to a unique org.
What shouldn’t happen. How would we pick which rule was right?
The job of a rule is to partition accounts and OrgIds into disjoint sets
Capturing what we know after evaluating a condition as data
Modeling operations as data
Creating an enum that models the relevant attributes
Modeling logical AND and OR as plain data
Modeling negation as plain data
Viewing higher level operations like contains in terms of simpler primitives
How do we get from data back to code?
Translating data back into code
This same simple data model could power a fancy UI in the future
Changing what our data denotes
Parameterizing Equality with a generic type variable
Somehow, we have to relate these two input values at the type level
Forcing that types all be the same
Giving concrete evidence to the type system that we know what we’re doing
Visualizing rules

Summary

  • Records and pattern matching have changed the cost of building custom DSLs in Java
  • In addition to nouns, “things we know,” and decisions, data can also model operations
  • Modeling operations as data decouples what we want to do from how we do it
  • It also decouples when we do it. Data describes operations we’ll evaluate later
  • Operations as data are just data! They can be stored, serialized, transformed, and transmitted like any other data
  • Expressing operations as data types allows us to build algebras on top of that data
  • Complex algebraic operations can be built from simpler primitives (like And and Or)
  • Algebras aren’t “stuck” with one data type; we can build layers that transform more convenient data types into our algebra
  • Designing around an algebra yields compositional APIs without any effort on our part
  • Interpreters are just denotations in reverse. We decide what the data means
  • Data can be interpreted however we want (and in more than one way!)
  • The line between interpreter, DSL, and “just writing code” is blurry in the world of data-oriented programming.
  • The more well modeled your data, the more it will fall into interpreter-like shapes
  • It’s OK to build small “interpreters” that solve the problem at hand and nothing else!

FAQ

What is the main idea behind modeling business rules as data?Business rules can be represented as plain data structures instead of hard-coded logic. This makes them easier to store, inspect, transform, serialize, and reinterpret in different ways.
Why does the chapter use a sales account routing example?It shows a realistic case where many overlapping, changing rules decide which sales organization handles an account. The example highlights how messy business rules become when they are scattered across code.
What does it mean for a rule to be correct in this chapter?Rules are considered correct when they partition the input space cleanly: no two rules should overlap on the same inputs, and no two rules should map the same inputs to different outputs.
Why are overlap and duplication in rules a problem?Overlapping or duplicated rules make it unclear which rule should win. They also create maintenance issues because the system may behave unpredictably or become hard to reason about.
What is the role of an interpreter in this approach?An interpreter defines what the data means. It takes the rule data and evaluates it against an account, turning the data back into executable behavior.
Why is a sealed interface useful for the rule algebra?A sealed interface lets the rule set stay closed and explicit. That makes pattern matching easier and ensures the interpreter can handle every rule type exhaustively.
How do AND, OR, and NOT fit into the rule model?They are modeled as data variants alongside equality checks. This allows complex business logic to be composed from a small set of reusable building blocks.
Why does the chapter move from strings to typed attributes?Using raw strings allows typos and invalid combinations that only fail at runtime. Typed attributes improve safety by ensuring only valid domain values are used with each field.
How does the chapter add better debugging and traceability?Instead of returning only a Boolean, the interpreter can return a richer result that records what was expected and what was found. This gives useful explanations for why a rule matched or failed.
Why is expressing business rules as data considered flexible?Because once rules are data, they can be reused in many ways: evaluated, serialized, documented, visualized, or shown in a UI. The same rule set can support multiple interpretations without changing the original data.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Data-Oriented Programming in Java ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Data-Oriented Programming in Java ebook for free