The O in Solid

In a previous post we considered the practical value of the Single Responsibility Principle (SRP). This is the second post in this series where we take a deeper look at each of the SOLID principles.

Robert C. Martin (a.k.a. “uncle Bob”) refers to the O in Solid as the heart of Object Oriented (OO) design. He goes so far as to say that this principle improves reusability and maintainability more than any other OO principle. You most likely already know that the O in SOLID belongs to the Open/Closed Principle (OCP).

We often hear about SRP or DRY (don’t repeat yourself) but seemingly less often about OCP. It turns out that this principle lays the foundation for many of the OO best practices. In this post we will talk about OCP and find out why uncle Bob is such an advocate of OCP.

What

When a module is open for extension it means we can make the module behave in new or different ways. When a module is closed for modification it means that we are no longer allowed to make changes to the source code of that module.

“Software entities (classes, modules, functions, ...) should be open for extension but closed for modification” - Bertrand Meyer

OCP urges us to write code that can address changing requirement without needing to be modified directly. In other words we address changing requirements by writing new code, not by changing existing code.

Why

TLDR: OCP allows us to:

  • Never worry about distant or unrelated side effect
  • Never be bound to a specific implementation or feature
  • Have tests that always pass, since we don't change existing code

When we add features by making many changes throughout the code base we risk introducing unanticipated side effects. Nothing knocks a developer’s confidence quite as much as bugs showing up in strange places.

“Imagine a world where you can add new behaviour without editing existing code.” - Sandi Metz

When we fail to adhere to the guidelines that OCP provide our code becomes increasingly rigid. Sandy Metz introduces a category of issues that she refers to as change preventers.

Change preventers include:

  • Divergent changes (A class changes for completely distinct reasons)
  • Shotgun surgery (Many changes to a code base to add a new feature)
  • Parallel inheritance hierarchies (Adding one subclass require a subclass to be added elsewhere)

Subscribing to the OCP best practices goes a long way in preventing or eliminating change preventers in our code base.

How

Most of the applications that we work on cannot be 100% closed. As profesional developers we aim to close our modules against the most likely changes. We should be careful not to guess what the future will bring. Instead, it is wise to wait for future changes before deciding which parts of our application to close off.

Interestingly we can address the previously mentioned change preventers by applying OCP refactoring techniques.

  • Divergent changes are addressed by extracting a new class
  • Shotgun surgery require that we move a field, move a method or inline a class
  • Parallel inheritance hierarchies can be addressed by moving a field or method.

The list of techniques above is by no means an exhaustive list of OCP related refactors. There are many OO best practices that have their origin in OCP. Below, I compiled a list of ideas or techniques that will improve our adherence to OCP.

  • Appropriately applying design patterns such as the decorator or template patterns
  • Make sure to “Ask, don‘t tell”
  • Avoid type checking conditionals
  • Prefer composition over inheritance
  • Dependency injection for objects that fullfill defined roles
  • Cautiously apply inheritance and polymorphism
  • Extracting classes or objects when appropriate
  • Tiny public interfaces
  • Make all member variables private
  • Avoid global variables at all times
  • Implement a plugin architecture

Conclusion

OCP is a vital part of SOLID software applications. OCP urges us to apply abstractions where changes are most likely. We can simplify our code and boost our developer confidence by studying and applying OCP inspired techniques.

“You should be able to extend the behavior of a system without having to modify that system.” - Robert C. Martin

Is your application riddled with change preventers? OmbuLabs is a lean software boutique where we adhere to the SOLID principles. We are experts at closing the right modules at the right time so applications are flexible and easy to maintain.

Further reading