过时属性的用法

发布于 2024-09-15 01:05:23 字数 703 浏览 12 评论 0原文

最近有人告诉我,在代码中使用 [已过时] 属性

[Obsolete("This property is obsolete. Use NewProperty instead.", false)]
public string OldProperty { get; set; }

[Obsolete("This method is obsolete. Use NewMethod instead.", false)]
public void OldMethod()
{
    // Method implementation
}

这些方法是我们代码库的内部方法,而不是 API 上的方法。这些方法处理较旧的加密函数。

我认为这是一种快速、安全的方式,可以向团队其他成员表明不应使用这些方法,并提供一条消息来建议替代方案。

其他人认为我应该完全删除这些方法,根据需要重写或重构现有代码。此外,人们认为很容易忽视编译器警告。

当第三方不使用代码时,是否有将代码标记为“过时”的“最佳实践”?或者这在很大程度上是主观的?

I was recently told it was bad practice to haved marked a number of methods in our code with the [Obsolete] attribute.

[Obsolete("This property is obsolete. Use NewProperty instead.", false)]
public string OldProperty { get; set; }

[Obsolete("This method is obsolete. Use NewMethod instead.", false)]
public void OldMethod()
{
    // Method implementation
}

These methods were internal to our codebase, rather than being on an API. The methods handled an older encryption function.

I felt it was a quick and safe way to denote to the rest of the team that these methods should not be used, and provided a message to suggest alternatives.

Others felt that I should have removed the methods entirely, rewriting or refactoring existing code as required. Additionally, it was thought too easy to overlook the compiler warnings.

Is there a 'best practice' for marking code as Obsolete when it's not being used by 3rd parties? Or is this largely subjective?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小忆控 2024-09-22 01:05:43

这不是一个简单的案例。如果您从正在运行的应用程序中删除方法并重构代码,则可能会引入新的错误并破坏正在运行的应用程序。如果该应用程序是关键任务,那么影响可能会很大,并且会让公司花费大量资金,因此需要仔细规划和测试此类更改。在这种情况下,将方法标记为过时可能是值得的,它应该有助于防止人们在进一步的开发中使用它们,从而使最终的重构变得更容易。然而,如果应用程序不是任务关键型或者引入错误的可能性很低,那么如果您有时间,重构可能会更好。最终添加 [Obsolete] 属性有点像待办事项,其使用取决于许多因素。

It is not a straight forward case. If you remove methods from a working application and refactor the code you create the posibility that you will introduce new bugs and break a working application. If that application is mision critical the impact could be huge and cost the company a lot of money, changes like that would need to be carefully planned and tested. In that case marking methods as obsolete could be worth while, it should help prevent people from using them in further development thus making the eventual refactoring easier. If however the application is not mision critical or the potential for bugs being introduced is low then it may be better to refactor, if you have the time. Ultimately adding the [Obsolete] attribute is a bit like a todo and its use depends on many factors.

甜宝宝 2024-09-22 01:05:40

当我们有旧代码需要重构最终但不是紧急时,我以前曾将它用作一种临时状态。最常见的情况是,编写了一些新代码,比之前的代码更好地完成了工作,但团队中没有人有时间回去替换大量旧代码。显然,这意味着无法立即进行简单的直接替换(有时新代码几乎可以完成旧代码所做的所有事情,但还有一小部分功能尚未实现) )。

然后,所有这些编译器警告都会持续提醒某人在有一点空闲时间时返回并完成重构。

这到底是好事还是坏事是相当主观的。与其他工具一样,它也是一个工具。

I've used it before as sort of a temporary state of affairs when we've got old code that needs to be refactored eventually but not urgently. Most often this is the case when some new code has been written that gets the job done better than what came before it, but nobody on the team has the time to go back and replace a lot of old code at the moment. Obviously this implies a situation where a simple drop-in replacement is not immediately possible (sometimes the new code does almost everything the old code did, but there's a small bit of functionality that hasn't been implemented yet).

Then having all those compiler warnings sitting around is a constant reminder for someone to go back and finish up the refactoring when he's got a little bit of free time.

Whether this is really a good or bad thing is pretty subjective. It's a tool, like any other.

旧竹 2024-09-22 01:05:38

这取决于。是的,您可以重构代码。您可以...吗?

问题是——你可以在一个程序内重构。如果 API 公开,并且您根本无法使用您的 API 重构代码,那么事情就会困难得多。这就是 Obsolete 的用途。

如果 API 是代码内部的,那么重构就是正确的选择。清理代码,不要留下混乱。

但如果公共 API 发生变化,如果可能的话,应该慢慢进行。

其余的仍然是主观的。我不喜欢内部 API 的“过时”。

It depends. Yes, you COULD refactor the code. COULD YOU?

The problem is - youCAN refactor WITHIN ONE PROGRAM. It is a lot harder if the API is out in the public and you simply CAN NOT refactor code using your API. This is what Obsolete is made for.

if the API is internal to your code, then refactoring is the way to go. CLean up code, do not leave a mess.

But if the public API changes, it should - if possible - be done slowly.

The rest is still subjective. I do not like "Obsolete" for internal API's.

ぶ宁プ宁ぶ 2024-09-22 01:05:37

我认为这是主观的。如果它是内部的并且是一个相当快的过程,那么我会执行更改。

但是,我也遇到过相应的重构花费了更长的时间(整个代码库中的许多调用)的情况,在这种情况下我使用了 [Obsolete] 属性。在这种情况下,新的开发将使用新功能,并且任何有时间的人都会进行重构,直到所有调用都消失,这意味着可以删除该方法。

I would think it is subjective. If it is internal and is a fairly quick process, then I would perform the change.

However, I've also had the situation where the corresponding refactoring took a lot longer (many calls throughout the code base), in which case I used the [Obsolete] attribute. In this case, new development would use the new functions and whoever had time performed refactorings until all calls were gone, which meant that the method could be removed.

掌心的温暖 2024-09-22 01:05:34

步骤 1. 将成员或类标记为 [Obsolete]

步骤 2. 更新成员或类的所有内部使用,以使用替换过时方法的新方法,或将该成员或类本身标记为 [Obsolete]

步骤 3.如果您在步骤 2 中将新内容标记为 [已过时],请根据需要重复此步骤。

步骤 4. 删除所有既不是公共的也不是被过时的公共成员或类使用的过时的成员和类。

步骤 5. 更新文档以更清晰地描述建议替换任何公共过时成员或类的方法。

最后,您将不再有仅由内部代码使用的过时代码。没有什么可说的,你必须一次性完成所有这些;在每个阶段你都取得了进步。开始步骤 1 和结束步骤 5 之间的时间可能是 5 秒或 5 年,具体取决于许多因素(其中大多数与复杂性有关)。

顺便说一句,如果有人发现很容易忽略编译器警告,那么问题不在于[已过时]。然而,不要在代码中长时间保留此类调用(即尽快完成步骤 2)的一个原因是确保人们最终不会习惯编译器警告,因为它们是编译器警告的一部分。编译代码的习惯反应。

Step 1. Mark the member or class as [Obsolete]

Step 2. Update all internal uses of the member or class to either use the new approach that replaces the obsolete approach, or mark that member or class itself as [Obsolete]

Step 3. If you've marked new stuff as [Obsolete] in Step 2, repeat this step as needed.

Step 4. Remove all obsolete members and classes that are neither public nor used by an obsolete public member or class.

Step 5. Update documentation to give a clearer description of the approach recommended to replace any public obsolete members or classes.

At the end of this, you will have no obsolete code that is solely used by internal code. There's nothing to say that you have to do all of this in one go though; at each stage you have made progress. The time between starting step 1 and ending step 5 could be 5 seconds or 5 years, depending on many factors (most of them to do with complexity).

Incidentally, if someone finds it easy to ignore compiler warnings, the problem is not with [Obsolete]. However, one reason not to leave such calls in the code for long (that is, to have done as far as step 2 ASAP) is to make sure people don't end up becoming used to compiler warnings as they're part of the habitual response to compiling the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文