Posted on April 11, 2016
by Doug Klugh

Decreasing time to market is often why many teams adopt Agile.  But realizing this value requires discipline in development processes, as well as in development techniques.  In order to release small, incremental pieces of functionality often, your software must be easily extensible.  Following the Open/Closed Principle (OCP) is one of the best ways to ensure that you can easily and quickly extend your software with new functionality while maximizing the value of Agile. 

What is the Open/Closed Principle?

OCP was defined in 1988 by Bertrand Meyer in his book Object Oriented Software Construction.  The principle states that “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification1.  Using this principle, a developer can extend the behavior of an entity without modifying its source code.  This relies on using inheritance and polymorphism by extending base classes and/or interfaces.

By applying OCP, software is made to be far more extensible than it would be otherwise.  Adding new functionality to software that was built using OCP will be relatively quick and easy.  And this is exactly what you need when following Agile development practices.  In an attempt to deliver new functionality within a short iteration, the last thing you need is to deal with code that is tightly coupled.  Not only does it make it difficult to extend, but it makes it much more likely to introduce new bugs.

SOLID Principles

OCP is part of the SOLID principles of object-oriented programming and design, as coined by Uncle Bob Martin in the early 2000s.  The Open/Closed Principle is often used in conjunction with the other 4 SOLID principles:  Single Responsibility, Liskov Substitution, Interface Segregation, and Dependency Inversion.  Together these principles help to promote highly cohesive, loosely coupled software that is easy to extend and easy to maintain.

Conclusion

Agile is all about being…  well, agile.  And to be agile, you need to be quick and responsive.  If your code is tightly coupled and difficult to extend, your team will spend a lot more time implementing features than it needs.  Following disciplined software engineering principles takes more time than not following them (in the short term).  But over a slightly longer period of time, your team will be spend much more time extending software and fixing bugs than if it had done things the right way; the “right way” being applying proven engineering principles, such the Open/Closed Principle.  Being successful at Agile requires much more than following an Agile process.  Your code must also be agile.  And one of the best ways to make your code agile is through OCP.

1Meyer, Bertrand (1988). Object-Oriented Software Construction. Prentice Hall. ISBN 0-13-629049-3.
Tags:
Agile SOLID Uncle Bob

Doug Klugh

Doug is an experienced software development leader, engineer, and craftsman having delivered consumer and enterprise firmware/software solutions servicing more than one billion users through 20+ years of leadership.

Related Items


assistant Development Tip

Open/Closed Principle

Write code that is open for extension but closed for modification.  Start by delivering a minimum viable product, then as the change requests begin pouring in, you will see the type of changes that are likely to come.  You can then begin applying the OCP to enable your code to be easily extended (without modification) for those types of changes.  This will help avoid over-engineering your initial solution (YAGNI).

Learn More
subject Article

Agile Is More Than Process

There is more to Agile than estimating stories, collaborating with customers, and showing working software.  Agile is also about technical excellence.  And this is where many Agile teams drop the ball.  All too often, teams focus too much on process and not enough on technical practices.  If the effort, complexity, and risk is too great for your team to extend and maintain their software, they will struggle to deliver functionality to their customers at the end of each iteration.  They will struggle to deliver working software as promised.

Read More
subject Article

Enhancing Software Testability

Keeping your customers happy depends a lot on your team’s ability to deliver (and sustain) a high-quality product.  And to ensure high quality, you must effectively validate your software artifacts against the functional (and non-functional) requirements of your system.  In many of my classes, I often talk about testability being an essential quality of good software.  But what exactly do we mean by “testability”?

Read More
;