Cohesion

Many months ago, I've watched a talk by Kent Beck, where he introduced coupling and cohesion.

Until this video, I was focused mainly on coupling (during reviews and design), the cohesion of my code, was mostly the result of grouping "related"-code and my OCD, assuming low coupling involved high cohesion.

Before going one, let's define each concept:

Coupling is the degree of interdependence between software modules, it's multidimensional.

From higher to lower coupling we have, on functions/procedures/methods:

  • Content: back in the days, there were goto, allowing to jump anywhere in another procedure
  • Common: access to the same global variables, without control
  • External: sharing a protocol to an external system, without sharing the implementation
  • Control: when a module drives another module flow
  • Stamp/data-structured: when two modules share a different subset of a data-structure
  • Data: when two modules only share the data-structure, completely

We also have other dimensions:

  • Temporal: things should be run in a specific order
  • Dynamic: modules are working because they are specifically running together
  • Semantic: modules are semantically similar
  • Logical: modules are bound conceptually

See connascence to analyze coupling.

On another hand, we have cohesion, which is the strength of the bounds between the element of a module (to some extent, a highly cohesive module, have components highly coupled together).

There are multiple levels:

  • Coincidental: arbitrary grouping
  • Logical: grouped because they belong to the same category (e.g. all queries together, all forms together, etc.)
  • Procedural: things are put together because they should be called in a given order
  • Communicational/informational: things are put together because they operate on the same data
  • Sequential: grouped because a part produce some data consumed by the next one
  • Functional: things are put together because they are contributing to achieve a well-defined process
  • Perfect: grouped because they are dedicated to a well-defined process and cannot be reduced

As mentioned earlier, I had a first "ahah moment", at the time of watching it, focusing more on cohesion, trying to balance it with coupling.

I had a clear motivation regarding coupling: reduce the pain of editing many places for a simple change.

But I lacked one for cohesion.

That's my second "ahah moment": clarity, reducing the noise making a change.

If you have a change to make, the cognitive load and the regression risk decrease as the targeted code is isolated and has not unrelated parts.