java 如何修复以下 PMD 违规

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4599446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:09:01  来源:igfitidea点击:

How to fix the following PMD violations

javacode-analysisstatic-analysispmd

提问by sarahTheButterFly

I am using PMD to analyze code and it produces a few high priority warnings which I do not know how to fix.

我正在使用 PMD 来分析代码,它产生了一些我不知道如何修复的高优先级警告。

1) Avoid if(x!=y)..; else...;But what should I do if I need this logic? That is, I do need to check if x!=y? How can I refactor it?

1)Avoid if(x!=y)..; else...;但是如果我需要这个逻辑我该怎么办?也就是说,我确实需要检查是否x!=y?我该如何重构它?

2) Use explicit scoping instead of the default package private level.But the class is indeed used only within the package. What access modifier should I use?

2)Use explicit scoping instead of the default package private level.但是这个类确实只在包内使用。我应该使用什么访问修饰符?

3) Parameter is not assigned and could be declared final.Should I add final keyword to all the places which PMD pointed out with this warning?

3)Parameter is not assigned and could be declared final.我应该在 PMD 指出的所有地方添加 final 关键字吗?

回答by mhaller

Avoid negation:Instead of if( x!=y ) doThis() else doThat(), check for the positive case first, because people/humans tend to like positive things more than negative. It twists the brain to have to reverse the logic in mind when reading the source code. So instead, write:

避免否定:而不是if( x!=y ) doThis() else doThat(),首先检查积极的情况,因为人们/人类往往更喜欢积极的事物而不是消极的事物。在阅读源代码时,大脑必须颠倒逻辑。所以相反,写:

 if ( x!=y ) doThis() else doThat()       // Bad - negation first
 if ( x==y ) doThat() else doThis()       // Good - positive first

Explicit scoping:According to PMD website, it's a controversial rule. You may hate it, someone else likes it. What you should do is make all the fields within your classes private. There seems to be a field or method (not a class) with a package visibility, e.g. something like this:

显式范围:根据PMD 网站,这是一个有争议的规则。你可能讨厌它,别人喜欢它。您应该做的是将类中的所有字段设为私有。似乎有一个具有包可见性的字段或方法(不是类),例如这样的:

 class Foo {
   /* private missing */ Object bar;
 }

Final parameters:Method parameters should be final to avoid accidental reassignment. That's just a good practice. If you're using Eclipse, the content assist even provides a quickfix called "Change modifiers to final where possible". Just select all code in the editor with Ctrl-a and then press Ctrl-1.

最终参数:方法参数应该是最终的,以避免意外重新分配。这只是一个很好的做法。如果您使用的是 Eclipse,内容帮助甚至提供了一个名为“尽可能将修饰符更改为最终版本”的快速修复。只需使用 Ctrl-a 选择编辑器中的所有代码,然后按 Ctrl-1。

回答by Andreas Dolk

You don't need to enable all rules. Choose some of the rules you agree to and refactor your code until all warnings are cleared.

您不需要启用所有规则。选择一些您同意的规则并重构您的代码,直到清除所有警告。

1- Refactor it to a if (x == y) ... else ...logic. Just avoid negative conditions in if statments, they make code harder to understand

1- 将其重构为if (x == y) ... else ...逻辑。避免在 if 语句中使用否定条件,它们会使代码更难理解

2- I wouldn't enable that rule.

2- 我不会启用该规则。

3- A lot of people declare a lot of fields and variables final. Especially when they want to make sure or express that the value of a variable shall not be changed in the method. If you don't like that, disable that rule.

3- 很多人将很多字段和变量声明为 final。尤其是当他们想确定或表达一个变量的值不能在方法中改变时。如果您不喜欢那样,请禁用该规则。

回答by ILMTitan

These all seem like minor warnings that could be turned off.

这些似乎都是可以关闭的小警告。

1) It wants you to flip the logic

1)它想让你翻转逻辑

if(x==y) {
    //old else clause
} else {
    //old if clause
}

2) If package is really the correct access you want, there is no access modifier to add. I am not familiar enough to know if there is a way to suppress that specific warning.

2) 如果包确实是您想要的正确访问权限,则无需添加访问修饰符。我不够熟悉,不知道是否有办法抑制该特定警告。

3) A style issue. Some people want final on everything it could be on. Others thinks it adds too much clutter for to little information. If you are in the latter camp, turn that warning off.

3)风格问题。有些人想要最终确定它可能进行的一切。其他人则认为它为很少的信息增加了太多的混乱。如果您属于后者,请关闭该警告。

回答by Uri

Regarding the first item (the inequality) there are two issues:

关于第一项(不等式)有两个问题:

1) Readability of double negation.

1) 双重否定的可读性。

Say you have:

说你有:

if(x!=y) { false clause } else { true clause }

The second clause is executed if "not x is not equal to y".

如果“not x 不等于 y”,则执行第二个子句。

This can be rewritten as:

这可以改写为:

if (x==y) {true clause } else {false clause}.

2) Correctness: if x and y are not-primitives, using if(!x.equals(y))is safer. This is the equivalent of using == instead of .equals() and can lead to very serious bugs.

2) 正确性:如果 x 和 y 不是原语,则使用if(!x.equals(y))更安全。这相当于使用 == 而不是 .equals() 并且会导致非常严重的错误。

回答by KrishPrabakar

You can also use // NOPMDat the end of any line where you don't want PMD rules to be checked.

您还可以// NOPMD在不希望检查 PMD 规则的任何行的末尾使用。

For example for the above given code you can suppress PMD check by giving,

例如,对于上面给出的代码,您可以通过给出来抑制 PMD 检查,

class Foo {
   /* private missing */ Object bar; // NOPMD
 }

Please be aware that the above comment may silently suppress other warnings in the sameline.

请注意,上述注释可能会默默地抑制同一行中的其他警告。