chapter four

4 Domain Modeling with Algebraic Data Types

 

This chapter covers

  • The walk through the messy design process
  • Modeling with Algebraic Data Types
  • Making Illegal states impossible to represent

We’re going to walk through designing a small feature in all of its chaotic glory. Data-oriented design is like all design processes: iterative and messy. We’ll make plenty of missteps, hit lots of dead ends, and have to do more than a few total resets. We’ll walk through all those messy bits here because we’d do them in real life, too (at least when I’m doing it). However, beyond that, we walk through all the “wrong” parts so that we can train ourselves to become sensitive to the friction that “wrong” causes in the code. Once we can identify problems, fixing them becomes much easier.

What might be surprising is that our design process will be focused entirely on the data in our domain and what it means. Data Oriented design is largely in the spirit of Fred Brooks’ classic observation from Mythical Man Month: “representation is the essence of programming.” If we get the representation of our data right, everything else tends to fall into place. As such, we won’t worry about behaviors, or efficiency, design patterns, or any other operational focused concerns for now.

We're also going to ignore that our job as software engineers is to build systems.

4.1 Starting from Requirements

4.1.1 The Humble Checklist

4.1.2 Firming up loose requirements

4.1.3 Translating into a model

4.1.4 New Requirements: Keeping Track of Who Did What

4.1.5 Increasing complexity: another requirements change

4.1.6 Beware of refactoring the code, rather than its meaning

4.2 Records are tools for modelling “AND”

4.2.1 Designing consciously with AND

4.3 Modeling “one of” relationships with OR

4.3.1 Interfaces are data types

4.4 Some data models should be closed to extension

4.5 Representing Sealed Families of Data

4.6 Algebraic Data Types

4.6.1 Product Types

4.6.2 Sum Types

4.6.3 What’s the point of knowing this stuff?

4.6.4 Refactoring domain models algebraically

4.7 What to do if you you're on older versions of Java

4.8 Wrapping Up

4.9 Summary