最佳实践删除未使用的代码 [英] Best Practices for Removing Unused Code

查看:90
本文介绍了最佳实践删除未使用的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是什么人的最佳实践是删除未使用的代码。我个人是删除(不只是评论)任何不是当前正在使用的粉丝。但我不确定能走多远。

I'd like to know what people's best practices are for removing unused code. Personally I'm a fan of deleting (not just commenting) anything that's not currently being used. But I'm unsure of how far to go.

以此为例子(尽管我感兴趣的一般性讨论)。在项目中,我有一个十几用户控件。对于后来被解职了一个功能,我实现了几个方法和属性的用户控件中的一个。额外的代码是不特定的功能,但需要支持它。它可能潜在有用以后。

Take this as an example (although I'm interested in general discussion). In my project I have a dozen or so UserControls. For a feature that later got canned, I implemented a couple of methods and properties on one of the UserControls. The additional code is not specific to the feature, but needed to support it. It may potentially be useful later on.


  • 因为我们不是现在使用它,我应该删除代码,而更少的代码有越容易阅读?这里的问题是,如何做未来的开发人员都知道,这项工作已经完成?

  • 或者我应该保持它的出现,让其他开发人员可以很容易地找到它,如果他们需要使用它后来(他们不会想到去通过源头控制,看是否有人已经做到了这一点,并删除了它)?

  • 或者是有另一种选择?

这同样适用于当前未使用用户控件。 ?我是否应该删除它们,或让他们

The same applies to UserControls not currently being used. Should I remove them or keep them?

编辑:不言而喻,我们正在使用源代码控制(或我认为它会)

It goes without saying (or I thought it would) that we're using source control.

推荐答案

要记住的第一件事是,所有的代码应该在源代码控制

The first thing to remember is that all your code should be in source control.

考虑到这一点,当然要删除过时的代码,而不是仅仅把它注释掉。任何长度的注释代码块只是危险的,至少有两个原因:

With that in mind, of course you want to delete obsolete code rather than just comment it out. Commented code blocks of any length are just dangerous, for at least two reasons:


  1. 有假设的意见维持与趋势代码的其余部分。这是不正确的,可能会导致类似的错误回归的问题。

  2. 这很容易在很长块的中间错过的注释掉花括号(例如)。

删除的代码仍然是可用的,如果你真的需要它,但它不再是塞满了你的工作拷贝。如果你的真正的关注曝光率旧代码,你可以发表评论,表明代码被删除,您需要找到它的版本号。在第一行,这比实际的代码块是用什么好了很多,它的更清楚,该代码已被允许经过,究竟它是如何长的失效。

The deleted code is still available if you really need it, but it's no longer cluttering up your working copies. If you're really concerned about discoverability for the old code you can leave a comment indicating that code was removed and the revision number you need to find it. At one line, that's a lot better than what the actual code block was using, and it's much clearer that this code has been allowed to lapse, and exactly how long it's lapsed.

这篇关于最佳实践删除未使用的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆