Can I teach ReSharper to rename interface parameters?

I have an interface in some related third-party assembly, for example:

public interface ITextHandler { void Handle(string text) } 

And in my own project, I take a specific value, inheriting this into my own interface:

 public interface INameHandler : ITextHandler { } 

When using a tool like ReSharper, I can start with:

 public class Foo : INameHandler { } 

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

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

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

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

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

+4
source share
1 answer

I would suggest that you do not. Indeed, it was only last night that I decided to make some effort to return the parameter names to the one declared in the interface (I suspect that I later changed my mind in the interface).

Consider this code:

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

Now consider this harmless prospective refactoring based on the assumption that you declare a variable through an interface type instead of a specific type:

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

If your parameter names are different, you will get a compile-time error. It may be worse than this - you can get code that compiles, but changes value if you reorder existing parameter names. Disgusting, unpleasant stuff.

I'm not saying that this will happen - but this is a good reason (IMO) to be careful when choosing parameter names, especially in interfaces, and stick to those names in implementations.

(Hmm ... maybe I should write a small tool to find all the violations of this ... maybe convenient.)

+11
source

Source: https://habr.com/ru/post/1403269/


All Articles