开发者

Should extension methods have a prefix?

开发者 https://www.devze.com 2022-12-21 00:04 出处:网络
From what I have read about extension methods, you will run into problems if the base class decides to add a method with the same name as your extension. For a specific class it is generally not to ha

From what I have read about extension methods, you will run into problems if the base class decides to add a method with the same name as your extension. For a specific class it is generally not to hard to pick a name to avoid clashes, however extension methods can be added to interfaces which adds infinitely more potential for conflicts.

In Objective-C (with their version, categories), this problem is avoided by adding a prefix before each method. I know we can define extension methods in namesp开发者_如何学Caces so that we can control whether they are imported or not, but this only solves the problem of clashes between extension methods, rather than clashes between an extension method and the base class.

Update:

Nobody, actually mentioned this, but extension methods aren't virtual. That means if you can i.myExtension() on an interface i, then it will always call the interface classes method, so the subclass method (which could have different intent) won't be called. So, overall, using extension methods is quite safe.


The use of a prefix will make the code ugly, which removes some of the value of an extension method.

I recommend instead, being careful to not create a large number of extension methods and to use method names that make sense. This way, in the case of a conflict, it's more likely that, for instance, the extension Where method and the conflicting Where method will have similar semantics.


The common convention is to have them in their own separate namespace (usually in the form of <something>.Extensions) which you can then use to decide whether or not you use them in any particular piece of code which contains types the extensions normally operate on.

Furthermore, if you do have a conflict, remember that extension methods are still static methods, so you can always call them explicitly in the case of a name conflict.

However, if you find that you are frequently running into name conflicts, you might want to reconsider the names you are choosing so that they don't interfere with names that already exist on the type.

To summarize, no, you should not prefix extension method names in C#, as they only serve to obfuscate your code and make it harder to maintain.


If the base class implements a method with the same name as your extension, I'd guess you'd have one of these two scenarios:

  1. The base class implemented the same functionality you were adding yourself with an extension method. You no longer need the extension method. Delete it and use the method on the base class instead.
  2. The base class implemented something different from what you wanted but gave it the same name. Either you or they are using the wrong name. Probably you. Rename your extension method to something that describes what the function actually does.


I'm inclined to say no they shouldn't unless you have reason good to suspect that the method signature you are using is going to be implemented. The reason i think this is...

  • It's is quite unlikely that your method signature will be duplicated in most cases.
  • If it does, it should always be detected in unit tests.
  • Should be trivial to fix.

I normally opt for relatively verbose method names when implementing extension methods anyway to avoid confusion.

0

精彩评论

暂无评论...
验证码 换一张
取 消