Code Refactoring Part 1
Bài đăng này đã không được cập nhật trong 6 năm
What is Code Refactoring?
Code Refactoring is the process of restricting existing code. Refactoring increase non-functional attributes of software application. It is a process of taking existing code and improves it while it makes code more readable, understandable, and clean.
"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." – Martin Fowler
Before refactoring we need to identify “Code Smells” in code. Refactoring will be based on “Code Smells”. Refactoring also removes “Code Smells” from your project, this is done to get certain benefits and these benefits may not be consumable immediately but over the long term. Refactoring can be different types like
- Code Refactoring
- Database Refactoring
- User Interface Refactoring
Now I will discuss about code refactoring.
Why Code Refactoring?
- To improve the design of Software application
- Easy to extend new requirements in the application.
- To Make Software easy to understand.
- To Make program run faster.
- To Make Re-Usable Components.
- To find Bug and Easy to Fix.
- To Make application component are loosely couple
Output after Refactoring
- Makes code more readable.
- Cleanup code and makes it tidier.
- Removes redundant, unused code and comments.
- Improves performance.
- Makes some things more generic.
- Keeps code DRY ( Don’t Repeat Yourself)
- Combines and dispose “Like” or “Similar” code.
- Splitting out long functions into more manageable bite.
- Create re-usable code.
- Better class and function cohesion.
Agile practices come along with refactoring and code smells for identifying refactoring needs. In computer programming, code smell is any symptom in the source code of a program that possibly indicates a deeper problem. Code smells are usually not bugs—they are not technically incorrect and do not currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future.
Common Code Smells and their refactoring
- Long Method
- Lazy Class
- Dead Code
- Refused Bequest
- Inappropriate naming
- Comments
- Duplicated code
- Primitive obsession
- Large class
- God class
- Middle man
- Data clumps
- Data class
- Long parameter list
- Switch statements
- Speculative generality
- Oddball solution
- Feature envy
- Black sheep
- Contrived complexity
- Divergent change
- Shotgun Surgery
Long Method Treatments
- Reduce the method body, use extract method.
- Replace temp with query
- Introduce parameter object
- Preserve Whole Object
- Replace Method with Method Object
- Decompose Conditional
Extract Method Example
Bad Code
void PrintOwning(double amount){
PrintBanner();
/ / print details
System.Console.Out.WriteLine(“name: “+ name);
System.Console.Out.WriteLine(“amount: “+ amount);
}
After Refactoring:
void PrintOwning(double amount){
PrintBanner();
PrintDetails(amount);
}
void PrintDetails(double amount){
System.Console.Out.WriteLine(“name: “+ name);
System.Console.Out.WriteLine(“amount: “+ amount);
}
Replace Temp with Query Example
Bad Code
double basePrice = _quanity * _itemPrice;
if(basePrice > 1000) {
return basePrice * 0.95;
} else {
return basePrice * 0.98;
}
After Refactoring:
if(getBasePrice() > 1000) {
return getBasePrice() * 0.95;
} else {
return getBasePrice() * 0.98;
}
double getBasePrice() {
return _quanitiy * _itemPrice;
}
Introduce Parameter Object Example
Replace Method with Method Object Example
Bad Code
//class Order....
double price() {
double primaryBasePrice;
double secoundaryBasePrice;
double tertiaryBasePrice
// long computation;
.....
}
After Refactoring
Decompose Conditional Example
Lazy Class Example
A class that isn't doing enough to carry its weight. We let the class die with dignity.
Treatment
- Inline Class
- Collapse Hierarchy
Collapse Hierarchy Example
Dead Code Example
- Code that is no longer used in a system or related system is Dead Code.
- Increased Complexity.
- Accidental Changes.
- More Dead Code
Treatment
- Delete
Here i just try to focus on some refectoring. There are lots more left to discuss. Those i will try to cover in my next post.
All rights reserved