Refactoring is the practice of making your code cleaner and clearer without affecting the functionality. Find out what's included in Visual Studio's Refactor menu, and discover a couple of Visual Studio add-ins that bring refactoring to the Visual Basic community.
One of the key aspects of Extreme Programming is refactoring, which improves code without affecting its function. The Visual Studio IDE provides refactoring features in the 2005 and 2008 versions that you can easily put into action with a mouse click. Unfortunately, this feature is only available to C# developers, but there are add-ons for developers using Visual Basic. Here's a closer look at refactoring via Visual Studio.
A simple approach
A simple explanation of refactoring is the practice of making your code cleaner and clearer while not affecting functionality. You may hear others describe it as code rewriting, but this is far from the truth. It is not rewriting since code functionality does not change. The code is working, so no rewriting is necessary — you're just trying to make it easier to maintain and read.
After all, what developer hasn't been faced with code they did not develop? Making a change in such a situation can be a pain when the actual code is hard to understand. Refactoring makes it easier to work through these instances.
Refactoring is typically accomplished in small steps while maintaining functionality. You may refactor as you move along with development activities. The Visual Studio 2005 and 2008 development environment provide plenty of options to help with refactoring.
Using Visual Studio
Visual Studio is the de facto standard development environment for those building .NET applications. Refactoring became a feature of the tool beginning with version 2005 of the IDE. It is available via a drop-down menu called Refactor. The menu is accessible by right-clicking within code as well. The Refactor menu provides the following choices:
- Rename: This feature allows you to easily rename items without the need to search and replace all references within the code. This menu option automatically performs the search and replace.
- Extract Method: This feature allows you to select a block of code and extract it into a separate method. The block of code is replaced with a call to the new method. This is a great feature to use when realizing that a block of code needs to be used repeatedly.
- Encapsulate Field: When you use this option, a public property encapsulating a field is created. It is an easy shortcut to creating properties when you realize they are necessary for certain fields within your code.
- Extract Interface: This feature allows you to create an interface for existing code. It examines the current class and creates an interface based on its public properties and methods.
- Promote Local Variable To Parameter: Developers often discover that a variable declared locally within a method should be a parameter passed to the method. This menu selection automates the process of moving a local variable to a method parameter. One click and the variable becomes a parameter.
- Remove Parameters: This allows you to easily remove a parameter from a method signature. In addition, all calls to the method are altered to remove the passed parameter as well. Visual Studio provides a preview window for viewing the changes before selecting OK and making them permanent.
- Reorder Parameters: This feature automates the reordering of parameters in a method signature. This may seem like a simple task, but it handles the reordering of parameters in all code that references the changed method.
The best way to get better acquainted with these features is to use them. Take Refactor for a test drive with your code to get an idea of how it works and notice how code is cleaned up and simplified.
Visual Basic developers
The only Refactor option available to Visual Basic developers is Rename. While this is a good feature to have, Microsoft leaves Visual Basic developers out in the cold with regards to the other features.
Visual Studio add-ins are available for bringing refactoring to the Visual Basic community. One popular product is Refactor! Pro. Another excellent Visual Studio add-in is ReSharper; it does not, however, target Visual Basic developers.
The omission of refactoring features for Visual Basic leaves me scratching my head. Microsoft proclaims it would never turn its back on the Visual Basic community, but actions speak louder than words.
Available to all developers
Who doesn't want to produce better code?
Refactoring allows developers to simplify their code to deliver cleaner and more manageable solutions without losing functionality. Thankfully, Microsoft has recognized the importance of refactoring with its inclusion in Visual Studio 2005 and 2008.
Are you employing extreme programming principles in your everyday development chores? Has refactoring helped you produce better code? Share your thoughts with the Web Developer community.
Related TechRepublic resources
- Download: Why bother with refactoring? Here are some good reasons
- White paper: Code Refactoring in VS.NET 2005
Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.
———————————————————————————————————————————-Get weekly development tips in your inbox Keep your developer skills sharp by signing up for TechRepublic's free Web Developer newsletter, delivered each Tuesday. Automatically subscribe today!