Sunday, November 9, 2014

Open/Closed Principle

Open/Closed principle says that "Software entities (classes, modules, etc..) should be closed for modification, but open for extension." While this doesn't make much sense, it is actually quite simple: to change behavior of a system (or a class or a module) you should not change existing code (it is closed for modification), but you should add new code (it is open for extension).

This principle is greatly tied to correct abstractions being present in existing code. If right abstraction is present, realizing this abstraction and plugging this realization into software allows for safer development, because there is only one new piece of behavior to test.

The problem with OCP is those very abstractions. It is impossible to predict what abstractions you are going to need in the future. Coming up with correct abstractions is one of the most important jobs of software developer. Developer needs to use their whole experience and knowledge of both software and domain to create abstractions, that can be expanded upon later on, while they don't unnecessarily complicate the design.

I believe OCP is one of the most important principle, especially in environment, where requirements change and demands on software change often. This is especially true in agile environment.