.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

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube