Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ReSharper be trained to rename interface parameters?

Tags:

c#

resharper

I have an interface in some referenced third-party assembly, such as:

public interface ITextHandler
{
  void Handle(string text)
}

And in my own project, I take on specific meaning by inheriting this into my own interface:

public interface INameHandler : ITextHandler
{
}

When using a tool like ReSharper, I might start off with:

public class Foo : INameHandler
{
}

Resharper will show an error I can click on and "Implement Interfaces". The result yields:

public class Foo : INameHandler
{
  public void Handle(string text)
  {
  }
}

Is there any way to tell ReSharper that I want to rename the parameter to be more specific? I'd like it to implement "name" instead of "text":

public class Foo : INameHandler
{
  public void Handle(string name)
  {
  }
}

Is there a resharper comment or something I can add to INameHandler that redefines the default parameter name?

like image 679
Matt Johnson-Pint Avatar asked Dec 21 '22 00:12

Matt Johnson-Pint


1 Answers

I would suggest that you don't do this. Indeed, just last night I ended up going to some effort to put my parameter names back to the one declared in the interface (I suspect I changed my mind in the interface later).

Consider this code:

Foo handler = new Foo();
handler.Handle(name: "Hello");

Now consider this harmless-looking refactoring, on the grounds of preferring to declare a variable via the interface type instead of the concrete type:

INameHandler handler = new Foo();
handler.Handle(name: "Hello");

If your parameter names are different, you'll get a compile-time error. It can be worse than this - you could end up with code that compiles but changes meaning if you reorder existing parameter names. Nasty, nasty stuff.

I'm not saying this will happen - but it's a good reason (IMO) to be careful when choosing parameter names, particularly in interfaces, and sticking with those names in implementations.

(Hmmm... maybe I ought to write a little tool to find all violations of that... could be handy.)

like image 163
Jon Skeet Avatar answered Dec 29 '22 10:12

Jon Skeet