DRY

DRY: Don't Repeat Yourself.

“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” - Pragmatic Programmer (Book)

The simple way to see DRY is in terms of duplicated code. See DRY (Don't Repeat Yourself) In Terms of Code.

However, a more important adherence to DRY is in terms of knowledge duplication. Code can look similar even duplicated and that can be OK if code represents different knowledge. To figure out whether knowledge is duplicated a great question to ask: Is code that is duplicated going to change together or separately?. If there is a good change of it changing separately it represents different knowledge. If it's going to change together it represents the same knowledge and MUST be deduplicated. (More on this at Not All Code Duplication Is Knowledge Duplication))

Don't repeat Knowledge.

Is code that is duplicated going to change together or separately?
Separately -> Duplicate.
Together -> Extract.

When we package knowledge into a single place we allow code evolution to happen smoothly. If we don't simple tasks turn into tentacled beasts.

  • Code Duplication: Copy-pasting the same implementation in multiple places.
  • Knowledge Duplication/Fragmentation: Scattering decision logic across several classes or modules.

The peskier violation of DRY is harder to spot, and is NOT about code duplication but about unpackaged/uncentralized responsibility. When a change in one class requires non-obvious, corresponding logic changes in other parts of the system, changes the compiler cannot enforce.

In this manner DRY is highly tied to SRP (single-responsibility-principle), in the way that not only a class should have a single reason to change. But that the class should be GREEDY about that reason for change that it claimed, class must NOT share the claimed reason for change with anyone else. It MUST aim to be the ONLY owner of that responsibility for change.

Classes MUST be GREEDY with Responsibility that they claimed.

Classes MUST Relinquish Responsibility claimed by others.

Responsibility = Reason to Change.

img

By being GREEDY with claimed Responsibility (Reason to Change), dilligintly guarding it from being fragmented into any other place. While relinquishing responsibility claimed by others.

By NOT sharing responsibilty -> Classes gain FREEDOM to change.

img


Children
  1. DRY (Don't Repeat Yourself) In Terms of Code