James Hughes James Hughes - 1 year ago 101
C# Question

When should I use the [Obsolete] attribute and when should I delete my code?

The function of

is essentially to stop a class/function from being used but still to maintain it in code for the record.

Is there any good reason why
should be used as opposed to just deleting, or commenting out the code. This question is even more relevant if you have source control so there is no point keeping the code for reference purposes as it will be in the SC.

I'm curious as to what is considered best practice?

Answer Source

It is used mostly for backwards compatibility, so when you do a new implementation of a functionality that has a different expected behaviour, any code using the old functionality will still work, but you make sure that new uses of your library uses the new implementation.

If you are maintaining a library that is being used by third parties, you should develop a road map of when and if the obsolete functionality is going to be removed. The if it's important, because many times you are just indicating that that function is no longer to be maintained and the new one should be used instead.

Internally, it can be used in refactors to replace poorly-implemented-but-working functionality in a gradual way. You mark it as obsolete and start working through the warnings until you see no more of them, then you can proceed to safely remove it.

Be aware that this is an opinion based on experience on renewing legacy code bases and there is no common consensus.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download