“为什么” PMD规则背后

发布于 2024-08-26 01:50:38 字数 511 浏览 7 评论 0 原文

是否有一个很好的资源来描述 PMD 规则集背后的“原因”? PMD 网站有“什么”——每条规则的作用——但没有描述为什么 PMD有这条规则,以及为什么忽略这条规则会给你在现实世界中带来麻烦。特别是,我有兴趣知道为什么 PMD 具有避免实例化对象循环和只有一个返回规则(如果您需要创建与集合中的每个对象相对应的新对象,第一个似乎是必要的,第二个似乎在许多情况下是必要的根据某些标准返回一个值),但我真正想要的是一个描述大多数 PMD 规则背后的“原因”的链接,因为这种情况经常出现。

需要明确的是,我知道我可以禁用这些以及如何做到这一点,我只是想知道为什么它们首先存在。很抱歉,如果我错过了一些明显的东西,但在发布此内容之前我进行了谷歌搜索和SO搜索。我也明白这些问题通常是“品味”的问题 - 我正在寻找的是规则的论点是什么以及有哪些替代方案。举个具体的例子,如何在不实例化循环中的每个对象的情况下实现循环中每个对象对应的一个对象(这是Java中的常见操作)?

Is there a good resource which describes the "why" behind PMD rule sets? PMD's site has the "what" - what each rule does - but it doesn't describe why PMD has that rule and why ignoring that rule can get you in trouble in the real world. In particular, I'm interested in knowing why PMD has the AvoidInstantiatingObjectsInLoops and OnlyOneReturn rules (the first seems necessary if you need to create a new object corresponding to each object in a collection, the second seems like it is a necessity in many cases that return a value based on some criteria), but what I'm really after is a link somewhere describing the "why" behind a majority of PMD's rules, since this comes up often enough.

Just to be clear, I know that I can disable these and how to do that, I'm just wondering why they are there in the first place. Sorry if there's something obvious I missed out there, but I did a Google search and SO search before posting this. I also understand that these issues are often a matter of "taste" - what I'm looking for is what the argument for the rules are and what alternatives there are. To give a concrete example, how are you supposed to implement one object corresponding to every object in a loop (which is a common operation in Java) without instantiating each object in a loop?

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

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

发布评论

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

评论(4

心碎无痕… 2024-09-02 01:50:38

在每种情况下,规则都可以是具体情况的问题,也可以只是“品味”的问题。

如果存在大量迭代并且实例化成本高昂,则应避免在循环中实例化对象。如果您可以将代码移出循环,您将避免许多对象实例化,从而提高性能。话虽如此,这并不总是可能的,并且在某些情况下,它对代码的整体性能并不重要。在这些情况下,请选择更明确的方法。

对于 OnlyOneReturn,有多种方式可以看待这一点(每种方式背后都有强烈的支持者),但它们基本上都可以归结为品味。

对于您的示例,OnlyOneReturn 支持者希望使用如下代码:

public int performAction(String input) {
    int result;
    if (input.equals("bob")) {
        result = 1;
    } else {
        result = 2;
    }
    return result;
}

而不是:

public int performAction(String input) {
    if (input.equals("bob")) {
        return 1;
    } else {
        return 2;
    }
}

正如您所看到的,ReturnOnlyOnce 的额外清晰度是有争议的。

另请参阅与 相关的问题循环内实例化

In each case, the rule can be a matter of specific circumstances or just "taste".

Instantiating an Object in a loop should be avoided if there are a large number of iterations and the instantiation is expensive. If you can move the code out of the loop, you will avoid many object instantiations, and therefore improve performance. Having said that, this isn't always possible, and in some cases it just doesn't matter to the overall performance of the code. In these cases, do whichever is clearer.

For OnlyOneReturn, there are several ways to view this (with vehement supporters behind each), but they all basically boil down to taste.

For your example, the OnlyOneReturn proponents want code like:

public int performAction(String input) {
    int result;
    if (input.equals("bob")) {
        result = 1;
    } else {
        result = 2;
    }
    return result;
}

Rather than:

public int performAction(String input) {
    if (input.equals("bob")) {
        return 1;
    } else {
        return 2;
    }
}

As you can see, the additional clarity of ReturnOnlyOnce can be debated.

Also see this SO question that relates to instantiation within loops.

痴梦一场 2024-09-02 01:50:38

本文Java 错误查找工具的比较,“作者 Nick Rutar、Christian Almazan 和 Jeff Foster,比较了 Java 的几种错误检查器...”—查找 Bug 文档和出版物。 PMD 看起来更加冗长。

附录:正如作者建议的那样,

“所有工具都在之间选择不同的权衡
产生误报和漏报。”

特别是,如果这是意图的话,AvoidInstantiatingObjectsInLoops 可能根本就不是一个错误。包含它是为了帮助 避免创建不必要的对象。同样,OnlyOneReturn本质上是暗示性的。多次返回代表一种goto形式,有时被认为是有害的,但是 人

我最讨厌的是那些在不了解误报概念的情况下强制使用此类工具的

。 -pmds-rules/2510152?noredirect=1#comment92243532_2510152">此处,PMD 的最新版本 在集成到构建过程中时支持改进的自定义。

This article, A Comparison of Bug Finding Tools for Java, "by Nick Rutar, Christian Almazan, and Jeff Foster, compares several bug checkers for Java..."—FindBugs Documents and Publications. PMD is seen to be rather more verbose.

Addendum: As the authors suggest,

"all of the tools choose different tradeoffs between
generating false positives and false negatives."

In particular, AvoidInstantiatingObjectsInLoops may not be a bug at all if that is the intent. It's included to help Avoid creating unnecessary objects. Likewise OnlyOneReturn is suggestive in nature. Multiple returns represent a form of goto, sometimes considered harmful, but reasonably used to improve readability.

My pet peeve is people who mandate the use of such tools without understanding the notion of false positives.

As noted here, more recent versions of PMD support improved customization when integrated into the build process.

带上头具痛哭 2024-09-02 01:50:38

您可以查看 PMD 主页,这里对规则进行了详细解释,并且通常还附有原因。该网站是针对规则组构建的,这里是基本规则的链接:http:// pmd.sourceforge.net/rules/basic.html

You can look at the PMD-homepage, the rules are explained here in detail and often with a why. The site is structured for the rules-groups, here the link to basic-rules: http://pmd.sourceforge.net/rules/basic.html

月下凄凉 2024-09-02 01:50:38

每个规则都位于 PMD 规则集中,它可以为您提供规则背后推理的线索(如果规则集页面本身没有详细解释)。

在避免InstantiatingObjectsInLoops 的情况下,一次又一次实例化类似的对象可能会很昂贵。然而,这常常是必要的。在我自己的项目中,我禁用了此规则,因为它标记了太多误报。

对于 OnlyOneReturn,请注意,它位于名为 Controversial 的规则集中,其中暗示这些规则是有争议的,并且取决于具体情况。我也禁用了整个规则集。

Each rule is in a PMD Rule Set, which can give you a clue to the reasoning behind the rule (if it isn't explained in detail on the Rule Set page itself).

In the case of AvoidInstantiatingObjectsInLoops, it can be expensive to instantiate a similar object again and again. However it is frequently necessary. On my own project, I have disable this rule, since it is flagging too many false positives.

In the case of OnlyOneReturn, note that it is in a Rule Set called Controversial, which is a hint that these rules are debatable, and depend on the case. I have disabled this entire Rule Set as well.

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