Home  /  Questions  /  Question



256   99.6
Sep 14, 2010


How to identify appropriate Design Patterns to resolve code smells

Hi,

I am involved with an application where there are a lot of coding issues (i.e code smells), such as, many if else conditions, hard coded data passing from one layer to another, etc..

I want to improve the code quality for the application. Can any one please tell me how I can identify that a particular Design Pattern is appropriate to resolve (or refactor) certain pieces of code?

Please let me know if the question is not clear.

Thanks.



50   50
Sep 14, 2010
HI,

There are multiple ways in which u can handle the situation.

1. Refactor your code:
 Review code and see where there is repetetive code. Create small methods and pass paramaters for small differences in repetive code.
2. Once you are finished and convinced that the code is refactored into private methods, u wud find that there needs to be refactoring across multiple classes. E.g similar code in multiple classes with small differences relvant to that specific class.
For this, create a separate class and move methods there. You would realize that either your classes containing common code need to inherit from this new class or need to have a refernce for it. Well, doing this is using design patterns.
3.Now in case if else statments consit of creating mutiple objects:
if(Case I)
  A a = new A();
else
 B b = new B();

such a case calls for creating common class and inherting A and B from same. use factory to create object at runtime.

There are just few small samples, this may help you.

Regards,
Pankaj
 2 comments
 
Hi Pankaj, Thanks for your reply. I will surely go through the code and refactor it. Hope I can improve my skills in this :) --- Harshad Riswadkar  Sep 15, 2010
 
Hi Harshad, I make use of the SIMIAN and Source-Monitor tools for finding duplicate lines of code and to find complexity and depth. URLs are as follows. If you can provide some code snippets or scenario it would be useful to suggest the design or pattern. http://www.redhillconsulting.com.au/products/simian/ http://www.campwoodsw.com/sourcemonitor.html -Saurabh_Saxena --- Saurabh Saxena  Sep 15, 2010