SOLID Principles

Gunar Gessner

Gunar Gessner

Jul 25, 2020

If we watch this talk by Kevlin Henney we'll see these are not principles but patterns. And these patterns boil down to:

S = Cohesion
O = Backwards compatibility
L = Backwards compatibility
I = Cohesion
D = Cohesion

# Cohesion

Cohesion is the hard-to-define property of designs where things that "belong together" stay together, and unrelated things stay apart. The canonical example is the utils directory, where the dictionary defines "utility" as the property of things that are useful. Since all our code should be useful, this label is harmful and we should avoid such directories/modules. The way we define the utils directory is by exclusion (everything that is not in any of the other folders), and instead we should always have positive definitions.

On a side note, Kevlin had an insight on how to define abstraction hierarchies. We should not define layers by their levels of nesting but by their specificity. We encouter such a problem when organizing directories of React components.

Chart of abstraction layers

# 2. Backwards compatibility

Backwards compatibility is the property of a system that evolves without breaking its consumers. It's thought as being about public interfaces, but it's actually about published interfaces — i.e. code you can't change because it lives on the client.

Sign up for the newsletter

Read other stuff