Friday, June 8, 2012

Speculative Generality: Build something even if you don’t need it right now!

Today we will have a short discussion about Speculative generality, it yet another kind of code smell, not in case of actual code but in case of design.

Did you ever write code that was not needed right then , rather you wrote it in case you needed it in future?, If the answer is yes then you probably, in fact did introduced Speculative generality with or without knowing it.

Here is advice from the Experts

“Please don’t write anything in your code base unless your needed it!”

Lets be frank we don’t know what client want, infect the preceding statement is not true for all cases for instance when you follow strict water fall, but that is a whole different store as far as software development processes is concern. So in an ideal world I would say that on 80% client don’t know what they want so its obvious that developers can not predict, when we build something and show it to customers, customers will always will have some opinion on average case they want some modification.

So we have to build just what is necessary for reaching a short goal, after that along the way we would start fit whatever is necessary.

An Interesting Example could be, event though we don’t need any Abstract class for a view we spontaneously created an interface and an abstract class.

We can put down lots of stuff like that.

Rules

  1. If there is only one class that implements a interface probably we don’t need it.
  2. If there is only one sub class of a superclass, again probably we don’t need it.
  3. If only single function works okay for a method we don’t need any overload.
  4. And so on.

So from now on we would avoid any code that is not necessary.

Friday, June 1, 2012

Switch Statement: Same choice once again.

You have been there and done that thousands of time, but did you know that switch statement is a code smell? I am sure lot of people don’t know about this. I my self got surprise to know that switch statement is a code smell, when I attended my first agile process training.

Why switch statement is code smell?

Where ever there is a need for switch statement there is a good chance of polymorphism. But we must need to consider the context as well. Note that there is another form of switch statement,

(if … else if … else if .. )  is also another from of switch statement where polymorphism can be considered.

One thing we must consider that where converting a switch statement to a polymorphic solution often create a bunch of classes so make sure before creating classes that they worth doing and actually abstract some part of the code and take significant amount of code in a child class so that it does not become a lazy class.

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,

Or

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.

References

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.