Encapsulate What Varies

Encapsulate What Varies, or ‘Encapsulate What Changes’ is the technique of reducing the impact of frequently changing code by encapsulating it. The encapsulated code can then change independently to code that relies on it.

Whilst this principle relates specifically to code that frequently changes there are many benefits to encapsulating code, unrelated to how often it changes. Such as:

  • Encapsulated code can be reasoned about in isolation from the code
  • Preventing tight coupling to the implementation of the encapsulated code e.g. if you encapsulate the interaction of a downstream system, then replacing that downstream system becomes easier
  • Encapsulating constants can indicate to other developers that they’re related and provides control over if/how they’re changed