Refactoring in Visual Studio 2005
12/5/2006 3:18:23 PM
Working in the custom software development arena it is customary that projects and / or source code grow increasingly complex over time. You are either adding extra "requirements" to existing code or inheriting less-than-ideal code (read kludgy). Enter refactoring. With refactoring you can reorganize the internal structure of the code without changing the public appearance or interface.
In Visual Studio 2005 there are quite a few new refactoring tools. The list below are some of the more helpful and are limited to C# development. Sorry.
Encapsulate Field
This process turns a variable declaration into a public property. If nothing else this will save you a significant amount of typing. The property syntax is automatically generated.
Before:
private string foo;
After:
public string Foo
{
get { return foo; }
set { foo = value; }
}
Extract Method
Imagine inheriting source code for a project that has a 550 line Page_Load method. It's ugly. Not only does the method not fit on one screen, there is no way to possibly describe what the method does in one sentence or less. If you're like me you will find a need to granularize this method once you get somewhere between SqlDataReader reader2 and SqlDataReader reader15. Now enter the Extract Method in the Refactoring drop-down. All you need to do is highlight a large block of code that you want to granularize / extract into a child method and select 'Extract Method' from the Refactoring drop-down. Now simply type a name for this child method and it will appear below the parent. You are now one step closer to having granularized source code. Remember, it's easier to build something big out of many small components.
Parameter Refactoring
Over time methods that take parameters will change and as a result of that the number of parameters will change in one way or another whether a string is now passed or there are two integers being passed in instead of four. This is a nice maintenance method that can help you reorder your parameters. Let's say that you have a few common parameters such as projectId, employeeId, timeStamp. If you are like me then it is of utmost importance to make sure that those parameters are passed into a method in the same order everytime. The Parameter Refactoring tool will allow you to do that. Every reference to a parameter-refactored method will be updated accordingly. Easy.
What About VB?
Sorry folks. The same refactoring tools that C# developers can take advantage of aren't built-in to Visual Basic .NET. There is an add-on called Refactor! that will mimic C#'s refactoring functionality and then some. Peace to having to download an add-on.
Programming,
Tools
