Introduction

What is the open/closed principle should be the first
question we ask. It states that an entity in a system should be open for
extension and closed for modification. 
In terms of C# what it means to us is that a class should only be
modified if a bug is found on it. The rest of the time when we are doing new
functionality we should create a new class.

For functionality that is related it implies that we inherit
from the original object.  In terms of
this post the idea is to detect the conditions in which the code in question is
either breaking this principle or it will affect people trying to follow it on
the future.



Which bring us to the followings which elements do we need to look at to
determine how will affect the people in the future?

How does Open/Closed affect test efforts?

When a project does not follow this principle during
execution it ends up impacting the test surface and the amount of regression
needed.

Let’s consider the following application:

It’s a web service it takes a small piece of xml with two values
and operation that will be done to it  brings back a result.

On the first iteration they created it with the
functionality for adding since it was all that was needed.

It looked something similar to this:

 

Public class MathOperations()

{

   Public long
  GetOperationResult(string xml)

   {

           Int result=;

          
  This.Logger.StartOperationLog(parser.operand1, parser.operand2,
  parser.symbol);

           Var parser = new
  OperationParser(xml);

           If(parser.symbol ==
  ”+”)

             {

                    result =
  parser.operand1 + parser.operand2;

              }

          
  This.Logger.EndOperationLog(parser.operand1, parser.operand2,
  parser.symbol);

           Return result

   }

}

 

In the implementation above if you think about adding the other 3 operations
for your second sprint would require to ensure that everything works once again
since changes in the main file might affect the processing of the add
operation.  How can we make this class
apply the principle and make it only impact the operation execution on our test?

 

One possible way to do it’s to write the original class
using a factory therefore adding next operations means that only a new class
gets added into this project and only the new class needs to be tested.

It would look something similar to below.

Public class MathOperations()

{

   Public long
  GetOperationResult(string xml)

   {

           Int result=;

          
  This.Logger.StartOperationLog(parser.operand1, parser.operand2,
  parser.symbol);

           Var parser = new
  OperationParser(xml);

 

            result =
  OperationFactory.GetInstance(parser).DoOperation();

 

          
  This.Logger.EndOperationLog(parser.operand1, parser.operand2,
  parser.symbol);

           Return result

   }

}

 

It means that after sprint one all the test you did for mathoperations would
still apply and not really need to be executed again.

On the next post I’m going to describe how to find out if
your team actually adheres to this principles and what are things that can tell
you.