< Browse > Home / Programming / Blog article: Refactoring In Visual Studio 2008

| RSS

Refactoring In Visual Studio 2008

August 9th, 2008 | 3 Comments | Posted in Programming

While there is a lot of talk about refactoring tools such as Resharper and Refactor Pro, not much is said about refactoring features available in Visual Studio 2008 straight out of the box. I think Resharper and Refactor Pro are fantastic tools but they cost money and most of the time our refactoring needs are simple or you maybe in an environment where such tools are not available. So what do you do? Worry not, Visual Studio comes with excellent refactoring support. In this post I will show different refactoring features of Visual Studio.

Refactoring can be accessed either by going to Refactor menu or by right clicking on the text editor. Visual Studio comes with 7 refactoring options as seen below.

image

I will now show you how some of these refactorings work. I will work with this simple piece of code to illustrate simple refactorings. I will use another code sample for some refactorings. You will see it when it is required.

static void Main(string[] args)
{
    string msg = "Hello";

    if (msg != string.Empty)
    {
        Console.WriteLine(msg);
    }
}

Rename

This is perhaps simplest of all. It renames a member. This refactoring comes in very handy and is most likely one of the most used refactoring. It can rename a member within scope. Here I have few lines of code with a member declared as msg. To rename the member I just need to put my cursor on the member name and click on Refator –> Rename menu or hit F2.

image

I can also include comments and/or strings in my refactor operation. Here I will ask msg to be renamed to message. Clicking OK on the dialog above opens another dialog where I can preview my changes.

image

Extract Method

Extract Method extracts a method out of one or many statements. I can extract a method for the code within if statement. I am presented with a dialog which asks me the name of my new method. It is also smart enough to figure out that I need a parameter passed into this method.

image

Resulting code now looks like this.

class Program
{
    static void Main(string[] args)
    {
        string msg = "Hello";

        PrintToConsole(msg);
    }

    private static void PrintToConsole(string msg)
    {
        if (msg != string.Empty)
        {
            Console.WriteLine(msg);
        }
    }
}

Encapsulate Field

From here on I will use a different code sample. This code sample is a customer object with two variables and a method.

public class Customer
{
    private string name;

    private string address;

    public void PrintCustomerInformation()
    {
        string result =
            string.Format("Name = {0}\nAddress = {1}\n", Name, Address);

    }
}

This refactoring converts a private variable into a public property. By running Encapsulate Field on the private name variable presents this screen.

image

Once again I get a preview of changes which will be made.

image

I will also apply Encapsulate Field to address variable. The resulting code is modified to this.

public class Customer
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private string address;

    public string Address
    {
        get { return address; }
        set { address = value; }
    }

    public void PrintCustomerInformation()
    {
        string result =
            string.Format("Name = {0}\nAddress = {1}\n", name, address);

    }
}

Extract Interface

Extract Interface as the name says will extract an interface based on the members I select. By running Extract Interface I am presented with a dialog where I can select the members I wish to include in my interface definition. For this example I will include all Address, Name and PrintCustomerInformation members. I will also name my interface ICustomer.

image

By applying this refactoring Visual Studio creates another file for ICustomer interface. The resulting code of both files looks like this.

interface ICustomer
{
    string Address { get; set; }
    string Name { get; set; }
    void PrintCustomerInformation();
}
public class Customer : RefactoringDemo.ICustomer
{
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private string address;

    public string Address
    {
        get { return address; }
        set { address = value; }
    }

    public void PrintCustomerInformation()
    {
        string result =
            string.Format("Name = {0}\nAddress = {1}\n", name, address);

    }
}

Conclusion

Visual Studio refactoring tools may not be at the level of Resharper or Refactor Pro but they are powerful enough for most refactoring jobs. I have demonstrated some of these here. If you are interested in learning more about refactoring then I highly recommend this classic book.

Refactoring: Improving the Design of Existing Code (The Addison-Wesley Object Technology Series)

by Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts

Read more about this book…

kick it on DotNetKicks.com

Technorati Tags:






Leave a Reply 2582 views, 10 so far today |
Follow Discussion

3 Responses to “Refactoring In Visual Studio 2008”

  1. Dan Waldron Says:

    Hi,

    I’m just getting started with my new blog. Would you want to exchange links on our blog-rolls?

    BTW - I’m up to about 100 visitors per day.

  2. Deepak Says:

    Dan,

    Thanks for the offer. This blog is about Microsoft .Net which is not related to the main topic of your blog. Thus I will pass on your offer.

    I wish you all the best with your blog.

  3. Dmitri Says:

    most of the time our refactoring needs are simple

    Ha! I disagree completely. Most of the time *my* refactoring needs are far more complex than even ReSharper can satisfy.

Leave a Reply