.NET Tips and Tricks

Blog archive

Warn Developers about Using Your Old Code

At least two or three times in my life, I've come back to old systems I'd worked on and realized that I now knew a better way of doing things. However, rather than just revise existing code, that "better way of doing things" required me to write a new method, property or class. When I released my new code out into the world, I wanted to tell other developers to stop using my old code and move to my new code. The best way to do that, I've decided, is to go back to the old code and decorate it with the Obsolete attribute.

You can add the Obsolete attribute either to a class or to members of a class. Regardless, when a developer attempts to use the item you've decorated, they'll get a warning message in their code that says the class (or the member) you've decorated with the attribute is deprecated.

Of course, developers aren't likely to stop using your class or method unless you tell them about your alternative. You can do that by passing a message to the Obsolete attribute -- that message is then tacked onto the end of the attribute's default warning message. This example will generate the message "'SampleClass' is obsolete: Use PHVIS.NewClass" when someone tries to use SampleClass:

<Obsolete("Use PHVIS.NewClass")>
Public Class SampleClass
  Public Sub SampleMethod()

  End Sub
End Class

By default, the Obsolete attribute just generates a warning message so it won't stop the developer's code from compiling. If you want to be more aggressive, you can pass True as the attribute's second parameter to generate a compile-time error and prevent the developer's code from compiling, as this example does:

Public Class SampleClass
  <Obsolete("Use BetterMethod", True)>
  Public Sub SampleMethod()

  End Sub
End Class

I'd wait awhile before doing that, though.

Posted by Peter Vogel on 12/17/2015


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