File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes PMD analysis- DU - Anomaly ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "PMD analysis- DU - Anomaly ?" Watch "PMD analysis- DU - Anomaly ?" New topic
Author

PMD analysis- DU - Anomaly ?

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
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?


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 17, 2008
Posts: 844
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.

[Jess in Action][AskingGoodQuestions]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Jul 17, 2008
Posts: 844
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 17, 2008
Posts: 844
So it does means. I can assume my code is fine.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19543
    
  16

What happens if you make both variables final?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Phil Wright
Greenhorn

Joined: Aug 18, 2009
Posts: 1
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.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PMD analysis- DU - Anomaly ?
 
Similar Threads
Better JUnit Tests with PMD - free book chapter
A Java brainteaser for you
Counting tokens allows us to test whether a postfix expression is wellformed. Anyone seen it before?
Looking for an optimization
Another way to write this?