Morgan Conrad

SOLID Considered Harmful Part 2: .O...

Tags: all SOLID Programming     Feb 16 2018

SOLID principles, developed by Robert Martin a.k.a. "Uncle Bob", are amongst the most cited by programmers. I believe that many of them are misunderstood, misapplied, overrated, and all too often they much less important than other software engineering principles.

In this post, let's consider the "O", the Open Closed Principle.

O: Open/Closed principle

"software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification" Wikipedia source

However, as you read more in that Wikipedia article, there's more than one variation / interpretation of this principle. Trouble.
Let's read Uncle Bob's Version Skip ahead to bullet 2 of "Description": (emphasis mine)

"The source code of such a module is inviolate. No one is allowed to make source code changes to it."

All modification are to be made via, in effect, subclasses (or similar). The base class is assumed to be perfect.

One place where this may be applicable is "plugin" frameworks. Discussion Below

However, for other, "normal" code, Nobody writes any real code this way! Can you imagine if Java's ArrayList was actually this?:

  1. java.util.ArrayList
  2. java.util.ArrayListThatImplementsIterable extends java.util.ArrayList
  3. java.util.ArrayListThatImplementsForEach extends ...

Sorry, outside of plugin architectures, the Open-Closed principle is insane, and impractical. Those dogmatic few that strictly follow it encourage the construction of long, increasingly fragile class hierarchies. But we are supposed to prefer Composition over Inheritance, right?

Plugin Architectures

Plugin architectures, like Eclipse, Vim, Metalsmith, and Minecraft, are a good case, possibly the "apotheosis", of the OCP. I've written several Metalsmith plugins myself.

However, plugin architectures usually fall apart (or, at the least, struggle) at some point as complexity rises. They all eventually fall into a form of "DLL hell". a.k.a.

There are various security and outdatedness issues:

"The cold reality is that, unless you are careful about what you install, it is almost impossible to be confident that the installed plug-ins are not ill-intentioned"

Many businesses test your plugins - I'm most familiar with snyk.

These problems are not game-stoppers. But, even assuming they do get worked out, do these projects truly follow the Open Closed Principle? How stable has the base code been? Remember, officially, it is "inviolate".

You get the idea. Even projects that try to follow the OCP via plugins fail miserably at the "inviolate" stuff.

Conclusion

For non-plugin architectures, OCP is, well, insane.

I like plugin designs, they are often a great idea, and perfect for the OCP. However, don't expect the OCP to work as designed for them either. It really should be rewritten as "for plugin projects, try to put most of the modifications and extensions outside of the core".