ReSharper 5.0 Adds New “Add Parameter” Refactoring

Posted by Peter Kellner on April 26, 2010 · 5 mins read

In this post, I’ll show a simple example of how when you add a parameter to C# method, ReSharper gives you a simple prompting to ask if you want to add a parameter to your method, or create an overloaded method that gives you the flexibility to maintain the old method signature and have the new method.

In my original code, I had the following code that called the method GetLoadResultsFromInterval.

 

List<LoadResult> loadResultsForInterval =
GetLoadResultsForInterval(rec.TimePoint,
rec.IntervalLengthInMs);

Then, I simply added one more parameter (loadFtpArrivalTimeDictionary) and ReSharper prompted me with the following:

 

image

If I choose the first “Add Parameter, I get what I would expect, which is just the method with an extra parameter.

/// </summary>
/// <param name="timePoint"></param>
/// <param name="intervalLengthInMs"></param>
/// <param name="loadFtpArrivalTimeDictionary"></param>
/// <returns></returns>
private static List<LoadResult>
GetLoadResultsForInterval(DateTime timePoint,
int intervalLengthInMs,
Dictionary<string, DateTime> loadFtpArrivalTimeDictionary)
{
throw new NotImplementedException();
}

It even adds the param xml definition automatically.

However, I’m not quite as pleased with the second refactoring, that is, “Create Overload”.  It does almost everything I would expect, but does not implement the method, which I think it could.  Or, at least it could give it a try.  What it does implement is the following:

image

I’m actually including a picture of it rather than the actual code because it created an error.  It should have said DateTime instead of loadFtpArrivalTimeDictionary.  Two other things of interest.

First, it did not add xml comments (which is probably because of the syntax error), and second, I would have expected it to have called the overloaded method as follows instead of what it did.

 

private static List<LoadResult> GetLoadResultsForInterval(DateTime timePoint,
int intervalLengthInMs,
Dictionary<string, DateTime> loadFtpArrivalTimeDictionary)
{
// - put additional code here
return GetLoadResultsForInterval(timePoint, intervalLengthInMs);
}
 
(before anyone points out this is the opposite of how you would normally overload a method call, I do know that.  That is, typically, you would have a method with 2 incoming parameters that internally would call a method with 3 internal parameters, and you would simply default the third parameter.  In my case, I’m going the other way, which is unusual, but not unheard of.)

I began this post to brag about yet another awesome ReSharper refactoring because I really just wanted to add a parameter to an existing method call and it worked perfect.  It was not until I decided to show the other refactoring for this blog post did I run into the problem. 

I’m going to post this to the refactor dev’s and see if they can let me know where either I’ve gone astray, or there is a problem in their code.  At any rate, I’m running this on vs2008 and have not tried it on vs2010.

Hope this helps!