Click here to Skip to main content
Click here to Skip to main content

Refactor your Code easily using the Visual Studio 2012

, 29 Jun 2012
Rate this:
Please Sign up or sign in to vote.
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

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 the developer to refactor their codes easily without using any 3rd party control or extensions. As this is an in-built feature now, hence 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 in 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 to 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 option to preview your code before changing it’s reference all over the projects. Have a look into the rename dialog:

 

Visual Studio 2012 Refactor - Rename

 

This dialog window also provides you option to search reference in comments, strings and also provides you 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 those 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 through out the whole solution before committing in all the files.

 

I hope that this small tips will be helpful for you to improve your productivity. Stay tuned to my blog to get article updates in 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 update.

License

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

About the Author

_ Kunal Chowdhury _
Technical Lead
India India
Kunal Chowdhury is a Microsoft "Client Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer 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 8 (WinRT). He posts his findings, articles in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Question"REFACTOR" option not available in Visual Studio Professional 2012 Version 11.0.60610.01 Update 3. PinmemberTTariq20-Nov-13 23:37 
QuestionAbout VB.Net Pinmembermy_webmail27-May-13 22:35 
GeneralMy vote of 4 PinmemberFarhan Ghumra2-Jul-12 2:42 
GeneralRe: My vote of 4 Pinmvp_ Kunal Chowdhury _3-Jul-12 9:30 
Thanks for the vote.
Regards - Kunal Chowdhury
Microsoft MVP (Silverlight) | Codeproject MVP & Mentor | Telerik MVP

 
Follow me on: My Technical Blog | Silverlight-Zone | Twitter | Facebook | Google+

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 29 Jun 2012
Article Copyright 2012 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid