java - 我的代码的每个部分真的都需要 JavaDoc/Comments 吗?

标签 java comments javadoc checkstyle

<分区>

我正在为我在 uni 的最后一年 CS 本科项目编写一个 Java 应用程序。在前几年,我们被告知必须对我们的代码使用 CheckStyle。但是,我发现使用默认的 CheckStyle 配置需要我为每件事编写一个 JavaDoc。即使是名称和实际代码足以描述那段代码的作用的明显实例字段和方法,也需要我编写 JavaDoc 注释。

老实说,我发现这是重复的,只会让我的代码困惑。如果没有所有这些评论,我会发现我的代码更具可读性和更短,这让我在需要查看类等时可以更好地了解。

问题是,如果 checkstyle 将它突出显示为一个问题(缺少 JavaDoc)并且我们被告知毫无异常(exception)地使用 CheckStyle,那么我怀疑我会失去分数,这是我最不想看到的。我引用这本优秀的书: 马丁·罗伯特·C. (2008-08-01)。清洁代码:敏捷软件工艺手册(第 54 页)。培生教育(美国)。

The proper use of comments is to compensate for our failure to express ourself in code. Note that I used the word failure. I meant it. Comments are always failures. We must have them because we cannot always figure out how to express ourselves without them, but their use is not a cause for celebration. So when you find yourself in a position where you need to write a comment, think it through and see whether there isn’t some way to turn the tables and express yourself in code. Every time you express yourself in code, you should pat yourself on the back. Every time you write a comment, you should grimace and feel the failure of your ability of expression. Why am I so down on comments? Because they lie. Not always, and not intentionally, but too often. The older a comment is, and the farther away it is from the code it describes, the more likely it is to be just plain wrong. The reason is simple. Programmers can’t realistically maintain them.

我不能说我是多么全心全意地同意上述观点。这些评论在我眼里只是一团糟。在相关的地方,我有意见。

所以,我的问题归结为以下几点:我应该坚持自己的信念和阅读上述书籍获得的知识/建议,还是应该遵守 CheckStyle 标准并消除因缺乏而被打分的风险JavaDoc 评论?

最佳答案

你说:

However, I'm finding that using the default CheckStyle configuration requires me to write a JavaDoc for EVERY thing. Even obvious instance fields and methods where the name and the actual code is sufficiently descriptive of what that piece of code does, requires me to write JavaDoc comments.

还有:

I did ask my supervisor who said it would be best to stick to what Sun proposes i.e. the default checkstyle config.

这两种说法不一致。

这是实际的、传统的、古老的 Sun 编码约定文档(1999 年修订):

section on comments确实说:

Discussion of nontrivial or nonobvious design decisions is appropriate, but avoid duplicating information that is present in (and clear from) the code. It is too easy for redundant comments to get out of date. In general, avoid any comments that are likely to get out of date as the code evolves.

并不是说每个成员都需要评论。

我认为这很清楚地表明,“明显的实例字段”或“名称和实际代码足以描述该段代码的作用的方法”都不需要注释。在将 Sun 编码约定和默认的 Checkstyle 配置等同起来时,您的主管的指示或您对它的解释是错误的。

同样值得记住的是,Sun 编码约定是由编写 Java 本身的程序员编写的,并且是为这些程序员编写的。那是一种很特别的工作;他们不仅需要生成有效的代码,并且维护人员可以理解,而且因为他们正在制作一个标准库,该库将仅以二进制形式分发,这是可以理解的,并且在许多情况下确实通过签名和单独评论。我认为将 1990 年代为实现 Java 本身而开发的规则应用于今天编写的应用程序代码是非常愚蠢的。这就像按照利未记过你的生活。

关于java - 我的代码的每个部分真的都需要 JavaDoc/Comments 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13435684/

相关文章:

c# - 使用 C# Intellisense Comments 的最佳实践

comments - plon 4 中的自定义工作流程

android - 在 eclipse 中创建 javadoc

java - 记录可能更改的 API

java - 递归 MergeSort 和计数比较

java - 从列表中删除、添加和使用项目

Java 函数匹配无法识别数字 [0-9]+

php - 对于 Web 应用程序,将多个页面上的嵌套讨论分开的最佳方法是什么?

eclipse - 为什么我们无法从主网站下载 SWT 的 Javadocs jar 文件

java - 如何评论 (javadoc) NamedQueries