为什么在每一行 JavaDoc 上都有星号前缀?

Why an asterisk prefix on every single JavaDoc line?

在 Sublime 中 putting an asterisk before each and every line in a JavaDoc block? While it seems to be the encouraged and accepted convention, and I've finally succumbed to doing it, it certainly doesn't make my writing of documentation any faster (despite tools that assist in creating them, such as the DocBlockr plugin 背后的原因是什么。

多行注释中每行不超过 75 个字符的一个明显好处是,您的代码可以更轻松地共享,而无需对其进行调整,并且查看者无需滚动到右侧即可阅读你的文档。

但为什么要更进一步,还要有这个额外的约定呢?可以使用 optional 星号前缀在左侧填充空格...为什么 every 行?

我没有证据证明这一点,但我总是把它归因于那个时代,当时世界上的一切实际上都是黑色或白色(即黑色或绿色)。在那些时候,您无法轻易区分代码和注释。

这些星号前缀让识别文件的部分变得非常容易,您可以放心地忽略它们。

自 Java 1.4 起,前导星号是可选的。

您可以省略它们以在 Java文档评论中导出代码缩进示例。

文档是这样说的:

Leading asterisks - When javadoc parses a doc comment, leading asterisk (*) characters on each line are discarded; blanks and tabs preceding the initial asterisk (*) characters are also discarded. Starting with 1.4, if you omit the leading asterisk on a line, the leading white space is no longer removed. This enables you to paste code examples directly into a doc comment inside a <PRE> tag, and its indentation will be honored. Spaces are generally interpreted by browsers more uniformly than tabs. Indentation is relative to the left margin (rather than the separator /** or <PRE> tag).

http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javadoc.html#leadingasterisks

这是一种风格约定……虽然 javadoc 命令确实(显然)在某些情况下以不同方式处理前导 * 和无前导 * 的情况。

Sun Java Style Guidelines 描述了第 5.2 节中的约定。

为什么?好吧,真正的答案只能由开发 Java 风格指南的人提供。

不过,我猜他们认为这会使 javadoc 注释更加突出。


Padding spaces to the left side could be done with an optional asterisk prefix..

呃……它可选的。您不必遵守约定,除非您的项目的样式指南要求您遵守。

It would nice to know why I have to spend an extra keypress or three on every single line of documentation I have ever and will ever write.

询问开发您正在使用的 IDE 的人:-)