Tuesday, November 5, 2013

Refactoring in Visual Studio, Part 2

Overview

In addition to renaming which we covered in Part 1, Visual Studio has many other refactoring options including extracting a method from an existing block of code, encapsulating a field within a property, extracting an interface from a class, removing parameters, and reordering parameters. Let's see what they do and how to use them.

Extract Method

The "Extract Method" refactor option lets you move a block of code into a new method. Take a look at the below block of code. This code looks like a commonly used block to construct a database command. I would like to move it into a separate method so I can reuse this same logic elsewhere.


To extract a method from this code, you first must select the code as show above. Next right-click within the selected block and select "Refactor-->Extract Method...".


Now you are presented with a dialog where you name your new method, so type in the new name and hit OK.

Your class now has a new method named CreateCommand(), and the prior location serves only to call the new function.


Encapsulate Field

The Encapsulate Field refactoring option takes a field within a class and encapsulates it within a property. This can save you from writing a lot of plumbing when setting up properties with private members. Let's see how it works.

In this class I have a single private field, m_aField. I want to allow other classes to manipulate this value, but in a noble effort to hide the implementation details of this complex chunk of awesome I have decided I want a public property that clients can use. Refactoring to the rescue!


Right-click the field and select "Refactor-->Encapsulate Field...". Visual Studio will select a name for your new field (feel free to change it if you like). Click OK and your code has a brand-spankin-new public property which encapsulates that private field. Yay!


Extract Interface

Now let's take that useful class from the last refactoring option and mangle it further. I have decided that I want multiple classes with the same interface (public properties and methods) as the class RefactoringStuff. The best way to do this is with an interface, and Refactoring can help you here too. Right-click on the class and choose "Refactor-->Extract Interface...".


Visual Studio kindly gives you an opportunity to name your interface and select the properties and methods that will make up the interface definition, so type/select what you like then click the OK button.


Pretty sweet huh? You have a brand new interface named IRefactoringStuff, and the class RefactoringStuff now implements that new interface.

public class RefactoringStuff : ABDebug.IRefactoringStuff
  {
    private string m_aField;
 
    public string AField
    {
      get { return m_aField; }
      set { m_aField = value; }
    }
  }

  interface IRefactoringStuff
  {
    string AField { getset; }
  }

Remove Parameters

Why would you ever want to use a GUI to remove parameters? If your method is called a few times in a few different units, it can be a pain to first manually remove the parameter then go find and change every place that calls the method. So, Visual Studio helps us out once again with Refactor-->Remove Parameters. Take the class below:

  public class RefactoringStuff
  {
    public string AMethod(string param1, string param2)
    {
      return param1 + " !some text in the middle! " + param2;
    }
 
    public void Caller1()
    {
      string local = AMethod("hey""you");
    }
 
    public void Caller2()
    {
      string local = AMethod("hola""senor");
    }
  }

I want to remove param2 from the method AMethod. Right-click in the method and select "Refactor-->Remove Parameters". Select the parameter you want to remove and click the Remove button. Click OK when done.


You are presented with another dialog where you can preview the changes that Visual Studio is going to make. Review it's plan and click OK when ready.


Boom! code modded. But hey wait just a minute here fella, my code doesn't compile! Yeah well nobody's perfect, including Visual Studio. You'll notice in the code below that param2 is still referenced in AMethod. Just get rid of it manually and move along.

  public class RefactoringStuff
  {
    public string AMethod(string param1)
    {
      return param1 + " !some text in the middle! " + param2;
    }
 
    public void Caller1()
    {
      string local = AMethod("hey");
    }
 
    public void Caller2()
    {
      string local = AMethod("hola");
    }
  }

Reorder Parameters

The last option we'll cover is reordering parameters. I won't get into much in the way of screenshots as you've probably figured out the process by now and are just about tired of reading, so let's just take a quick look at some code. Here we're back to the class RefactoringStuff. I want to switch the order of param1 and param2 in the method AMethod.

  public class RefactoringStuff
  {
    public string AMethod(string param1, string param2)
    {
      return param1 + " !some text in the middle! " + param2;
    }
 
    public void Caller1()
    {
      string local = AMethod("hey""you");
    }
 
    public void Caller2()
    {
      string local = AMethod("hola""senor");
    }
  }

Right-click within the method AMethod and select "Refactor-->Reorder Parameters...". Use the dialog to reorder parameters as you desire, click OK, the preview window then comes up, review your changes, click Apply. The parameters in AMethod have been reordered and all calls to AMethod have had their parameters switch around as well.


    public string AMethod(string param2, string param1)
    {
      return param1 + " !some text in the middle! " + param2;
    }
 
    public void Caller1()
    {
      string local = AMethod("you""hey");
    }
 
    public void Caller2()
    {
      string local = AMethod("senor""hola");
    }
  }

Thanks for your time again folks, and if you enjoyed this post be sure give us a big happy +1 in google, share us on facebook and twitter, subscribe to the blog, tell your neighbors and your neighbor's dog. Comments are encouraged!

No comments:

Post a Comment