Click here to Skip to main content
15,884,099 members
Articles / Refactor

Refactor Your Code Easily Using Visual Studio 2012

Rate me:
Please Sign up or sign in to vote.
4.91/5 (4 votes)
29 Jun 2012CPOL4 min read 52.9K   7   5
How to refactor your code easily using Visual Studio 2012

In our last blog post, we discussed about browser switching mechanism in Visual Studio 2012 for web developers to test their web applications against different web browsers which actually increases the productivity time of such applications.

Today in this post, we will learn about the Refactor feature of Visual Studio 2012 which will again improve your productivity without using any additional tools or extensions. So, continue reading this post to learn about this feature of new Visual Studio IDE.

Note: I composed this post based on Visual Studio 2012 RC (version: 11.0.50522.1 RCREL) and can’t guarantee this in the final version of the product though the chance of feature change is very negligible.

Microsoft introduces a new “Refactor” menu in Visual Studio 2012 IDE, which will help developers to refactor their codes easily without using any 3rd party control or extensions. As this is an in-built feature now, you will get it automatically with the new Visual Studio IDE.

Here is a sneak peak of what you will now see in your Visual Studio 2012 IDE when any code file is open on the screen:

Refactor Menu in Visual Studio 2012

You can also easily access the “Refactor” menu from the right click context menu of the code editor screen as shown below:

Refactor Context Menu in Visual Studio 2012

There are six sub menus available in the “Refactor” menu named “Renamed”, “Extract Method”, “Encapsulate Field”, “Extract Interface”, “Remove Parameters” and “Reorder Parameters” as shown in the above screenshots. You will easily get the meaning of each individual menu items by their name. Let’s go a little deep and see the individual screens of the “Refactor” menu items.

Rename

Using the “Rename” menu item, you can easily rename any field, property, method, class, etc. which will give you an option to preview your code before changing its reference all over the projects. Have a look into the rename dialog:

Visual Studio 2012 Refactor - Rename

This dialog window also provides you an option to search reference in comments, strings and also provides you an option to search and rename the overloads.

Extract Method

Suppose you have a bunch of code which you want to move into a separate method, the “Extract Method” menu will help you to refactor such code.

Visual Studio 2012 Refactor - Extract Method

Once it extracts the code to a separate method, it will replace the existing code at the original place with the method call.

Encapsulate Field

Do you have class level fields? Do you want to encapsulate those fields in a property? This dialog window will help you to encapsulate the actual field data by public properties.

Visual Studio 2012 Refactor - Encapsulate Field

This will allow you to preview the changes before commit. This dialog also enables you to search the reference in comments and other strings available in the solution.

Extract Interface

This refactor option will allow you to extract a method to the interface level. When chosen, this will create a new interface in the project and extract the method there to use in other places. Have a look into the below screenshot:

Visual Studio 2012 Refactor - Extract Interface

As shown above, you can choose the methods that you want to extract to the interface level and change the name of the interface. When applied, this will automatically inherit that interface in that original class.

Remove Parameters

This refactoring option allows you to remove additional parameters from a method and update the references everywhere that has been used. Generally, you need this feature to remove unused parameters from the methods.

Visual Studio 2012 Refactor - Remove Parameters

As seen in the above screenshot, this refactoring allows you to preview the changes before committing the changes in all the files.

Reorder Parameters

Parameter reordering is another refactoring mechanism that allows you to change the parameter position passed through the method. The up and down arrows placed at the right side allows you to rearrange them in the method signature as shown below:

Visual Studio 2012 Refactor - Reorder Parameters

Like other refactoring mechanism, this also allows you to preview the reference changes throughout the whole solution before committing in all the files.

I hope that this small tip will be helpful for you to improve your productivity. Stay tuned to my blog to get article updates in the coming days. Also connect with me on Twitter, Facebook to get regular updates. Subscribe to my blog’s RSS Feed and Email Newsletter for immediate updates.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
QuestionLame compared to Eclipse or IntelliJ Pin
kevin cline16-Oct-15 13:49
kevin cline16-Oct-15 13:49 
Question"REFACTOR" option not available in Visual Studio Professional 2012 Version 11.0.60610.01 Update 3. Pin
TTariq20-Nov-13 23:37
TTariq20-Nov-13 23:37 
QuestionAbout VB.Net Pin
my_webmail27-May-13 22:35
my_webmail27-May-13 22:35 
GeneralMy vote of 4 Pin
Farhan Ghumra2-Jul-12 2:42
professionalFarhan Ghumra2-Jul-12 2:42 
GeneralRe: My vote of 4 Pin
Kunal Chowdhury «IN»3-Jul-12 9:30
professionalKunal Chowdhury «IN»3-Jul-12 9:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.