tags:

views:

771

answers:

7

I have seen these being used every which way, and have been accused of using them the wrong way (though in that case, I was using them that way to demonstrate a point).

So, what do you think are the best practices for employing Extension Methods?

Should development teams create a library of extension methods and deploy them across various projects?

Should there be a collection of common extension methods in the form of an open source project?

Update: have decided to create an organization wide extension methods library

+2  A: 

I think that it depends on what purpose the Extension methods serve.

  • Extension methods that relate to specific business needs of a project (whether they are connected to basic data types or custom objects) should not be included in a library that would be distributed across multiple projects.
  • Extension methods that relate to basic data types (int, string, etc) or generics that have a wider application could be packaged and distributed across projects.

Take care not to globally include Extension methods that have little application, as they just clog up intellisense and can lead to confusion and/or misuse.

Yaakov Ellis
A: 

Well yes. That is right.

But should there be guidelines more specific. (for example, should one extend the object class?)

Should you extend third party libraries?

Vaibhav
+2  A: 

I've been including my extension methods in with my Core libraries in the Utils class because people who are working with my framework are likely to find the methods useful, but for mass deployment where the end developer might have a choice of extension method libraries, I would advise putting all of your extensions into their own namespace, even their own project file, so that people can choose to add a reference or a using statement or simply where required, like so:

Core.Extensions.Base64Encode(str);

My Utils class is my bestest friend in the whole world, it was before extension methods came along and they have only helped to strengthen our relationship. The biggest rule I would go by is to give people choice over what extension framework they are using where possible.

tags2k
+3  A: 

you might want to take a look at http://www.codeplex.com/nxl and http://www.codeplex.com/umbrella which are both extension method libraries. I personally haven't had a look at the source code but I'm sure the guys there would be able to give you some good pointers.

lomaxx
+2  A: 

The Objective-C language has had "Categories" since the early 1990s; these are essentially the same thing as .NET Extension Methods. When looking for best practices you might want to see what rules of thumb Objective-C (Cocoa & NeXT) developers have come up with around them.

Brent Simmons (the author of the NetNewsWire RSS reader for Mac OS X and iPhone) just posted today about his new style rules for the use of categories and there's been a bit of discussion in the Cocoa community around that post.

Chris Hanson
+4  A: 

The upcoming release of the Framework Design Guidelines, 2nd Edition will have some guidance for implementing extension methods, but in general:

You should only define extension methods "where they make semantic sense" and are providing helper functionality relevant to every implementation.

You also should avoid extending System.Object as not all .NET languages will be able to call the extension method as an extension. (VB.NET for instance would need to call it as a regular static method on the static extension class.)

Don't define an extension method in the same namespace as the extended type unless you're extending an interface.

Don't define an extension method with the same signature as a "real" method since it will never be called.

Scott Dorman
+9  A: 

I would recommend that you check out the following blog article:

http://blogs.msdn.com/vbteam/archive/2007/03/10/extension-methods-best-practices-extension-methods-part-6.aspx

It has some guidelines for how to use extension methods.

I wrote it back when I used to work for Microsoft, where I was the guy on the VB compiler team that implemented extension methods.

-Scott

Scott Wisniewski