Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

obsolete

The function of [Obsolete] 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 [Obsolete] 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?

like image 219
Persistence Avatar asked Nov 01 '16 13:11

Persistence


People also ask

What is an obsolete attribute?

Obsolete attributes are used to display an error or warning during compilation with an optional message to alert the developer that the given type or its member should not be used in the code as it is going to be replaced.

What is obsolete code?

Obsolete code: Code that may have been useful in the past, but is no longer used, e.g. code to use a deprecated protocol.

How do you mark obsolete code?

In you examples the "Method1 is deprecated" part is rather redundant. By marking it as obsolete you are saying that it is indeed obsolete, so no need to restate it in the message. Especially since the resulting warning/error will read 'Method1' is obsolete: 'Method1 is deprecated, please use Method2 instead.

How do you you specify that a method is obsolete in C#?

The Obsolete attribute decorates a program element by putting the word “Obsolete” above it inside square brackets. Since it is an attribute, we can use either Obsolete or ObsoleteAttribute. [Obsolete] − is a no parameter constructor and is a default using this attribute.


2 Answers

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.

like image 108
Yeray Cabello Avatar answered Oct 04 '22 14:10

Yeray Cabello


The Obsolete attribute marks a program entity as one that is no longer recommended for use. Each use of an entity marked obsolete will subsequently generate a warning or an error, depending on how the attribute is configured.

Here an example for Hashtable comparer from reference source.

    [Obsolete("Please use KeyComparer properties.")]        
    protected IComparer comparer
    {
        get
        {
            if( _keycomparer is CompatibleComparer) {
                return ((CompatibleComparer)_keycomparer).Comparer;
            }    
            else if( _keycomparer == null) {
                return null;
            }                            
            else {
                throw new ArgumentException(Environment.GetResourceString("Arg_CannotMixComparisonInfrastructure"));
            }                
        }
        set
        {
            if (_keycomparer is CompatibleComparer) {
                CompatibleComparer keyComparer = (CompatibleComparer)_keycomparer;
                _keycomparer = new CompatibleComparer(value, keyComparer.HashCodeProvider);
            }
            else if( _keycomparer == null) {
                _keycomparer = new CompatibleComparer(value, (IHashCodeProvider)null);               
            }                
            else {
                throw new ArgumentException(Environment.GetResourceString("Arg_CannotMixComparisonInfrastructure"));
            }
        }
    }
like image 40
mybirthname Avatar answered Oct 04 '22 14:10

mybirthname