Archive for Reviews

Finding references in Visual Studio – an annoying behavior

A good tool from Visual Studio 2005 is the “Find all references” in the context menu. However, it has a annoying behavior in a common situation within my project.

Suppose I have the A, B and C classes that implements the I interface, and that interface has declared a method named “save”. Now, I want to know where in my code the method C.save() is called, and usually I would right-click the method name in the method definition at the C class and use the “Find all references” tool. However, if I do this, the Visual Studio tool will find all the references of any “save()” method of the project, even of the A or B classes.

The best behavior of this tool would be finding only the references of the “save()” method called from instances of the C class.

Leave a Comment

Refactoring with Visual Studio 2005

I have started worrying about systems maintenance while working on my first BIG project, a virtual “classroom” for an e-learning solution. A great idea when dealing with this subject is to know more about refactoring, a technique that lets you change your code (with the purpose of getting it more simple or easier to understand) without changing its external behavior. The refactoring discipline has some common techniques that are well known like the “extract method”, that consists on identifying a source code snippet that is commonly used along the software and put it inside a new method.

The Visual Studio 2005 has the new “Refactor” feature, that help us implement some of the most used refactoring techniques. It is very simple to use: all you need to do is select the part of the code that you want to refactor and right-click and select the “refactor” submenu. This is shown below:

Refactor submenu

I think I don’t need to talk about renaming =) But this is very useful, much better than the old find-and-replace way.

You can use the extract method refactoring when you have a code snippet that is commonly used along the software code, resulting in duplicated logic. All you need to do is select the part of the code that represents this snippet and right-click selecting the Refactor >> Extract Method option. Then you define a name for the new method and click OK. You must take care about some issues:

  1. If the code snippet is used elsewhere, you will have to go there and change the code manually do use the new method.
  2. Visual Studio will pass the scope variables used by the code snipped automatically to the new method. So, it’s a good practice to be more careful with side effects dealing with these variables, specially if the method will be used somewhere else.

The encapsulate field option generates code for the get and set handlers of a given class field. This is useful, but would be better if the generated code was placed in a default #region snippet, resulting in a well organized code.

Look at the code below.

public class Car
{
   private int power;
   private string model;
   private string manufacturer;
}

If we right click at each field and select Refactor >> Encapsulate field, the result will be this:

public class Car
{
   private int power;

   public int Power
   {
      get { return power; }
      set { power = value; }
   }
   private string model;

   public string Model
   {
      get { return model; }
      set { model = value; }
   }
   private string manufacturer;

   public string Manufacturer
   {
      get { return manufacturer; }
      set { manufacturer = value; }
   }
}

However, the code does not look good… It would be better if the generated code looked something like this:

public class Car
{
   private int power;
   private string model;
   private string manufacturer;

   #region Field access methods
   public int Power
   {
      get { return power; }
      set { power = value; }
   }
   public string Model
   {
      get { return model; }
      set { model = value; }
   }
   public string Manufacturer
   {
      get { return manufacturer; }
      set { manufacturer = value; }
   }
   #endregionregion
}

I did not use the other options of the Refactor submenu, but as soon as I use them I’ll post something about.

Leave a Comment