Last post Sep 22, 2010 01:42 PM by uid413084
Sep 22, 2010 05:07 AM|riswadkarharshad|LINK
I am working on an application which contains code in lot of places where certain objects are created based on certain condition. The code is something like the following:
if(affectedObject == "Customer")
Customer c = new Customer();
else if(affectedObject == "Salesman")
SalesMan s = new SalesMan();
// Do some processing.
// Do another processing.
I have gone through design patterns once but I am not able to figure out how I can use a pattern to replace my code.
Can any one please help me with this? Any practical code sample would be of great help (code without refactoring and refactored code) so that I can understand it better.
Thanks in advance.
Sep 22, 2010 06:31 AM|uid413084|LINK
The code you are using is a violation of the Open-Closed Principle (OCP):
This means that when everytime there is a new object you need to modify your code especially for this new object. The Open-Closed Principle states that your code should be Open for extension but Closed for modification; in your case it is not.
To refactor your code there are different ways, the most often used pattern to deal with this is Dependency Injection or DI:
It is almost synonomous with the principle of Inversion of Control or IoC:
The general idea behind IoC is giving away the responsability of creating objects during runtime to an IoC container.
All this principles and patterns however will not all resolve your problem, because you are still using some magic strings in your code such as "Customer" and "SalesMan", ... these are coming from somewhere. If you can refactor them out and from your code
and use Polymorphism, ...
If you have any further questions please ask.
Sep 22, 2010 07:07 AM|uid413084|LINK
If you are looking for a short term solution you can use one of overloads of Activator.CreateInstance() and pass the affectedObject:
Sep 22, 2010 09:02 AM|riswadkarharshad|LINK
Thanks a lot for your message and links that you have provided. I am working on the application which was created around 2 years back and at that time probably because of the timeline constraints, the code was written in a haphazard manner.
So, I am really planning to refactor the code. Although, I am bit aware of design patterns, I am struggling a bit to actually implement it in any real-life application. So, needed help in it.
Also, could you please guide me on how and where to start refactoring the code?
Sep 22, 2010 01:42 PM|uid413084|LINK
Refactoring legacy applications is always going to be difficult. Refactoring is about making small changes to the code without changing functionality. The problem is how can you guarantee that you don't change existing functionality or that you don't break
the code? Hopefully the code is covered by unit tests and integration tests. If it is not, refactoring existing code can become very problematic and very costly.
There are tools like Pex and Moles from Microsoft that can generate Tests for an existing code base. If you want to write unit tests manually for a legacy application than the problem is that you need to mock all dependencies. The program is that the existing
program was propably not designed for testability and therefore the dependencies are very dificult to mock.
I would only recommend to refactor code in those parts of the codebase where you are actively working on so basically in the parts of the codebase that you are actively touching and leave the rest of the codebase untouched. So if your customer asks you to
implement a specific feature in the existing codebase or to fix a bug this would be an ideal opportunity to add extra time for some refactoring but resist the temptation to refactor other parts of the codebase.
Here is some recommend reading:
Working Effectively with Legacy Code:
Refactoring: Improving the Design of Existing Code:
Refactoring to Patterns:
Beside the books there are also several useful tools, products you can take a look at:
Microsoft Pex and Moles:
Team Foundation Server:
The advantage of TypeMock over any other mocking frameworks is that it can mock anything. It is a commercial framework but it is perfect for code that was not designed for testability like most legacy applications.