What is Refactoring

October 27, 2008

To express in very simple way, Refactoring is rewriting the existing code. But why should you? Hmm. we need to go for something better. Lets try again. Refactoring is changing the existing code to restructure the design which will be easier to understand or decrease total amount of code or maximize the use of same piece of code or cutting of deprecated methods by using new but do not change the observable behavior of the code.

Most of the time, its not like you start refactoring as a task. There are very few scenario that leads refactoring as independent task. Most of the time people do it while developing a feature. Better to say Feature Mode and Factoring Mode at the same time. Think of a scenario. Suppose You have a method that takes no of seconds as input and print number of minits and hours by that amount of seconds by simply divide by 60 and again by 60.

void printHourMin(long seconds) {
long min = seconds/60;
long hour = min/60;
System.out.println(min);
System.out.println(hour);
}

Now you need to print no of Hours only in another feature. What you would do? write a new method to do that feature? I do believe no. You may like to Re-factor the existing code.

void printHourMin(long seconds) {
long min = seconds/60;
System.out.println(min);
printHour(min)
}

void printHour(long min) {
long hour = min/60;
System.out.println(hour);
}

This two method can serve the perpose of two different requirement but minimize the number of code as well as readability. Well this could be example of very simple refactoring. But the field is too massive, it is consists of too many techniques and ideas.

By: Md. Shahjalal


Feature Mode and Refactoring Mode at same time ?

October 23, 2008

Feature mode and Refactoring mode. Two frequently interchangeable mode of a Programmer. Think like eraser and pencil. A writer must know which one to use and when while correcting a essay. One of the most false idea of typical programmers is you will develop a feature and re-factor a feature in separate time. While a good programming approach combine both, but you must know which hat you have to put on. Feature hat or Refactoring hat.

While in coding, most of the time we take help from the existing code. We link our code with methods or classes of existing code. There are some cases that shows its better to refactoring an existing code than writing a new piece of code. A method of class can be reused by simple refactoring. In this point a programmer must change his mode to refactoring mode. So, he can not only rely with feature mode though coding for a feature.

It not only reduce you time and code but increase the re usability of same piece of code. Well before to end this topic, a very important thing. You must test your code after refactoring as well as think widely about the side effect of the whole change. Refactoring must be done smartly otherwise it will make system like a nightmare that you never think about in your worst time.

By: Md. Shahjalal


Follow

Get every new post delivered to your Inbox.