Refactoring in Visual C# 2005 - ' The Need for Refactoring ' (
Page 2 of 5 )
One of the new features of Visual C# is refactoring, the practice of improving the design of your program while coding. Visual C# 2005 supports refactorings such as Rename, Extract Method, and several more. This article demonstrates how to use these tools, why you would want to use them, and shows how they can help you be more productive.
The Need for Refactoring
ADVERTISEMENT
In his book, Refactoring, Martin Fowler provides the following definition:
"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."
We all do refactoring at one time or another, even if we don't have a label for it. If we've given a method a name that isn't as meaningful as it should be, we rename the method and then rename the calls to the method. In another example, consider every time you write an algorithm that goes too long, or when you add functionality to an existing method. Often, a method becomes too long, and you find that it is trying to do more than one thing. Such a situation suggests that you should extract some of its code and put it into a separate method for proper modularization. These are typical refactorings we do all the time.
For all the good that is had by refactoring, there can also be difficulty. Naturally, human error is a continuing factor any time we work with code, and refactoring can result in errors being introduced into your code. Unit testing helps detect these problems by running tests against the code after each refactoring, greatly decreasing or eliminating the chance of introducing new errors.
The next evolution in refactoring is tools to automate this process. Visual C# 2005 joins the population of IDEs that offer automated refactoring. It supports some of the most common refactorings, such as Rename, Extract Method, and Encapsulate Field. This article shows you how and why you should use each of these refactorings.
Note: Martin Fowler does an excellent job explaining the motivation for these refactorings. While I touch on the reasons why they are important, I recommend his book, Refactoring (Addison Wesley) for a more thorough discussion.
Rename
Often, we would like to rename a method to make it more meaningful, but we avoid it because of the thought of all the work changing the referring calls. When done in a practical matter, a Rename refactoring doesn't have to be so painful.
The Rename refactoring in Visual C# makes the process even easier. It not only changes the name of an identifier, but also renames every reference to the identifier within all projects of a solution.
Always be aware that changing identifiers of public type members breaks the public interface to code outside the solution. If this is the case, you should continue the Rename refactoring, but leave the copy of the public method intact, delegating to the new method name and decorate it with an Obsolete attribute.
One nice thing Visual C# now does is help keep class and file names in sync. I like to have one class per file, so this works out great. By default, the name of the main console application file appears as Program.cs. Similarly, the name of the class defined in Program.cs is Program. If you change the name of Program.cs in Solution Explorer, Visual C# asks if you want to change the class name also. Just click the OK button and Visual C# will make the change. For example, if I renamed Program.cs to BeginMain.cs, the Program class would be renamed as BeginMain.
To perform a Rename refactoring, select the identifier you want to rename. Proper selection works even if the carat is placed in the text of the identifier or only part of the identifier is selected. The identifier could be the method name or a call to the method name; it works both ways. Then right click, select Refactor, and select Rename. Assuming that you selected the identifier Add in Listing 1, you would see a dialog box as shown in Figure 1:
Listing 1. This demonstrates a typical method with a somewhat general name. We would like to do a Rename refactoring to give it a more meaningful and specific name, such as AddIntegers.
public int Add(int first, int second)
{
return first + second;
}