What is refactoring ?
Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure
Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior.
I use most of the refactoring methods, described in the reference book.
This way the standard of code is reaching higher level as in my company
Redigon Software Solutions
Why should we refactoring?
In essence when you refactor you are improving the design of the code after it has been written,
refactoring Makes Software Easier to Understand, helps you find bugs and makes you program faster
What will heppend when future developer works on not refactored code?
Someone will try to read your code in a few months' time to make some changes. The programmer will take a week to make a change that would have taken only an hour if she had understood your code.
When to Refactor ?
Refactor when you add Function
Refactor when you need to fix a bug
Refactor as you do code review
When you should not refactor?
There are times when the existing code is such a mess that although you could refactor it, it would be easier to start from the beginning.
The other time you should avoid refactoring is when you are close to a deadline. At that point the productivity gain from refactoring would appear after the deadline and thus be too late.
What is Bad Smell in Code?
Duplicated code
Long Method
Large Class
Long Parameter List
Refactoring Methods
Extract Method
Before refactoring
void printOwing(double amount)
{
printBanner();
//print details
WriteLine("name:" + _name);
WriteLine("amount" + amount);
}
After Refactoring
void printOwing(double amount)
{
printBanner();
printDetails(amount);
}
void printDetails(double amount)
{
WriteLine("name:" + _name);
WriteLine("amount" + amount);
}
Method: Split Temporary Variable
You have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable. Make a separate temporary variable for each assignment.
Before Refactoring
double temp = 2 * (_height + _width);
WriteLine(temp);
temp = _height * _width;
WriteLine(temp);
After Refactoring
double perimeter = 2 * (_height + _width);
WriteLine(perimeter);
double area = _height * _width;
WriteLine(area);
Move Method
A method is, or will be, using or used by more features of another class than the class on which it is defined.
Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.

The Rule of Three(Don Roberts)
The first time you do something, you just do it
The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway.
The third time you do something similar, you refactor.
Tip: Three strikes and you refactor
Text and code samples are taken from the book by Martin Fowler. I recommend to everybody to read this fantastic book.
Reference
Improving the Design of Existing Code (Addison-Wesley) by Martin Fowler.

Great Presentation
ReplyDeleteNice work man thanks i found solution .
ReplyDelete