好的 Ruby 代码中没有注释是否可以接受?

发布于 2024-11-19 00:57:54 字数 1431 浏览 2 评论 0原文

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

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

发布评论

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

评论(2

古镇旧梦 2024-11-26 00:57:54

这个问题并不是 Ruby 独有的。

代码注释应保持在绝对最低限度,因为它们通常在代码更改时不会更新,并且变得更具误导性而不是有用性。

正如您已经建议的,最好的代码是自我记录的,不需要注释。

编辑:澄清一下,如果您无法减少代码以消除复杂性,则您必须提供注释。根据我的经验,这种情况很少见,通常仅在外部组件的行为不符合您的预期时才适用。

This issue isn't unique to Ruby.

Code comments should be kept to an absolute minimum, because they are usually not updated when the code changes, and become more misleading than helpful.

As you have already suggested, the best code is self-documenting and requires no comments.

Edit: to clarify, if you cannot reduce the code to remove the complexity, you must provide comments. This is rare in my experience, and usually only applies when external components don't behave as you might expect.

冷夜 2024-11-26 00:57:54

Ruby 的创建者 Matz 时不时地阐述了他的哲学:“源代码就是文档。它甚至正确地指出了所有错误。”他对评论的意思可能也是如此。我想很多在Ruby源代码中不注释的人都是在听他的话。至于要不要追随他,就看你自己了。

我还读到一些 Ruby 介绍网站说,每当您觉得有必要发表评论时,这表明您应该像在单独的方法中那样拆分该例程,并按照您要评论该部分的方式命名它。

Matz, the creator of Ruby, states his philosophy here and there: "The source code is the documentation. It even states all the bugs correctly." And he probably means that for comments as well. I think many people who don't comment in Ruby source code are following his words. Whether for you to follow him is up to you.

I have also read some Ruby introductory websites saying that, whenever you feel the necessity to comment, that is an indication that you should split that routine as in individual method, and name it as you were to comment that part.

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