Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Saturday, February 09, 2008

Refactor .NET code using Visual Studio and Refactor! Pro

Modifying the existing source code without changing its functionality is called refactoring of Source Code. Visual Studio gives us a lots of features for doing Refactoring the .NET Source Code, but sometime these are not sufficient for true .net developers. In this article we shall discuss about various refactoring features available in Visual Studio .NET and Refactor! Pro.


Visual Studio Refactoring Features

1. Extract Method: When you are calling a function which you did not write till now or yet to be declared, then you can use this refactor feature to write ready made code with dummy function.

2. Encapsulate Field: The feature converts public class member variable into private class member variable and generates code for property set get methods.


3. Extract Interface: Using a set of existing type members it defines a new interface type.

4. Reorder Parameters: This feature allows you to re order the parameters which you declare as formal variable for a function.

5. Rename: This is very frequently used feature to rename a variable throughout the project.

6. Promote Local Variable to Parameter: It helps to convert variables from local to function formal parameters.

Visual Studio Plug-in Refactor! Pro Features: Refactor! Pro is Visual Studio Plug-in, which nearly 125 refactoring features to visual studio. I have mentioned some of main features bellow from 7th point.



7. Make Implicit: If you would like to refactor your code in declaration part as traditional declaration to implicitly-typed local variable then you can avail the new Visual Studio refactor option called Make Implicit.

For example your declaration is as follows

Dictionary<string, string> map = new Dictionary<string, string>();

Now simple right click on above declaration statement then you get context menu with the menu item called Refactor, Now choose Make Implicit then your code automatically become as follows by using implicitly-typed local variable.

var map = new Dictionary<string, string>();

We can also use this option in reverse to change your code declarations from implicitly typed local variables to traditional full declaration.

Here you can view Make Implicit Refactoring demos using bellow links created by Did it with .NET

VMake Implicit Refactoring Demo 1
VMake Implicit Refactoring Demo 2

8. Name Anonymous Type: This is one of the beautiful feature I like in visual studio 2008.  Anonymous Type is one of the new feature of .NET 3.0, it enables creating the new objects with our class definition. See the bellow example.

var employee = new { EmpNumber = 100, EmpName = "Dutt" };

In some cases like targeting the .NET 3.0 compiler to .NET 2.0, above feature will work. In such cases we can use Name Anonymous Type Refactoring feature. It automatically generates class definition to our Anonymouse Types.

Check the Name Anonymous Demo

9. Renaming Query Expressions: Query Expressions is a new good feature of .NET 3.0 and 3.5. Now you can use Rename Refactor feature even for the names used in Query Expressions like LINQ queries.

Check the Rename LINQ Query Expressions Refactoring Demo

10. Convert to Auto-Implemented Property: Automatic properties are new feature of .NET 3.0. This feature is used to change traditional .NET 2.0 properties to .NET 3.0 automatic properties.

Check the Covert to Auto-Implemented Property

even you can use this feature in reverse to covert Automatic Properties to normal properties. Here is the demo

11. For to Fetch: This Refactoring option is to covert from For loop to foreach loop.

12. Expand Lambda Expression: This feature converts Lambda expression query parameters by using delegates. See the demo

Read Also:
22 New Features of Visual Studio 2008 for .NET Professionals
Writing/Developing Facebook Applications in .NET using Facebook.NET SDK
.NET Obfuscation using Dotfuscator for Source Code Protection

Post a Comment