Code Refactoring – Part 1

Refactoring means making the code better, better doesn’t mean faster, better here means better structured / better built / more readable and more understandable to make it easier to work, easier to identify the bugs and fix them, easier to add new features and stop code gradually getting out of control.

Please remember that there are techniques which we can learn from books and stuff on internet but how you can refactor your code that you can do by scanning your code for large methods, large objects, use of temp variables, use of conditions and at last code smell.

In this article we will discuss few ways

-> Extract Method: Extract method is a technique which allows you to create a method for the block of code which you have written for a specific purpose.

Code smell:
Large method >> Large doesn’t mean length it
Comment >> If you have comment for the section of code then that code can be form as method and that method name (Verb Noun combination to create method name) can itself explain the purpose.

-> Inline Method: Inline method  refactoring is exactly opposite of extract method. Inline method is finding a call to a method that adds no value, no additional clarity and no additional modularity and then remove that method and replace with the code.

Example:
static double calculateAreaOfCircle(double Radius) {
return (getValueOfPI() * Math.Pow(Radius,2));
}

static double getValueOfPI(){
return Math.PI;
}

Above method “calculateAreaOfCircle” can be replaced with below code

static double calculateAreaOfCircle(double Radius) {
return (Math.PI * Math.Pow(Radius,2));
}

-> Replace the temp with the Query refactoring – Inline Temp Refactoring
This technique consists of two part

1) Extract the expression and create method
2) Replace the method name wherever temp variable is used.

Please remember that before applying this technique make sure that in the code you are not modifying the value of temp variable.

Example:
static bool CheckWithdrawalStatus(int WithdrawalAmount) {
double availableBalance = _balance – _charges;
if (availableBalance > WithdrawalAmount)
{
Console.WriteLine(“Approve!”);
return true;
}
else {
Console.WriteLine(“You have available ” +  availableBalance + ” balance.”);
return false;
}
}

This can be rewrite like below

static bool CheckWithdrawalStatus(int WithdrawalAmount)
{
if (availableBalance() > WithdrawalAmount)
{
Console.WriteLine(“Approve!”);
return true;
}
else
{
Console.WriteLine(“You have available ” + availableBalance() + ” balance.”);
return false;
}
}

static double availableBalance(){
return _balance – _charges;
}

Refactoring That Add Temps
>> Split Temporary Variable: Don’t use the same temp for different reason.

Example:
double Price = 100.50;
int Quantity = 10;
double t = Price * Quantity;

double TotalAmount = 5000;
double DiscountAmount = 100;
t = TotalAmount – DiscountAmount;

>> Introduce Explaining Variable: Variable should be self explanatory
Example: Below example is easy to understand but after a week, a month or a year it will take time to understand if new person is trying to debug

if((order.getTotal() > 99 && (stock.CheckStatus(order.getItem()) > order.getQuantity())) || _balance > 4000){
Console.WriteLine(“Everything is fine.”);
}

This code can be then rewritten by introducing new temp variables

bool freeShipping = order.getTotal() > 99;
bool stockAvailable = stock.CheckStatus(order.getItem()) > order.getQuantity();
bool balanceAvailable = _balance > 4000;

if((freeShipping  && stockAvailable) || balanceAvailable ){
Console.WriteLine(“Everything is fine.”);
}

>> Remove Assignments to Parameters: This simply means don’t assign the value to the parameters passed as input to the method and then use in the method,instead of doing this introduce new temp variables and use them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s