自动检查 java 代码的正确约定和文档

发布于 2024-11-09 12:05:22 字数 1539 浏览 0 评论 0原文

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

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

发布评论

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

评论(2

任谁 2024-11-16 12:05:22

您可以使用 Checkstyle。然而,标准检查(sun 编码标准)非常严格,您可能希望根据您的喜好从用于配置的 XML 中删除一些检查

You can use Checkstyle. However, the standard checks (sun coding standards) are quite strict and you probably may want to remove some checks from the XML that is used for configuration, depending on your taste

掩耳倾听 2024-11-16 12:05:22

虽然像 Checkstyle 这样的工具可以检查对样式约定的遵守情况,但它们不应该用于评估文档。是的,checkstyle 可以检查文档是否存在,但不能检查该文档是否正确或以任何方式有用 - 无用的文档比没有更糟糕,因为它至少不会使源代码变得混乱。例如,checkstyle 会考虑:

/**
 * Gets the amount.
 * @return the amount
 */
BigDecimal getAmount() {
    return amount;
}


/**
 * Sets the amount.
 * @param amount the amount to set
 */
void setAmount(BigDecimal amount) {
    this.amount = amount;
}

更好(因为存在注释并且所有参数和返回值都已记录),而不是

/**
 * @return the amount of this transaction (positive for deposits, negative for withdrawals)
 */
BigDecimal getAmount() {
    return amount;
}


/**
 * @see #getAmount()
 */
void setAmount(BigDecimal amount) {
    this.amount = amount;
}

因为后者没有记录方法参数......

简而言之,该记录是否足够不能通过机器,并且容易获得的指标仅显示了图片的一部分 - 在我看来,这很大程度上是不相关的部分。

While tools like Checkstyle can check adherence to style conventions, they should not be used to evaluate documentation. Yes, checkstyle can check that documentation is present, but not that this documentation is correct or in any way useful - and useless documentation is worse than none, since it at least doesn't clutter the source code. For instance, checkstyle would consider:

/**
 * Gets the amount.
 * @return the amount
 */
BigDecimal getAmount() {
    return amount;
}


/**
 * Sets the amount.
 * @param amount the amount to set
 */
void setAmount(BigDecimal amount) {
    this.amount = amount;
}

to be better (because comments exist and all parameters and return values are documented), than

/**
 * @return the amount of this transaction (positive for deposits, negative for withdrawals)
 */
BigDecimal getAmount() {
    return amount;
}


/**
 * @see #getAmount()
 */
void setAmount(BigDecimal amount) {
    this.amount = amount;
}

because the latter does not document the method parameter ...

In short, that documentation is adequate can not be verified by a machine, and easily obtainable metrics only show part of the picture - a largely irrelevant part in my opinion.

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