Alex Rivera | Logout

How to fix the following PMD violations

Asked 2011-01-04T23:18:07.180
19

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?

Edit
Report

2 Answers

6

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.

answered 2011-01-04T23:35:24.563
1

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.

answered 2012-06-03T10:05:48.977

Your Answer