74 Chapter 2
Operations and composition constrain data
In the last few pages, we’ve been looking at the decision
of whether to nest data or to reference data. We made that
decision by noting how many difcult questions we face
when we copy the order or when we change the name of
the customer. In that case, we’re applying the operation
lens to guide our thinking. We thought through the conse-
quences of the copy order and change customer name opera-
tions. Knowledge from the operations constrained our
data.
The operation lens asks us to consider the domain op-
erations that will have to apply to our data. We can do this
a little bit while we’re modeling our data. But we will see
in Chapter 3 that it is better to begin with the operations
rst, before we encode our data, because each operation
constrains the decision space. The more constraints we
have, the easier decisions become. Each constraint may
eliminate choices, making our decisions easier.
Further, in addition to considering the operations
alone, we considered them in composition. We asked,
“What happens when we change the name after making
a copy?” With that question, we are applying the compo-
sition lens, which we’ll see in Chapter 4. Operations are
rarely applied alone. Instead, we apply a set of operations,
often in sequence, to achieve a result. We want those
sets of operations to guarantee properties. Composition
guarantees constrain our choices further.
It is very hard to make decisions about data structures
without some thought about the operations that will use
it. There are simply too many choices to rely on t alone.
That is why we’re talking about it now, before the chap-
ters on the operation and composition lenses.
Data encoding is on the implementation side, while
operations and composition are on the specication side.
We generally want to be modeling in specications with-
out regard to implementation. Although that means we
should work within the operation and composition lens-
es rst, I have found that people don’t feel comfortable
thinking so abstractly before they have learned the con-
crete skills of modeling and encoding data.
So let’s keep trucking along with the data lens. We’ll
get to operations and their composition in due time.
Operations we
considered
• copy order
• change customer name
Compositions we
considered
• copy order then
• change customer name