liveBook · Manning

Chapter 3: Encoding Semantics in the Type System

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

The chapter introduction argues that understanding what data means is a foundation for building reliable systems before representing that meaning in code.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction argues that understanding what data means is a foundation for building reliable systems before representing that meaning in code.

Key terms: data, meaning

3.1 “What does it mean?”

The section shows how asking what each field means reveals domain facts and invariants that a mechanical JSON-to-Java translation would miss.

Key terms: code-first, shapes, meaning, daysElapsed, sparse, invariants, contactAngle, degrees, signed

3.1.1 Discovering more about your domain

The section uses a Sample ID to show why understanding domain data requires looking beyond its string format to the meaning and constraints of its parts.

Key terms: ID, opaque identifiers, domain information, Sample ID, method, number, syntax, semantics, invariants

3.1.2 The importance of understanding meaning

Understanding what data means through careful domain questioning is the foundation for designing software that solves customer problems.

Key terms: meaning, domain, data, understanding

3.2 Capturing semantics in code

This section shows why the existing Measurement record needs to encode the meaning of its data, not merely accept values of the right Java types.

Key terms: Measurement record, type system, semantically correct, invariants

3.2.1 Detailed documentation and better variable names are not enough

The section explains why documentation and descriptive names clarify data’s meaning but cannot prevent code from violating its invariants.

Key terms: documentation, variable names, invariants, well-formed

3.2.2 Enforce invariants during construction

The section shows how constructor validation protects a data type’s invariants, but not the meaning or constraints of primitive values read back out of it.

Key terms: invariants, validation checks, constructor, data type, Measurement, contact angle, double, constraints, meaning

3.2.3 Types denote sets of values

This section treats a type as a set of concrete values and uses that view to check whether a chosen type matches the meaning of domain data.

Key terms: type, set of values, int, meaning, data model, friction

3.3 Using the type system as a design tool

The section uses a contact-angle example to show how comparing a type’s possible values with data’s domain meaning can reveal a modeling problem, and how a more specific type preserves that meaning.

Key terms: meaning, contactAngle, double, Degrees, constraints, data type, Measurement, compiler, type level, re-validate

3.3.1 Balancing type specificity with abstraction

This section explains how choosing between a domain-specific angle type and a more flexible abstraction changes where a program must enforce meaning.

Key terms: Degrees, domain types, architectural boundaries, Angle, raw data, architectural boundary

3.3.2 Scoping numeric types to specific ranges

The section narrows the type of elapsed days to match its non-negative meaning and shows how that type makes the Measurement model clearer.

Key terms: daysElapsed, int, non-negative integers, Measurement

3.3.3 Beware the String

The section shows why validating a sample ID as a String does not fully capture its meaning, then models it from its meaningful parts instead.

Key terms: String, Sample ID, curing medium, information leakage, serialized, SampleId, enum, sample number, NonNegativeInt

3.4 Is all this effort worth it?

The section argues that adding Java types is a tradeoff: too few leave dangerous gaps, while too many create needless complexity.

Key terms: types, type safety, sweet spot, boilerplate, safety, correctness, understandability

3.4.1 Do types only catch low hanging fruit?

The section uses a costly percentage mix-up to show why precise types provide a defense that tests and programmer care alone cannot.

Key terms: types, type checker, low hanging bugs, double, percent, type signature

3.5 What about performance?

The section argues that performance fears should not prevent precise type modeling without evidence from measurement.

Key terms: precise modeling, premature optimization, object allocations, JVM

3.6 Wrapping Up

The section connects domain meaning to constraints and invariants, then presents alignment between a type’s meaning and its data as a check on a model.

Key terms: meaning, constraints, invariants, type system, meaning of our type, meaning of our data

3.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Data, Identity, and Values Chapter 4: Domain Modeling with Algebraic Data Types →

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.