.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

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

  • Introduction to .NET Aspire

    Two Microsoft experts will present on the cloud-native application stack designed to simplify the development of distributed systems in .NET at the Visual Studio Live! developer conference coming to Las Vegas next month.

  • Microsoft Previews Copilot AI for Open-Source Eclipse IDE

    Catering to Java jockeys, Microsoft is yet again expanding the sprawling reach of its Copilot-branded AI assistants, previewing a coding tool for the open-source Eclipse IDE.

Subscribe on YouTube

Upcoming Training Events