这些PMD规则的原因是什么? [英] What is the reason for these PMD rules?

查看:274
本文介绍了这些PMD规则的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


DataflowAnomalyAnalysis:找到
'DD'-异常变量'变量'
(行'n1' - 'n2')。

DataflowAnomalyAnalysis: Found 'DD'-anomaly for variable 'variable' (lines 'n1'-'n2').

DataflowAnomalyAnalysis:找到
'DU'-anomaly变量'variable'
(行'n1' - 'n2')。

DataflowAnomalyAnalysis: Found 'DU'-anomaly for variable 'variable' (lines 'n1'-'n2').

DD和DU听起来很熟悉......我想说的是关于最弱前后条件的测试和分析,但我不记得具体细节。

DD and DU sound familiar...I want to say in things like testing and analysis relating to weakest pre and post conditions, but I don't remember the specifics.


NullAssignment:将对象分配给
null是一种代码气味。考虑
重构。

NullAssignment: Assigning an Object to null is a code smell. Consider refactoring.

不会将对象设置为 null 如果对象是本地对象(在方法之外没有使用),协助垃圾收集?或者这是一个神话?

Wouldn't setting an object to null assist in garbage collection, if the object is a local object (not used outside of the method)? Or is that a myth?


MethodArgumentCouldBeFinal:参数
'param'未分配,可能是
声明为final

MethodArgumentCouldBeFinal: Parameter 'param' is not assigned and could be declared final

LocalVariableCouldBeFinal:本地
变量'variable'可以声明为
final

LocalVariableCouldBeFinal: Local variable 'variable' could be declared final

使用 final 参数和变量有什​​么好处吗?

Are there any advantages to using final parameters and variables?


LooseCoupling:避免使用
'LinkedList'这样的
实现类型;使用接口
而不是

LooseCoupling: Avoid using implementation types like 'LinkedList'; use the interface instead

如果我知道我特别需要 LinkedList ,为什么我不会用一个让未来的开发者明确表达我的意图?返回有意义的类路径中最高的类是一回事,但为什么我不会声明我的变量是最严格的呢?

If I know that I specifically need a LinkedList, why would I not use one to make my intentions explicitly clear to future developers? It's one thing to return the class that's highest up the class path that makes sense, but why would I not declare my variables to be of the strictest sense?


AvoidSynchronizedAtMethodLevel:使用
块级别而不是方法级别
同步

AvoidSynchronizedAtMethodLevel: Use block level rather than method level synchronization

块的优点是什么?级别同步超过方法级同步?

What advantages does block-level synchronization have over method-level synchronization?


AvoidUsingShortType:不要使用
短类型

AvoidUsingShortType: Do not use the short type

我的第一语言是C和C ++,但在Java世界中,为什么我不能使用最能描述我数据的类型?

My first languages were C and C++, but in the Java world, why should I not use the type that best describes my data?

推荐答案


  • DD和DU异常(如果我没记错的话—我使用FindBugs并且消息有点不同)参考将值赋给永远不会读取的局部变量,通常是因为它在被读取之前被重新分配了另一个值。一个典型的情况是在声明时用 null 初始化一些变量。 在需要之前不要声明变量。

    • DD and DU anomalies (if I remember correctly—I use FindBugs and the messages are a little different) refer to assigning a value to a local variable that is never read, usually because it is reassigned another value before ever being read. A typical case would be initializing some variable with null when it is declared. Don't declare the variable until it's needed.

      null 分配给局部变量以协助垃圾收集器是一个神话。 PMD让你知道这只会产生适得其反的混乱。

      Assigning null to a local variable in order to "assist" the garbage collector is a myth. PMD is letting you know this is just counter-productive clutter.

      在本地变量上指定final应该对一个非常有用优化器,但我没有任何具体的当前JIT利用这个提示的例子。我发现它有助于推断我自己的代码的正确性。

      Specifying final on a local variable should be very useful to an optimizer, but I don't have any concrete examples of current JITs taking advantage of this hint. I have found it useful in reasoning about the correctness of my own code.

      用&hellip指定接口;好吧, interfaces 是一个很棒的设计实践。您可以轻松更改集合的实现,而不会影响调用者。这就是界面的全部内容。

      Specifying interfaces in terms of… well, interfaces is a great design practice. You can easily change implementations of the collection without impacting the caller at all. That's what interfaces are all about.

      我想不出很多情况下调用者需要 LinkedList ,因为它不公开任何未被某些接口声明的API。如果客户端依赖于该API,则可以通过正确的接口使用它。

      I can't think of many cases where a caller would require a LinkedList, since it doesn't expose any API that isn't declared by some interface. If the client relies on that API, it's available through the correct interface.

      块级同步允许关键部分更小,这允许尽可能多的工作尽可能同时完成。也许更重要的是,它允许使用由封闭对象私有控制的锁定对象。这样,您可以保证不会发生死锁。使用实例本身作为锁,任何人都可以错误地同步它,导致死锁。

      Block level synchronization allows the critical section to be smaller, which allows as much work to be done concurrently as possible. Perhaps more importantly, it allows the use of a lock object that is privately controlled by the enclosing object. This way, you can guarantee that no deadlock can occur. Using the instance itself as a lock, anyone can synchronize on it incorrectly, causing deadlock.

      类型为的操作数短都会升级为 int 。此规则让您知道此促销正在发生,您也可以使用 int 。但是,使用 short 类型可以节省内存,所以如果它是实例成员,我可能会忽略该规则。

      Operands of type short are promoted to int in any operations. This rule is letting you know that this promotion is occurring, and you might as well use an int. However, using the short type can save memory, so if it is an instance member, I'd probably ignore that rule.

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

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