在生产版本中保留注释掉的代码是一种不好的做法吗

发布于 2024-09-17 23:18:01 字数 161 浏览 10 评论 0原文

我经常看到开发人员(大公司和个人)提供的生产代码,其中包含已被注释掉的代码。据推测,这消除了早期尝试实现由于某种原因而不起作用的功能。

在我看来,这很混乱,但可能有一些好处,例如,在返回重构或扩展代码时,开发人员可以看到以前尝试过的内容。

这有什么安全或最佳实践方面的问题吗?

I regularly see production code from developers (large companies and individuals) that contains code that has been commented out. Presumably this removes earlier attempts at achieving the functionality that didn't work for some reason.

To my mind, this is messy, but potentially has some benefits e.g. on returning to refactor or extend the code, the developer can see what has been tried previously.

Are there any security or best-practice aspects to this?

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

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

发布评论

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

评论(3

失眠症患者 2024-09-24 23:18:01

最佳实践是使用 SCM。如果你认为旧代码确实是人们将来想要参考的东西,请留下评论“//我们曾经用另一种方式来做,它有有趣的属性X - 请参阅修订版103”,而不是留下整个块不执行任何操作的代码。

注释掉代码有它的作用,但这个地方是快速测试,甚至不值得花时间做分支。

如果代码值得保留,那么它比在某处的注释中丢失更有价值。如果不值得保留,就用火杀死它。

Best practice is to use SCM. If you think the old code is really something people will want to refer to in the future, leave a comment of "// We used to do it another way, which had interesting property X -- see revision 103" rather than leaving whole chunks of code that don't do anything.

Commenting out code has its place, but that place is quickie tests that aren't even worth the time to do a branch.

If the code is worth keeping, it's worth more than being lost in a comment somewhere. If it is not worth keeping, kill it with fire.

心凉怎暖 2024-09-24 23:18:01

最佳实践是您的代码仅显示当前的。您应该使用处理代码历史记录的 SCM。

Best practice is that your code shows only the current. You should be using an SCM that deals with code history.

西瓜 2024-09-24 23:18:01

代码应存储在源代码管理中。
应保留注释来解释难以理解的代码或执行不寻常操作的原因。

Code should be stored in source control.
Comments should be reserved to explain difficult to understand code or the reason for doing something out of the ordinary.

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