Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PMD analysis- DU - Anomaly ?

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran PMD analyzer. it gives me an informational message.

DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text.



I did not understand what is the problem in this code and what is the correct code that can be written?
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The message indicates that the line buz = 2 is completely pointless, since the value si never used. Since buz is a local variable, its value is forgotten once the method ends. Next time the method runs, the variable will have a different value, starting with 5. So what was the point of setting it to 2?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a reason "DU - Anomaly" is in the "controversial" section: it's not always an indicator of incorrect code. In this case the final assignment is meaningless, so it lets you know that you may have done something other than you intended.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whatever i am doing in my code is fine as per my requirements. Nothing is done useless here.
This is just a snapshot of the code given by PMD.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want people to explain exactly what PMD means, then (not surprisingly) you'll have to let folks see exactly what PMD is seeing. Otherwise people are just guessing, or explaining what's wrong with your code even though it's not your real code. If you show us made-up code, then of course, we can only guess at what the real stuff looks like.
 
Mike Simmons
Ranch Hand
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternately, if this really is the actual code you're using, one of two things is probably true:

(a) Your instructor is imposing really strange requirements on you, in order to try to force you to learn something. Be aware that the code you're being forced to write does not really resemble real-world code that anyone might use to do something useful. Other than learning something, perhaps. If this is the case, you should simply ignore PMD entirely, at least while taking your current course. The goals of PMD's creators are incompatible with those of your instructor.

OR:

(b) Your code really is doing useless things, despite your disclaimers, and you simply don't understand why that is true. If this is the case, you aren't going to learn anything new by simply insisting that your code is "fine per my requirements". Maybe you could describe those requirements in more detail.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok here is the code:

This is the snapshot of the code. Skip undefined variables.
As it is a pretty big code. To make it easy. I have written short.

PMD is giving me informational message for- boolean recalculateDuty = false;
Set<DBRecordKey> sDBSplDuties = new HashSet<DBRecordKey>();

I am passing these field variables to method startCalculatingDuties, Inside this method it can change its value as per requirement.
Also sDBSplDuties is paased to this method- sDBSplDuties
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you missed where I said that DU anomalies can appear on normal source.

Here's what it says on the PMD site, in case you don't believe me:
The PMD docs wrote:2. DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text.

And, also again, that's one reason it's in the "controversial rules" section.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it does means. I can assume my code is fine.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't *assume* your code is fine, but you can *know* it's fine--we can't.

(I wouldn't even think it would compile unless you define E somewhere else, which makes the exception handling a little suspect.)
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you make both variables final?
 
Phil Wright
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably too late but...

Amandeep Singh wrote:
PMD is giving me informational message for:

boolean recalculateDuty = false;

I am passing these field variables to method startCalculatingDuties, Inside this method it can change its value as per requirement.


No, it can't. startCalculatingDuties() is always going to be called with false no matter what you do to the parameter inside of the method. Java is pass-by-value.

If you want to change recalculateDuty based on something happening in startCalculatingDuties() then it should return a boolean.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic