Refactoring in Visual Studio 2005


Introduction

Refactoring is a new feature added to Visual Studio 2005 codename Whidbey. Refactoring allows developers to use the built-in feature of Visual Studio to rename and convert objects, which could be a handy tool when a developer wants to reorganize or optimize their codes.

Refactor Menu

The Refactor option is available only when you are editing the source code of the project. Either you can launch refactor option from main menu of Visual Studio (See Figure 1) or you can use right mouse click in the code editor itself (See Figure 2).

Figure 1. Refactor menu from VS.NET main menu.



Figure 2. Refactor menu in the code editor.

Encapsulate Field

Encapsulate Field option encapsulate a field in a property. So you can convert your fields defined in your class in properties using this option. For example, if you have private variables defined as following:

and you want to convert your variable (field) 'c' to a property, just right click on 'c' and select Refactor->Encapsulate Field, which launched Encapsulate Field dialog (see Figure 3), where you can give the name of the property.

Figure 3. Encapsulate Field dialog.

Next dialog is preview dialog after you click OK (see Figure 4).

Figure 4. Preview Changes - Encapsulate Field dialog.

Now when you click Apply, you will see new property added to the code, which looks like Figure 5.

Figure 5. Property generate using Encapsulate Field.

Encapsulate Field can be a handy feature when in a large project you are converting your existing fields to properties.

Extract Method and Extract Interface

Extract Method (see Figure 6) and Extract Interface (see Figure 7) options extract the selected code and write new methods and interfaces.

Figure 6. Extract Method dialog.

Figure 7. Extract Method dialog.

Reorder Parameters

As you can tell from Reorder Parameters name, it is used to reorder a method parameters. For example, I have a method called Add with two parameters a and b (see Figure 8).

Figure 8. Add method.

Right click on the method and select Reorder Parameters, you will see Reorder Parameters dialog (see Figure 9). On this dialog, you can change the order of parameters by selecting the parameter and use up and down arrows.

Figure 9. Reorder Parameters dialog.

The new method is shown in Figure 10.

Figure 10. Reordered Add method.

Remove Parameters

Remove Parameters simply removes selected parameters from a method. I personally don't see any use of this feature because It doesn't remove the parameter references. So I still have to go change the code manually for the parameters which I have removed.

You simply select parameters and click Remove method (see Figure 11) to remove a parameter.

Figure 11. Remove Parameters dialog.

The removed parameter is marked as shown in Figure 12.

Figure 12. Removed parameters.

The new method looks like the following:

Rename

Rename option simply renames a method or a field. You can even rename all the references of a method or field or property in the entire project using this option.

Promote Local Variable to Parameter

Promote Local Variable to Parameter option converts a local variable to a parameter of a method. There is an error if you try to convert an non initialized variable. Now let's say I want to convert variable c to a parameter of Add method, I fist initialize it. The new method looks like the following:

The new method looks like the following after promoting 'c' to a parameter:

Surround With and Insert Expansion

Surround With option is pretty useful. You can use it to surround with expansions or your already saved code snippets (see Figure 13).

Figure 13. Surround With option.

If you select My Expansions, you can select from various expansions (see Figure 14).

Figure 14. Surround With option.

Now if you have some code snippet saved in your library, that will be available on code snippets option and code will be inserted in your class.

Insert Expansions option simply insert the expansions (see Figure 14).

Summary

In this article, I discussed Refactoring feature of Visual Studio 2005. Even though you may not use these features often, but they can be used from time to time and help reduce your development time.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.