Bugs, Workarounds, And Gotchas in VS 2008: Keeping Your Extension Methods Under Control

Extension methods have the capacity for misuse, but a few simple guidelines will let you leverage their power:

  1. Extension methods should handle null/Nothing for the first (extension) parameter in an appropriate way. It should only throw an exception if you want it to behave exactly like an instance method.
  2. When working with a tree, put the method on the class closest to the leaf. For example, instead of "GetStringAttribute" on XElement, create GetString that works directly with the XAttribute and returns an empty string or null if the attribute is null.
  3. Categorize extension methods as either organizational or project. Separate organizational and project extension methods into different assemblies. This will make it easier to reference correct extension methods for a given project. Be small grained on these assemblies.
  4. Keep extension methods in the same namespaces whether they are project or organizational. Either one namespace or namespaces that track the category of extension methods such as StringExtensions. This makes it easier to move extension methods from projects into organizational assemblies.
  5. Keep project extension methods with the same name in sync. The same name should not be used differently in different projects.
  6. Extension methods should have good names. Standard guidelines are Pascal cased, no underscores, each word starting with a capital, no abbreviations. Because they are methods, they should include a verb.
  7. Provide overloads if some parameters can be set to a default. Set a single order of parameters and pick the most logical overloads. Don't provide too many overloads.
  8. When providing a variation of existing framework functionality, use an overload of the same name or starting with the same characters. If you're creating parallel functionality (such as providing List behavior to System.Collections.ObjectModel.Collection) use the same signatures.
  9. Expect some work when you move to future .NET versions. This will occur if your signatures conflict. If you've worked logically and followed these guidelines, changes should be minimal.
comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube