如何解决以下 PMD 违规问题

发布于 2024-10-10 01:51:35 字数 300 浏览 6 评论 0原文

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

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

2) 使用显式作用域而不是默认的包私有级别。但是该类确实仅在包内使用。我应该使用什么访问修饰符?

3) 参数未分配,可以声明为final。我是否应该将final关键字添加到PMD通过此警告指出的所有位置?

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

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?

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?

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?

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

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

发布评论

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

评论(5

江城子 2024-10-17 01:51:35

避免否定:首先检查积极的情况,而不是if( x!=y ) doThis() else doThat(),因为人们倾向于喜欢积极的事物多于负面。阅读源代码时必须颠倒头脑中的逻辑,这真是绞尽脑汁。因此,请写:

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

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

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

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

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()       // 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:

 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.

兲鉂ぱ嘚淚 2024-10-17 01:51:35

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

1 - 将其重构为 if (x == y) .​​.. else ... 逻辑。只要避免 if 语句中的负面条件,它们会使代码更难理解

2 - 我不会启用该规则。

3 - 很多人将很多字段和变量声明为final。特别是当他们想要确保或表达变量的值不应在方法中更改时。如果您不喜欢这样,请禁用该规则。

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

2 - I wouldn't enable that rule.

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.

痴骨ら 2024-10-17 01:51:35

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

1) 它希望你翻转逻辑

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

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

3)风格问题。有些人希望一切都可以最终确定。其他人则认为它增加了太多的混乱,而信息却很少。如果您属于后一个阵营,请关闭该警告。

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

1) It wants you to flip the logic

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.

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.

江挽川 2024-10-17 01:51:35

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

1)双重否定的可读性。

假设:

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

如果“not x is not equal to y”,则执行第二个子句。

这可以重写为:

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

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

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

1) Readability of double negation.

Say you have:

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

The second clause is executed if "not x is not equal to 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.

情绪操控生活 2024-10-17 01:51:35

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

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

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

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

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

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

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

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

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