Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Way to optimize this?

 
Thread Tools Search this Thread
Old 11-06-2009, 05:10 PM   #1
Default Way to optimize this?


Hi,

I'm using Java 1.5. My code checker (PMD plug-in, part of Eclipse
Galileo), is complaining about the line "Integer fileCount = null;" in
the following ...

public Integer getVendorRequestFileDigestCount(final
YouthfulDriverVendor vendor,
final String
requestFileDigest) {
Integer fileCount = null;
final Session session = sessionFactory.openSession();
try {
fileCount = (Integer)
session. createCriteria(AddressRequestFile.class)
.add(Restrictions.eq("requestFileDigest", requestFileDigest))
.add(Restrictions.eq("vendor", vendor))
.add(Restrictions.gt("processingResult", 0))
.add(Restrictions.isNotNull("processingDate"))
.setProjection(Projections.rowCount()).uniqueResul t();
} finally {
session.close();
}
return fileCount;
}

with the complaint, "Found 'DD' -- anomaly for 'fileCount', lines
123-126". Evidently, it doesn't like the fact that the value of the
variable "fileCount" changes. But I don't know how to rewrite this
code block to satisfy the code checker. Do you?

Thanks, - Dave


laredotornado
  Reply With Quote
Old 11-06-2009, 05:23 PM   #2
Lew
 
Posts: n/a
Default Re: Way to optimize this?
Contrary to your subject line, this is not an optimization issue.

laredotornado wrote:
> I'm using Java 1.5. *My code checker (PMD plug-in, part of Eclipse
> Galileo), is complaining about the line "Integer fileCount = null;" in
> the following ...
>
> * public Integer getVendorRequestFileDigestCount(final
> YouthfulDriverVendor vendor,
> * * * * * * * * * * * * * * * * * * * * * * * * *final String


Please, please, please don't indent so heavily.

> requestFileDigest) {
> * * Integer fileCount = null;


Since you never use this 'null' value it's superfuous and you
shouldn't assign it..

> * * final Session session = sessionFactory.openSession();


Aren't there exceptions you need to catch here?

> * * try {
> * * * fileCount = (Integer)


Why is this cast necessary? What is the return type of 'uniqueResult
()'?

> * * * * session. * * * *createCriteria(AddressRequestFile..class)


Weird spacing.

> * * * * .add(Restrictions.eq("requestFileDigest", requestFileDigest))
> * * * * .add(Restrictions.eq("vendor", vendor))
> * * * * .add(Restrictions.gt("processingResult", 0))
> * * * * .add(Restrictions.isNotNull("processingDate"))
> * * * * .setProjection(Projections.rowCount()).uniqueResul t();
> * * } finally {
> * * * session.close();
> * * }
> * * return fileCount;
> * }
>
> with the complaint, "Found 'DD' -- anomaly for 'fileCount', lines
> 123-126". *Evidently, it doesn't like the fact that the value of the
> variable "fileCount" changes. *But I don't know how to rewrite this
> code block to satisfy the code checker. *Do you?


That "code checker" sure gives you obscure, uninformative messages,
doesn't it?

Switch to FindBugs.

I don't know how you read that it doesn't "like" the change to the
'fileCount' variable from that message. Since you don't even indicate
what the line numbers are, it's impossible for us to know anything
about that message. What are lines 123-126, hm?

Incomplete questions can lead to incomplete answers.

I'm going to ignore your "code checker" and just talk about good
sense. It is perfectly fine to reassign values to non-final
variables.

You should minimize the scope of variables, however. You can declare
'fileCount' inside the 'try' block, or eliminate it altogether.

--
Lew


Lew
  Reply With Quote
Old 11-06-2009, 05:48 PM   #3
Noel
 
Posts: n/a
Default Re: Way to optimize this?
On Nov 6, 9:10*am, laredotornado <laredotorn...@zipmail.com> wrote:
> Hi,
>
> I'm using Java 1.5. *My code checker (PMD plug-in, part of Eclipse
> Galileo), is complaining about the line "Integer fileCount = null;" in
> the following ...
>
> * public Integer getVendorRequestFileDigestCount(final
> YouthfulDriverVendor vendor,
> * * * * * * * * * * * * * * * * * * * * * * * * *final String
> requestFileDigest) {
> * * Integer fileCount = null;
> * * final Session session = sessionFactory.openSession();
> * * try {
> * * * fileCount = (Integer)
> * * * * session. * * * *createCriteria(AddressRequestFile..class)
> * * * * .add(Restrictions.eq("requestFileDigest", requestFileDigest))
> * * * * .add(Restrictions.eq("vendor", vendor))
> * * * * .add(Restrictions.gt("processingResult", 0))
> * * * * .add(Restrictions.isNotNull("processingDate"))
> * * * * .setProjection(Projections.rowCount()).uniqueResul t();
> * * } finally {
> * * * session.close();
> * * }
> * * return fileCount;
> * }
>


Integer fileCount = null;
try {
fileCount = ...;
} catch (...) {
fileCount = null;
} finally {
...
}
return fileCount;

Of course, it is advisable to minimize the scope of fileCount, so one
could instead do this (or a variation):

try {
Integer fileCount = ...;
return fileCount;
} catch (...) {
return null;
} finally {
...
}

But then your code checker may complain about having more than one
return statement in a method.

> with the complaint, "Found 'DD' -- anomaly for 'fileCount', lines
> 123-126". *Evidently, it doesn't like the fact that the value of the
> variable "fileCount" changes. *But I don't know how to rewrite this
> code block to satisfy the code checker. *Do you?


It's unfortunate than an application intended to improve clarity of
code emits unhelpful messages. I'm unfamiliar with this code checker,
but a cursory Google search turned up that this particular rule
belongs to its "Controversial" rule set (http://pmd.sourceforge.net/
rules/controversial.html).


Noel
  Reply With Quote
Old 11-06-2009, 06:08 PM   #4
laredotornado
 
Posts: n/a
Default Re: Way to optimize this?
On Nov 6, 10:48*am, Noel <prosthetic_conscien...@yahoo.com> wrote:
> On Nov 6, 9:10*am, laredotornado <laredotorn...@zipmail.com> wrote:
>
>
>
> > Hi,

>
> > I'm using Java 1.5. *My code checker (PMD plug-in, part of Eclipse
> > Galileo), is complaining about the line "Integer fileCount = null;" in
> > the following ...

>
> > * public Integer getVendorRequestFileDigestCount(final
> > YouthfulDriverVendor vendor,
> > * * * * * * * * * * * * * * * * * * * * * * * * *final String
> > requestFileDigest) {
> > * * Integer fileCount = null;
> > * * final Session session = sessionFactory.openSession();
> > * * try {
> > * * * fileCount = (Integer)
> > * * * * session. * * * *createCriteria(AddressRequestFile.class)
> > * * * * .add(Restrictions.eq("requestFileDigest", requestFileDigest))
> > * * * * .add(Restrictions.eq("vendor", vendor))
> > * * * * .add(Restrictions.gt("processingResult", 0))
> > * * * * .add(Restrictions.isNotNull("processingDate"))
> > * * * * .setProjection(Projections.rowCount()).uniqueResul t();
> > * * } finally {
> > * * * session.close();
> > * * }
> > * * return fileCount;
> > * }

>
> * * Integer fileCount = null;
> * * try {
> * * * * fileCount = ...;
> * * } catch (...) {
> * * * * fileCount = null;
> * * } finally {
> * * * *...
> * * }
> * * return fileCount;
>
> Of course, it is advisable to minimize the scope of fileCount, so one
> could instead do this (or a variation):
>
> * * try {
> * * * * Integer fileCount = ...;
> * * * * return fileCount;
> * * } catch (...) {
> * * * * return null;
> * * } finally {
> * * * *...
> * * }
>
> But then your code checker may complain about having more than one
> return statement in a method.
>
> > with the complaint, "Found 'DD' -- anomaly for 'fileCount', lines
> > 123-126". *Evidently, it doesn't like the fact that the value of the
> > variable "fileCount" changes. *But I don't know how to rewrite this
> > code block to satisfy the code checker. *Do you?

>
> It's unfortunate than an application intended to improve clarity of
> code emits unhelpful messages. *I'm unfamiliar with this code checker,
> but a cursory Google search turned up that this particular rule
> belongs to its "Controversial" rule set (http://pmd.sourceforge.net/
> rules/controversial.html).


PMD is mandated by our company so sadly I don't have control over the
decision to use it at this time. Noel, per your suggestion, the code
checker does complain about the return statement needing to be the
last line in the method when I switch to what you have, although I do
like how you've reduced the scope of the variable.

- Dave


laredotornado
  Reply With Quote
Old 11-06-2009, 06:45 PM   #5
Noel
 
Posts: n/a
Default Re: Way to optimize this?
On Nov 6, 10:08*am, laredotornado <laredotorn...@zipmail.com> wrote:
> Noel, per your suggestion, the code
> checker does complain about the return statement needing to be the
> last line in the method when I switch to what you have, although I do
> like how you've reduced the scope of the variable.


To satisfy both rules, it seems to me that the scope of fileCount
cannot be minimal.

Integer fileCount; // don't initialize
try {
fileCount = ...;
} catch (...) {
fileCount = null;
} finally {
...
}
return fileCount;

I suspect a NullAssignment complaint will be raised. (Good grief.)


Noel
  Reply With Quote
Old 11-06-2009, 06:46 PM   #6
Noel
 
Posts: n/a
Default Re: Way to optimize this?
On Nov 6, 10:08*am, laredotornado <laredotorn...@zipmail.com> wrote:
> Noel, per your suggestion, the code
> checker does complain about the return statement needing to be the
> last line in the method when I switch to what you have, although I do
> like how you've reduced the scope of the variable.


To satisfy both rules, it seems to me that the scope of fileCount
cannot be minimal.

Integer fileCount; // don't initialize
try {
fileCount = ...;
} catch (...) {
fileCount = null;
} finally {
...
}
return fileCount;

I suspect a NullAssignment complaint will be raised. (Good grief.)


Noel
  Reply With Quote
Old 11-06-2009, 07:21 PM   #7
Roedy Green
 
Posts: n/a
Default Re: Way to optimize this?
On Fri, 6 Nov 2009 09:10:07 -0800 (PST), laredotornado
<> wrote, quoted or indirectly quoted someone
who said :

> public Integer getVendorRequestFileDigestCount(final
>YouthfulDriverVendor vendor,
> final String
>requestFileDigest) {
> Integer fileCount = null;
> final Session session = sessionFactory.openSession();
> try {
> fileCount = (Integer)
> session. createCriteria(AddressRequestFile.class)
> .add(Restrictions.eq("requestFileDigest", requestFileDigest))
> .add(Restrictions.eq("vendor", vendor))
> .add(Restrictions.gt("processingResult", 0))
> .add(Restrictions.isNotNull("processingDate"))
> .setProjection(Projections.rowCount()).uniqueResul t();
> } finally {
> session.close();
> }
> return fileCount;
> }


I have stared and stared at the code. I don't see anything wrong with
it. fileCount is always assigned a value. You didn't mark it final.
Maybe this lint dislikes variables being redefined, wanting everything
final.

--
Roedy Green Canadian Mind Products
http://mindprod.com

Without deviation from the norm, progress is not possible.
~ Frank Zappa (born: 1940-12-21 died: 1993-12-04 at age: 52)


Roedy Green
  Reply With Quote
Old 11-06-2009, 07:37 PM   #8
Lew
 
Posts: n/a
Default Re: Way to optimize this?
laredotornado wrote:
> Noel, per your suggestion, the code
> checker does complain about the return statement needing to be the
> last line in the method ...
>


That's not a valid rule.

--
Lew


Lew
  Reply With Quote
Old 11-06-2009, 07:49 PM   #9
Noel
 
Posts: n/a
Default Re: Way to optimize this?
On Nov 6, 11:21*am, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> I have stared and stared at the code. I don't see anything wrong with
> it. fileCount is always assigned a value. You didn't mark it final.
> Maybe this lint dislikes variables being redefined, wanting everything
> final.


The PMD online documentation reads: "DD - Anomaly: A recently defined
variable is redefined. This is ominous but don't have to be a bug."
Very picky lint.



Noel
  Reply With Quote
Old 11-06-2009, 08:36 PM   #10
Eric Sosman
 
Posts: n/a
Default Re: Way to optimize this?
Noel wrote:
> On Nov 6, 11:21 am, Roedy Green <see_webs...@mindprod.com.invalid>
> wrote:
>> I have stared and stared at the code. I don't see anything wrong with
>> it. fileCount is always assigned a value. You didn't mark it final.
>> Maybe this lint dislikes variables being redefined, wanting everything
>> final.

>
> The PMD online documentation reads: "DD - Anomaly: A recently defined
> variable is redefined. This is ominous but don't have to be a bug."
> Very picky lint.


Does anybody understand what they mean by "defined" and
"redefined?" The only thing that happens to fileCount in the
original code is that it's declared with an initial value, then
assigned to, and finally used as the source of the method's
returned value. Where's the "redefinition?"

PMD also mentions a rule that objects when "a recently
defined variable is undefined." What in the world do they
mean by that? The only way I know to "undefine" a variable
is to let it go out of scope.

--
Eric Sosman
lid


Eric Sosman
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Dual-Monitors to Optimize Productivity Admin Front Page News 0 07-31-2009 08:21 AM
Looking for help with MTU/RWIN settings to optimize EVDO Matt Wireless Networking 0 06-29-2007 03:26 AM
7 Ways to Speed up and Optimize Windows XP Code name 47 Computer Support 2 04-02-2007 06:24 AM
Optimize internet performance =?Utf-8?B?YW5nbGVmYW45OQ==?= Wireless Networking 6 06-23-2006 05:08 PM
WPA2 and turbo mode? Jeff Wireless Networking 5 08-30-2005 10:40 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46