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.
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.
发布评论
评论(2)
这个问题并不是 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.
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.