How can you mark code as “not for future use”

后端 未结 4 2527
你的背包
你的背包 2021-02-19 17:04

I often end up in a situation where I want to discourage other developers from continuing to use a method or class. For example, let\'s say I have two library methods \"A\" and

相关标签:
4条回答
  • 2021-02-19 17:35

    So you want a warning, but without any warnings?

    The main problem here, is there's nothing that upon compilation distinguishes "old code, before we thought better of it" from "new code, that shouldn't use the old habits"; it's all just code.

    About the only thing you could do is to use the ObsoleteAttribute and then use #pragma warning disable 612, 618 on the current uses. As always, #pragma warning should never exist without a comment:

    #pragma warning 612, 618 //This block of code uses BadOldMethod(), code review planned
    /* ... code here */
    #pragma warning restore 612, 618
    

    Of course, if there's a good reason to stop using it, there's a good reason to make that review sooner, rather than later.

    Edit: Oops, I forgot about 612 as well as 618. You can set the attribute to raise 619 instead of 618, but that can't be disabled (one of the main reasons for setting it, is that that sometimes suits).

    A further discouragement can come from marking the member as [EditorBrowsable(EditorBrowsableState.Never)]. The fact that the method won't show up in intellisense at all, while the new one will, would encourage people toward the new one (as long as the library is referenced as a library rather than as a project within the solution, or a class within the same project).

    0 讨论(0)
  • 2021-02-19 17:37

    Use ObsoleteAttribute.

    [ObsoleteAttribute("This method is obsolete. Call NewMethod instead.", false)] 
    public string SomeObsoleteMethod()
    {
       // ...
    }
    

    The last argument (IsError) will emit a compilation error if set to true, otherwise a warning will be given. You can disable the warnings by using #pragma 612, 618

    EDIT:

    Ok, fine, I relent. The solution you want appears to be:

    /// <summary>
    /// Please don't use
    /// </summary>
    public string SomeObsoleteMethod()
    {
       // ...
    }
    

    No compiler support at all.

    0 讨论(0)
  • 2021-02-19 17:40

    Adding the Obsolete attribute to your method will give the strikethrough in intellisense.

    [ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)] 
    public static string OldProperty
    { get { return "The old property value."; } }
    

    To disable the warnings add this before the attribute:

    #pragma warning disable 612, 618
    

    And to reenable:

    #pragma warning restore 612, 618
    

    As noted here, putting a ignore in your project file instead of in your code would be a very clean solution.

    <WarningsNotAsErrors>618</WarningsNotAsErrors>
    

    EDIT: Also, check out @JonHanna's answer about using the EditorBrowsable attribute.

    As others have noted, there are actually 2 warnings that are thrown with the obsolete attribute.

    EDIT:

    #pragma warning disable 612, 618
    [Obsolete]
    #pragma warning restore 612, 618
    public class test1
    {...
    

    When you try to use test1 you will get:

    enter image description here

    Note that when you type var test = new test1() the strikethrough does not occur.

    But test1 test = new test1() will show the strikethrough.

    0 讨论(0)
  • 2021-02-19 17:45

    Personally I think that you should use ObsoleteAttribute, making sure that you use #pragma (see here for examples) to disable it where needed in the existing code.

    In time you fix the code.

    0 讨论(0)
提交回复
热议问题