13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Usage ideas and guidel<strong>in</strong>es273those who are go<strong>in</strong>g to use and ma<strong>in</strong>ta<strong>in</strong> your code. So, you need to consult with therelevant people as far as you can: this depends on your type of project and its audience,of course, but it’s nice to present different options and get appropriate feedback.Extension methods make this particularly easy <strong>in</strong> many cases, as you candemonstrate both options <strong>in</strong> work<strong>in</strong>g code simultaneously—turn<strong>in</strong>g a method <strong>in</strong>to anextension method doesn’t stop you from call<strong>in</strong>g it explicitly <strong>in</strong> the same way as before.The ma<strong>in</strong> question to ask is the one I referred to at the start of this section: is the“what does it do” of the code more important than the “how does it do it?” That variesby person and situation, but here are some guidel<strong>in</strong>es to bear <strong>in</strong> m<strong>in</strong>d:■■■■■■■■■Everyone on the development team should be aware of extension methods andwhere they might be used. Where possible, avoid surpris<strong>in</strong>g code ma<strong>in</strong>ta<strong>in</strong>ers.By putt<strong>in</strong>g extensions <strong>in</strong> their own namespace, you make it hard to use them accidentally.Even if it’s not obvious when read<strong>in</strong>g the code, the developer writ<strong>in</strong>g itshould at least be aware of what she’s do<strong>in</strong>g. Use a projectwide or companywideconvention for nam<strong>in</strong>g the namespace. You may choose to take this one step furtherand use a s<strong>in</strong>gle namespace for each extended type. For <strong>in</strong>stance, you couldcreate a TypeExtensions namespace for classes that extend System.Type.The decision to write an extension method should always be a conscious one. Itshouldn’t become habitual—certa<strong>in</strong>ly not every static method deserves to be anextension method.An extension method is reasonably valid if it’s applicable to all <strong>in</strong>stances of theextended type. If it’s only appropriate <strong>in</strong> certa<strong>in</strong> situations, I’d make it clearthat the method is not part of the type by leav<strong>in</strong>g it as a “normal” static method.Document whether or not the first parameter (the value your method appearsto be called on) is allowed to be null—if it’s not, check the value <strong>in</strong> the methodand throw an exception if necessary.Be careful not to use a method name that already has a mean<strong>in</strong>g <strong>in</strong> theextended type. If the extended type is a framework type or comes from a thirdpartylibrary, check all your extended method names whenever you change versionsof the library.Question your <strong>in</strong>st<strong>in</strong>cts, but acknowledge that they affect your productivity. Justlike with implicit typ<strong>in</strong>g, there’s little po<strong>in</strong>t <strong>in</strong> forc<strong>in</strong>g yourself to use a featureyou <strong>in</strong>st<strong>in</strong>ctively dislike.Try to group extension methods <strong>in</strong>to static classes deal<strong>in</strong>g with the sameextended type. Sometimes related classes (such as DateTime and TimeSpan) canbe sensibly grouped together, but avoid group<strong>in</strong>g extension methods target<strong>in</strong>gdisparate types such as Stream and str<strong>in</strong>g with<strong>in</strong> the same class.Th<strong>in</strong>k really carefully before add<strong>in</strong>g extension methods with the same extendedtype and same name <strong>in</strong> two different namespaces, particularly if there are situationswhere the different methods may both be applicable (they have the samenumber of parameters). It’s reasonable for add<strong>in</strong>g or remov<strong>in</strong>g a us<strong>in</strong>g directiveto make a program fail to build, but it’s nasty if it still builds but changesthe behavior.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!