Thursday, May 3, 2012

Large Class : The making of code smell history.

In this particular short discussion we are going to take a good look at “Large Class” code smell, and some obvious reason for creating one in any project. And then we would discuss some easy way to eliminate the large class code smells.

“Large class: a class that has grown too large”

This is what Wikipedia has to say about large class. And the statement is so true, as no class is a large class at the beginning, but as time passes some how it started to grow big and big and eventually end up with a unmanageable situation.

Who a large class?

  1. A class do too many work
  2. A class has too many methods and members both private and public and they used for versatile purpose.

In a large class we tend to add more functionally, when we are unsure where to put them, in this way more and more unsure stuff gets added and eventually got spoiled. Just like large method large class is very hard to manage and understand, a class has too many responsibility and do too many work all by it self, where as it could be separated in few smaller classes or perhaps could be refactored in such a way so that they do violate “Single responsibility principle”.

How to eliminate large class?

First of all we have to separate the responsibility and identify the set which belong to one group, and create another class to which has one single responsibility and using “move method” refactor technique move the related field and method to another class.

Follow the above process until all the responsibility is been distributed to other class.

No comments:

Post a Comment