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

后端 未结 4 2496
你的背包
你的背包 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).

提交回复
热议问题