Book Deep Read · Fundamentals of Software Architecture · Chapter 3

Modularity: Cohesion, Coupling, and Connascence

Fundamentals of Software Architecture · Ch 3 · Mark Richards & Neal Ford · 2020

中文 →

What is this chapter about?

Everyone says code should be "modular" — split into pieces. But ask "is this a good split?" and most people can only answer from gut feeling. Chapter 3 tries to turn that feeling into something you can count and compare: do the things inside one chunk of code really belong together? How tangled are the chunks with each other? If you change one spot, how many other spots must change with it?

An analogy: packing to move house

When you move, you put the dishes in one box, the books in another, the clothes in a third. Everything in a box is related — the box "hangs together." And when you unpack, you don't have to hunt all over the house — the boxes "don't tangle." The worst box is the junk box: a remote, a sock and a screwdriver, together only because they happened to share a drawer. Code has junk boxes too, and the book ranks them dead last.

Why does good structure decay on its own?

A room you never tidy gets messier, and code is the same. Today, in a rush, someone reaches into the guts of the neighboring module; tomorrow, someone does it again. Over time the lines between modules knot into a tangle. The authors put it bluntly: keeping things modular takes constant effort; it does not maintain itself.

Three rulers

The first ruler measures how tightly a module hangs together: does everything inside revolve around one job? One clue is whether the pieces share the same data. If half the functions only touch one thing and the other half only touch another, it is really two modules squeezed into one.

The second measures how tangled it is with others: how many modules lean on it, and how many it leans on. Think of a coworker whom dozens of colleagues depend on — when they take a day off, the whole floor stalls. Or the opposite: a role nobody depends on, but which relies on ten other people, so it has to change every time any of them does.

The third and finest is called "born together": if you change A, you must also change B, or the system goes wrong. Say two forms agree that "the first box is the seat number, the second is the name." Fill them in backwards and nothing complains — things just quietly break. The more hidden such an agreement and the farther apart the two sides, the more dangerous it is. Hence the rule: the farther apart two pieces are, the simpler and more obvious their agreement must be; keep the complicated entanglements inside one module.

What this buys you

With these three rulers, "is this a good split?" stops being an argument and becomes a measurement. The "powder keg" everyone depends on and nobody dares touch, the empty shell no one uses — both stand out, and a tool can check for them every day. One honest cost: the rulers see structure, not business meaning — whether two things truly belong together is still a call for someone who understands the domain.

One line to remember

A good module = tight inside, loose outside, and the farther apart, the simpler the agreement — and that tidiness needs daily upkeep.

Want the seven levels of cohesion, instability and the "main sequence" chart, and the nine forms of connascence? → Switch to Deep