Bodhisattva in Training

September 24, 2006 OOP, Unit Testing

Open Closed Principle and Testability

And again I will start of with an exerpt from c2.com:

Open Closed Principle


Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. (http://www.objectmentor.com/publications/ocp.pdf)

BertrandMeyer first coined it in the first edition of ObjectOrientedSoftwareConstruction as follows:

A class should be open for extension, but closed for modification.

In other words, (in an ideal world…) you should never need to change existing code or classes: All new functionality can be added by adding new subclasses and overriding methods, or by reusing existing code through delegation.

This prevents you from introducing new bugs in existing code. If you never change it, you can’t break it.

In the previous post in this series YAGNI and Testability were disscussed; that discussion will not be repeated here. I will continue the on the topic of interaction based testing(IBT). OPC plays a key role in allowing IBT. It partners with the Dependency Inversion Principle in creating a design where IBT is an option. OPC’s role is simply in allowing the substition for a test double. Having the option to use a test double without the need to refactor, extract interface, increases testability. There really isn’t any more to it than that.

Leave a comment