Tuesday, May 3, 2011

How to mark a class Deprecated?

How to mark a class deprecated? I do not want to use a class anymore in my project, but do not want to delete it beforea period of 2 weeks.

From stackoverflow
  • You need to use the attribute [Obsolete].

    This is an example:

    [Obsolete("Not used anymore",true)]
    public class MyDeprecatedClass
    {
     //...
    }
    

    You do not have use parameters, they are optional (overloaded method). The first parameter is for the reason and the last one is to mark an Error in compile time instead of a warning.

  • If you are using version control I would recommend just deleting the class. There is no reason to have unused code around.

    Version control will be a handy undo if you decide later that you want the class.

    shsteimer : I'm guessing/assuming he wants to mark it deprecated in order to allow the code to still compile in the meantime while he removes all references to it in the codebase.
    configurator : I agree, Why not delete the class, or if you don't have version control, comment it?
    jjnguy : That would be one great reason for not using my solution I guess.
    Mister Dev : I need to do it progressivly. I can't erase it from the project right now. Commenting a class or a method is hard to find it later... I do not want to forget about it. Deprecating a method has still is place I think.
    jjnguy : That makes sense. I would get rid of the class eventually though.
    Mister Dev : Yes it's my plan... read the question :P in 2 weeks :P
    Mister Dev : (I haven't voted you down)
    jjnguy : Downvotes are part of life. I don't harbor bad feelings about it. (but thanks)
    Michael Meadows : If you're developing a public API, you don't have the option of deleting an obsolete method or class. You often have to give your customers several releases to code away their dependency on your code.
    jjnguy : I guess I'm not thinking big-picture
  • The reason to not erase a class and deprecate instead is to adhere to some "politeness policies" when your code is an estabished API and then is consumed by third parties.

    If you deprecate instead of erase, you give consumers a life cycle policy (e.g., maintenance and existence of the classes until version X.X) in order to allow them to plan a proper migration to your new API.

0 comments:

Post a Comment