“为什么” PMD的规则背后 [英] The "Why" behind PMD's rules

查看:124
本文介绍了“为什么” PMD的规则背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个很好的资源来描述PMD规则集背后的原因? PMD的网站有什么 - 每个规则的作用 - 但它没有描述为什么PMD有这个规则,为什么忽视这个规则可以让你在现实世界中遇到麻烦。特别是,我有兴趣知道为什么PMD有AvoidInstantiatingObjectsInLoops和OnlyOneReturn规则(如果你需要创建一个对应于集合中每个对象的新对象,第一个似乎是必要的,第二个似乎在许多情况下是必要的根据某些标准返回一个值),但我真正想要的是一个链接,用于描述大多数PMD规则背后的原因,因为这经常出现这种情况。

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.

为了清楚起见,我知道我可以禁用这些以及如何做到这一点,我只是想知道为什么他们首先在那里。很抱歉,如果有一些明显的东西我错过了,但我在发布之前进行了谷歌搜索和搜索。我也明白这些问题往往是品味的问题 - 我正在寻找的是规则的论点是什么以及有什么替代品。举一个具体的例子,你应该如何实现一个对应循环中每个对象的对象(这是Java中的常见操作)而不在循环中实例化每个对象?

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?

推荐答案

在每种情况下,规则可以是特定情况或只是品味。

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.

对于OnlyOneReturn,有几种方法可以查看它(每个背后都有激烈的支持者),但它们基本上都归结为味道。

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

对于您的示例,OnlyOneReturn支持者需要以下代码:

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;
}

而不是:

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

如您所见,ReturnOnlyOnce的额外清晰度可以有争议。

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

另见这个与循环内的实例化

这篇关于“为什么” PMD的规则背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆