Code Refactoring
Another great new tool provided in VS2005 is Code Refactoring. What is Refactoring? Well, it’s the ability to change the internal structure of your code without changing the external behavior of your code.
Code Refactoring has 7 operations; you can use these operations by issuing a right-click in your code IDE. I’ll discuss each of them in detail:
- Extract Method – this operation enables you to create a new method based on the code fragments of your code. Example:
(Just highlight the code between the Extract Method begin and end. Right click, select Refactor->Extract Method)
// before the Refactoring “Extract method” applied
private void button1_Click(object sender, EventArgs e) {
DataTable dt;
int x = 0;
for (int i = length - 1; i >= 0; i--) {
// Extract Method begin
x = 100;
dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col1");
DataRow dr = dt.NewRow();
x = i + 1;
// Extract Method end
}
}
// After the Refactoring “Extract method” applied
private void button1_Click(object sender, EventArgs e) {
DataTable dt;
int x = 0;
for (int i = length - 1; i >= 0; i--) {
dt = NewMethod(ref x, i);
}
}
private static DataTable NewMethod(ref int x, int i) {
DataTable dt;
x = 100;
dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col1");
DataRow dr = dt.NewRow();
x = i + 1;
return dt;
}
As you can see, the Refactor operation is smart enough to consider variables in the selected code fragment that has an outside declaration of the code selected. See what happened to the DataTable variable. Since the declaration is outside its scope, it then just made it as a parameter to avoid code-logic changes.
- Rename – this operation is more simple than the previous. It just gives you the ability to change a variable (field, class, etc…) name and also change all variables that references to it. For example you have a class Student and you want to rename it as StudentNew, if you’ll do it manually you’ll have to search all of your documents and change the class name. But, if you use the Rename operation, the IDE automatically replaces all occurrences of the class Student and rename it to StudentNew. To use, just highlight the variable name that you want to change, right click, select Refactor->Rename.
- Encapsulate Field – this operation gives you an automated way to create properties of the class by existing local variables or fields. To properly take a grasp of its meaning, Here’s an example:
// before the Encapsulate field operation
class RefactoredClass {
public RefactoredClass() { }
/*start selection*/
string _name = "";
/*End selection*/
}
// After the Encapsulate field operation
class RefactoredClass {
public RefactoredClass() { }
string _name = "";
public string Name {
get { return _name; }
set { _name = value; }
}
}
- Extract Interface – this gives you the capability to automatically generate interfaces based on an existing class or struct definition. Lets use our RefactoredClass sample: (to use, just highlight the class name and right click, select Refactor->Extract Interface)
// Before the Extract Interface operation
class RefactoredClass {
public RefactoredClass() { }
string _name = "";
public string Name {
get { return _name; }
set { _name = value; }
}
}
// Before the Extract Interface operation
class RefactoredClass : CodeSnippets.IRefactoredClass {
public RefactoredClass() { }
string _name = "";
public string Name {
get { return _name; }
set { _name = value; }
}
}
// Plus, it creates a new File which contains the Interface definition.
This operation would let you choose what fields or methods to include in the interface.
- Promote Local Variable to Parameter – I guess this is self-explanatory. It simply lets you select a local variable and make it a parameter with just a single click.
- Remove Parameters – This also is self-explanatory. It simply provides you with a dialogbox to remove parameters:
- Reorder Parameters – this is just like the remove parameters operation. It also gives you an interface to reorder your paramters in a method:
This feature is indeed very useful and could boost your programming time just like code snippets. These Refactoring operations that are being introduced would lessen the chance of bugs appearing whenever we want to reorganize our code. So I guess it’s a very good practice to make use of these tools whenever you want to clean-out your code…