1.1如何设置PMD检查?
PMD将规则配置存储在称为Ruleset XML文件的特殊存储库中。此配置文件包含有关当前安装的规则及其属性的信息。
这些文件位于PMD发行版的rulesets目录中。在使用Eclipse时,请参考Customizing PMD。
1.2 Is it necessary to follow this warning advice?
A class which only has private constructors should be final
所有构造函数始终以调用超类构造函数开始。如果构造函数显式包含对超类构造函数的调用,则使用该构造函数。否则,隐含使用无参数构造函数。如果无参数构造函数不存在或对子类不可见,则会出现编译时错误。
因此,实际上不可能从每个构造函数都是私有的类派生子类。将这样的类标记为
final
是一个好主意(但不是必要的),因为它明确防止子类化。
1.3 What is that supposed to mean?
The class 'Dog' has a Cyclomatic Complexity of 3 (Highest = 17)
复杂度是方法中决策点的数量加上方法入口一个。决策点包括'if'、'while'、'for'和'case标签'。一般来说,1-4表示低复杂度,5-7表示中等复杂度,8-10表示高复杂度,11+表示非常高的复杂度。
话虽如此,我只会引用
聚合环形复杂度毫无意义的一些部分:
[...] This metric only has meaning in the context of a single method. Mentioning that a class has a Cyclomatic complexity of X is essentially useless.
Because Cyclomatic complexity measures
pathing in a method, every method has
at least a Cyclomatic complexity of 1,
right? So, the following getter method
has a CCN value of 1:
public Account getAccount(){
return this.account;
}
It’s clear from this boogie method
that account
is a property of this
class. Now imagine that this class has 15 properties and follows the typical getter/setter paradigm for each property and those are the only methods available. That means the class has 30 simple methods, each with a Cyclomatic complexity value of 1. The aggregate value of the class is then 30.
Does this value have any meaning, man?
Of course, watching it over time may
yield something interesting; however,
on its own, as an aggregate value, it
is essentially meaningless. 30 for the
class means nothing, 30 for a method
means something though.
The next time you find yourself
reading a copasetic aggregate
Cyclomatic complexity value for a
class, make sure you understand how
many methods the class contains. If
the aggregate Cyclomatic complexity
value of a class is 200– it shouldn’t
raise any red flags until you know the
count of methods. What’s more, if you
find that the method count is low yet
the Cyclomatic complexity value is
high, you will almost always find the
complexity localized to a method.
Right on!
所以对我来说,这个PMD规则应该小心处理(实际上并不是很有价值)。
1.4 What about this one? I would love to change this, but nothing crosses my mind at the moment regarding the change:
Assigning an Object to null is a code smell. Consider refactoring.
不确定你对这个的疑惑在哪里。
2.1 延迟至静态字段声明之后再写入,真的那么糟糕吗?[...]
我猜测你会收到警告,是因为该方法包含非同步的懒加载非易失性静态字段。由于编译器或处理器可能重新排序指令,如果该方法可以被多个线程调用,则不能保证线程看到一个完全初始化的对象。您可以使字段易失性以解决问题。
2.2 [...] readLine()
的结果立即解除引用
如果没有更多文本行可读,则 readLine()
将返回 null,并且解除引用将生成空指针异常。因此,确实需要检查结果是否为 null。