DCSIMG
Extension Methods Coding Convention - Technicals and Technicalities

Technicals and Technicalities

Ariel's uneditable Bliki

Extension Methods Coding Convention

Hey there. Me again. That annoying overexcitable programmer. An I sill LOVE Extension methods.

For the past couple of months I’ve experimented with a convention of placing the static class which declares an extension method in the same namespace as the extended type. I didn’t like having to look for extension methods when I wanted to use them (and even Resharpers “Add using” was less than perfect with extension methods). The fact is - if you have an extension – you need to be able to access it with ease. And what’s easier than always having it in Intellisense, just like any other method?

I’ve bounced this idea off my Israel Alt.Net peers, and Amir has noted that this is not future-proof, because someone could add a similarly-named method to the type, and on the next re-compile – invocations will go to it. If the methods would have been declared at another NS – at least I would have gotten a “redundant Using statement” warning from Resharper, and try to find out why.

1) What’s the deal with this lenient compiler? Why is this legal? Throw some compilation errors my way!

2) He’s right.

 

So I’ve come up with a naming standard that all extension methods will end with an X: the classic “string.IsNullOrEmpty(stringInstance)” will be “stringInstance.IsNullOrEmptyX()”.

This will solve the possible future naming conflict, allow me to declare extension methods in the same NS, and still wouldn’t really bother me when typing the methods name (since it is just a suffix).

I said I will try it out for a while and then decide.

 

A while passed and I like it.

 

The reflex-comment about “Hungarian notations are evil” bounces right off me. I think in this case – the fact that I know that some method is an extension does actually help me. Reading code (without an accessible F12 key) could be confusing if you see a method that “you don’t remember ever seeing before on that type”.

One exception to the rule could be extension methods for System.Object. I would not add those extension methods under System, because then they would affect everything. Even if you wrote a perfectly useful method (I had fun with a myObj.GetPrivateField<T>(“_MyField”) method once) – this would cause too much clutter for everyone else. This is the sort of method I would keep in the standard “YourProject.Infra.Extensions” namespace.

An exception to that exception is, of course, if you have a limited scope to begin with – this would not clutter anything: say you have (borrowed from SpecUnit) and assertion method like “myObj.ShouldNotBeNull()” – you could still declare it under System (in your tests project), because it will only be seen in the tests project, where it’s logical to have it everywhere.

Comments

Avi Pinto said:

overexcitable indeed ;)

Your solution might only postpone the problem, since other developers, that will read this post and decide to follow your convention, will also call the function

IsNullOrEmptyX ...

You can always make it internal if it is relevant only for your scope

# October 7, 2009 7:51 PM

Ariel said:

Avi,

Using it is not a bad thing. Just like I would like developers to use any Infra class I write - I'd like them to use (or just know about) my extension method.

Discoverability is the point.

# October 7, 2009 8:35 PM

Avi Pinto said:

If you want to also expose your extension methods then you can put it under the main namespace of the component - when using the component, the developers will add it's namespace - which will expose all the goodies

# October 7, 2009 8:56 PM

Yair Cohen said:

Totally agree with Avi, I think it is not the wrong solution, but the wrong problem... maybe laziness? I just put them in the graceful namespace of my company (Mobideo) and by the naming conventions, almost all our code is or under Mobideo. or using it and it is discoverable. I agree that it has some hassles when intellisense does not recoginze it... but this is the rare case.

# October 8, 2009 9:40 AM

Ariel said:

Avi -

Well, that was the point of this post. The initial promise was to put extension methods in the namespace of the extended type...

Yair -

"The wrong problem" does not compile. :)

I have a problem which is the discoverability of my extension methods.

A solution to that problem could be placing them in the root namespace of your project, but let's face it - it's a hack. They don't really "belong" there. And if you're willing to put them in a namespace other than your run-of-the-mill "MyProj.Infra.Extensions" namespace - why not the extended types namespace?

# October 8, 2009 10:59 AM

Avi Pinto said:

Ariel -

I know it was the point of this post, but it might be a wrong point :-)

If you put them at the the extended types namespace you go back to the original problem that Amir raised(unless they are internal, which is not what you wanted in the first place).

and putting it under a separate namespace MyProj.Infra.Extensions only hides them.

# October 8, 2009 1:30 PM

Ariel said:

But now that I've suffixed my method with an X - it's more than probable that no-one will create a method called like that.

Unless you extend the class Bicycle with the method BMX().

# October 8, 2009 3:57 PM

Avi Pinto said:

maybe you should put a GUID instead of the mighty X,

more powerful ain't it?

# October 8, 2009 4:37 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 9, 2009 10:31 AM

Dew Drop – October 11, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop &#8211; October 11, 2009 | Alvin Ashcraft&#039;s Morning Dew

# October 11, 2009 5:26 PM

Technicals and Technicalities said:

Me like! &#160; int sleepTime = 1.Seconds(); while (condition) { &#160;&#160;&#160; Thread.Sleep(sleepTime

# December 14, 2009 12:21 PM

L100 Chandelier, L100 Coupon Gift Card said:

Pingback from  L100 Chandelier, L100 Coupon Gift Card

# May 21, 2010 3:26 PM

Phaeton Price Luxury, Volkswagen Phaeton Boiler Worcester said:

Pingback from  Phaeton Price Luxury, Volkswagen Phaeton Boiler Worcester

# May 22, 2010 9:01 AM

Ce500g 12tc Express Series, E500 Sell Come said:

Pingback from  Ce500g 12tc Express Series, E500 Sell Come

# May 22, 2010 10:56 AM

2003 350z Horsepower, Lexus Ls400 Replacement Parts Power Steering Pump said:

Pingback from  2003 350z Horsepower, Lexus Ls400 Replacement Parts Power Steering Pump

# May 22, 2010 1:37 PM

C230 Accessories Gps, Mercedes C230 Replacement Parts Brake Pads said:

Pingback from  C230 Accessories Gps, Mercedes C230 Replacement Parts Brake Pads

# May 22, 2010 4:12 PM

F59 Discount Replacement Step Bumper, Replacement Super Duty Ford F59 said:

Pingback from  F59 Discount Replacement Step Bumper, Replacement Super Duty Ford F59

# May 22, 2010 9:42 PM

Cb3000 Video Used Locator Concession Trailers, B300 Bulb Sae Approved Oe Replacement said:

Pingback from  Cb3000 Video Used Locator Concession Trailers, B300 Bulb Sae Approved Oe Replacement

# May 23, 2010 5:14 AM

Passport Children Birth Certificate, Uk Passport Requirements Usa - 412.unlockiphone30.net said:

Pingback from  Passport Children Birth Certificate, Uk Passport Requirements Usa - 412.unlockiphone30.net

# May 25, 2010 1:54 AM

2000 - 1996 @ Oldsmobile Toronado Cutlass Supreme, 1970 Oldsmobile Ninety Eight - 407.dlmreza.net said:

Pingback from  2000 - 1996 @ Oldsmobile Toronado Cutlass Supreme, 1970 Oldsmobile Ninety Eight - 407.dlmreza.net

# May 27, 2010 12:31 PM

2000 - 1991 @ Cougar Discount Lowest Toy Hauler Fifth Wheels, Chrysler Fifth Avenue Parts Online Replacement Headlight Assembly - 397.luna-atra.net said:

Pingback from  2000 - 1991 @ Cougar Discount Lowest Toy Hauler Fifth Wheels, Chrysler Fifth Avenue Parts Online Replacement Headlight Assembly - 397.luna-atra.net

# May 27, 2010 1:50 PM

Try-Catch-Finally .NET » Extension Method Naming Conventions Best Practices said:

Pingback from  Try-Catch-Finally .NET   &raquo; Extension Method Naming Conventions Best Practices

# September 17, 2010 7:34 PM

cb3000 said:

i love cb6000 too.

http://www.spicesforlove.com/

# January 23, 2011 3:49 PM

ToratordDrale said:

Acemaster53|I saw thisп»ї LOOOONG before Meekakitty posted hers since I subscribe to both Tessa and Kristina. Meeka s is good but this is way more creative.

 911canadianmeds.com

thplousaqq

# February 25, 2011 10:59 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: