Rename Method Refactoring in .NET

When programming in .NET there’s a useful little idiom that can assist with some renaming refactorings. In his book, Fowler lists the steps needed to rename a method safely. Step 6 is “Find all references to the old method name and change them to refer to the new one”.

I wondered if there was any way to automate this rather than doing this by hand. The answer is simple: label the old method with an Obsolete attribute. The compiler now issues a warning for each use of the method enabling me to quickly press F4 (I’m using Scite) to step through all the method calls. Here’s what it looks like. The second parameter of the attribute should be set to false so that only a warning is issued. (After all this is a behaviour preserving refactoring, not an error).

[Obsolete("Prefer to use Arc version of this method", false)]
public void addProperty(string uriRef, string literalValue) {
  PlainLiteralNode theObject = new PlainLiteralNode(literalValue);
  ResourceDescription desc = new ResourceDescription(theObject);
  addProperty(new UriRef(uriRef), desc);
}

Permalink: http://blog.iandavis.com/2004/08/rename-method-refactoring-in-net/


Other posts tagged as net, programming, technology

Earlier Posts