Vlad's Roam Garden

Powered by 🌱Roam Garden

Keep knowledge local

An object should not be aware of global context it is in. As it makes it harder to trace what can impact code, harder to test it in isolation and harder to reuse.

Some examples of global context can include:

creating an object you depend on internally (vs having it injected)

using global variables or even constants

A main way to avoid reliance on the global context is ensuring that all dependencies are passed in to the object through clear pathways (i.e. constructor or method parameter)

Another aspect of keeping the knowledge local is ensuring that the outside world does not know about the internals of the object - that is maintaining encapsulation

Some of the test smells we’ve identified, such as needing “magic” to create mocks, are to do with knowledge leaking between components. If we can keep knowledge local to an object (either internal or passed in), then its implementation is independent of its context; we can safely move it wherever we like. Do this consistently and your application, built out of pluggable components, will be easy to change.