Friday, May 25, 2012

Primitive Obsession:I will design all from scratch!

This is one of the primary tendency of every fresher developer or programmer, I my self is not out of the boundary, After starting the programming I was also kind of had the same tendency, I didn’t have much idea about the vast class library and functionality the c++, java or c# has to offer, rather I used the basic and primitive data structure and ideas and techniques that I learned while coding c.

This simple ignorance is some time called “Primitive Obsession”.

Here is what “codinghorror” has to say about “Primitive Obsession”,

“Don't use a gaggle of primitive data type variables as a poor man's substitute for a class. If your data type is sufficiently complex, write a class to represent it. ”

Code that has primitive obsession has the following phenomena 

  • use of primitives data type (like integers or strings) for solving complex problem.
  • use of low-level methods to perform operation on data.

Eventually we loose a higher level of abstraction.

One simple example in c# could be build a list of objects, we could build a class to keep a specific type of data and then expose different methods and properties to support the class,


We can use a List<T> to keep the object, where what ever we need from a list is there. I think you got the idea.


Friday, May 18, 2012

Oddball Solution: some one does the same thing in different way.

In this section we would take a look at oddball solution code smell. So what is a odd ball solution, if one problem is solved in one way throughout a system and the same problem is solved in another way in the same system in some cases, one of the solutions is oddball solution.

This particular smell is also known as Inconsistent Solution.

This happens specially in case of algorithms, different algorithm or different version of the same algorithm is been used several places which creates inconsistency and duplicate code.

How odd ball solution get in to the system?

There are two obvious reason for this,

  • Ignorance of how a solution is implemented elsewhere in a system.
  • Not spending enough time refactoring code to use a consistent solution.


How to get rid of odd ball solution?

The Simple process to get rid of oddball solution is to use extract method and use same method and use same algorithm all over the system.

While picking the right solutions for a problem we must consider which solution is been used majority of time then decide if this solution is better than the one is been used minority times. Compare and then keep the best one and eliminate the other one.

Thursday, May 10, 2012

Lazy Class : Does not have any purpose in the Matrix.

There is a very nice saying in the movie matrix, every thing is the matrix has purpose, other wise its been deleted by the agents. Believe we also create purpose less classes in our projects. as we all can guess the purpose of this discussion is to take a small note on “Lazy Class”

Here is what Wikipedia has to say about Lazy Class.

“A class that does too little”

This particular code smell is also known as freeloader. A Lazy class does not start from the beginning, And had definite purpose, but after some move method and refactor the class gets so smaller in size and the minor functionality that it has can be offered by another more meaning full class, or perhaps it already offered by some one other class already. So it end up with doing nothing at all.

So we don’t feel pity about it and can delete the lazy class.

How to eliminate Lazy Class?

Use “Collapse Hierarchy” or “Inline Class” to eliminate this code smell.

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.