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, 08:38 PM   #11
Default Re: Way to optimize this?


Michal Kleczek wrote:
> laredotornado 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;
>> }
>>
>> 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?
>>

>
> Don't know this particular code checker but IMHO it is right complaining and
> this would make it happier (that's what Lew suggested actually):
>
> final Session session = sessionFactory.openSession();
> try {
> return
> session.createCriteria....
> }
> finally {
> session.close();
> }

return null;

.... or the method "falls off the end" and won't compile. And
then you've got two `return' statements, which will probably
earn still another scolding from this disagreeable tool.

--
Eric Sosman
lid


Eric Sosman
  Reply With Quote
Old 11-06-2009, 08:51 PM   #12
Daniel Pitts
 
Posts: n/a
Default Re: Way to optimize this?
laredotornado 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;
> }
>
> 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



public Integer
getVendorRequestFileDigestCount(final YouthfulDriverVendor vendor,
final String requestFileDigest) {
final Integer fileCount; // make final and don't assign yet.
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;
}

That is the easiest way. Note, you can simplify it even more:

public Integer
getVendorRequestFileDigestCount(final YouthfulDriverVendor vendor,
final String requestFileDigest) {
final Session session = sessionFactory.openSession();
try {
return (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();
}
}

no fileCoount variable worry about anywhere.


Daniel Pitts
  Reply With Quote
Old 11-06-2009, 08:53 PM   #13
Daniel Pitts
 
Posts: n/a
Default Re: Way to optimize this?
Eric Sosman wrote:
> Michal Kleczek wrote:
>> laredotornado 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;
>>> }
>>>
>>> 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?
>>>

>>
>> Don't know this particular code checker but IMHO it is right
>> complaining and this would make it happier (that's what Lew suggested
>> actually):
>>
>> final Session session = sessionFactory.openSession();
>> try {
>> return
>> session.createCriteria....
>> }
>> finally {
>> session.close();
>> }

> return null;
>
> .... or the method "falls off the end" and won't compile. And
> then you've got two `return' statements, which will probably
> earn still another scolding from this disagreeable tool.


Adding that return null is a compile error.


Daniel Pitts
  Reply With Quote
Old 11-06-2009, 10:24 PM   #14
Jim Janney
 
Posts: n/a
Default Re: Way to optimize this?
laredotornado <> writes:

> 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


The complaint is simply that you're initializing a variable to a value
that is never used, just as if you had written

int i = 4;
i = 15;

The minimal fix is to remove the useless initialization for fileCount: replace
Integer fileCount = null;
with
Integer fileCount;

But I think Michal Kleczek's and Daniel Pitts' solution is better
overall. Putting the return statement after the finally clause
creates the impression that the return value might be calculated in
more than one way.

--
Jim Janney


Jim Janney
  Reply With Quote
Old 11-07-2009, 02:43 AM   #15
Eric Sosman
 
Posts: n/a
Default Re: Way to optimize this?
Daniel Pitts wrote:
> Eric Sosman wrote:
>> Michal Kleczek wrote:
>>> [...]
>>> final Session session = sessionFactory.openSession();
>>> try {
>>> return
>>> session.createCriteria....
>>> }
>>> finally {
>>> session.close();
>>> }

>> return null;
>>
>> .... or the method "falls off the end" and won't compile. And
>> then you've got two `return' statements, which will probably
>> earn still another scolding from this disagreeable tool.

>
> Adding that return null is a compile error.


Right you are. Sorry, Michal. <FX: Self-administered
dope slap>

--
Eric Sosman
lid


Eric Sosman
  Reply With Quote
Old 11-07-2009, 04:19 AM   #16
Eric Sosman
 
Posts: n/a
Default Re: Way to optimize this?
Jim Janney wrote:
> laredotornado <> writes:
>
>> 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

>
> The complaint is simply that you're initializing a variable to a value
> that is never used, just as if you had written
>
> int i = 4;
> i = 15;


No, the initial value *is* used, if anything in that
chain of method calls throws an exception.

> The minimal fix is to remove the useless initialization for fileCount: replace
> Integer fileCount = null;
> with
> Integer fileCount;


I think that would produce a compile error. (But I've
already embarrassed myself once on a similar point in this
thread, so don't take my word for it ...)

--
Eric Sosman
lid


Eric Sosman
  Reply With Quote
Old 11-07-2009, 06:01 AM   #17
Lew
 
Posts: n/a
Default Re: Way to optimize this?
laredotornado writes:
>>> 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() ).uniqueResult();
>>> } finally {
>>> session.close();
>>> }
>>> return fileCount;
>>> }


Jim Janney wrote:
>> The complaint is simply that you're initializing a variable to a value
>> that is never used, just as if you had written
>>
>> int i = 4;
>> i = 15;


Eric Sosman wrote:
> No, the initial value *is* used, if anything in that
> chain of method calls throws an exception.


Since there's no exception handler, an exception would cause the execution to
leave the method and the initial value would not be used.

Since the method as shown has no checked exceptions, only a runtime exception
can happen.

Jim Janney wrote:
>> The minimal fix is to remove the useless initialization for fileCount:
>> replace
>> Integer fileCount = null;
>> with
>> Integer fileCount;


Eric Sosman wrote:
> I think that would produce a compile error. (But I've


Nope. Shouldn't. I use a similar idiom quite frequently.

For example, to initialize a connection:

Connection cxn;
try
{
cxn = obtainConnection();
}
catch ( SomeException exc )
{
log( exc );
return;
}
assert cxn != null;
// etc.

--
Lew


Lew
  Reply With Quote
Old 11-07-2009, 07:41 AM   #18
Jim Janney
 
Posts: n/a
Default Re: Way to optimize this?
Eric Sosman <> writes:

> Jim Janney wrote:
>> laredotornado <> writes:
>>
>>> 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

>>
>> The complaint is simply that you're initializing a variable to a value
>> that is never used, just as if you had written
>>
>> int i = 4;
>> i = 15;

>
> No, the initial value *is* used, if anything in that
> chain of method calls throws an exception.


No, it isn't. There's no catch in the try-finally block, so if an
exception is thrown the return statement is never reached and there's
no need for fileCount to have a value.

>> The minimal fix is to remove the useless initialization for fileCount: replace
>> Integer fileCount = null;
>> with
>> Integer fileCount;

>
> I think that would produce a compile error. (But I've
> already embarrassed myself once on a similar point in this
> thread, so don't take my word for it ...)


See above. I notice some other comments in this thread added a catch
block to return null when an exception is thrown, but that's not how
the original code works (and in this case I think returning null would
be the wrong thing to do).

--
Jim Janney


Jim Janney
  Reply With Quote
Old 11-07-2009, 12:50 PM   #19
Eric Sosman
 
Posts: n/a
Default Re: Way to optimize this?
Eric Sosman wrote:
> [...]
> I think that would produce a compile error. (But I've
> already embarrassed myself once on a similar point in this
> thread, so don't take my word for it ...)


s/once/twice/

(Sigh.)

--
Eric Sosman
lid


Eric Sosman
  Reply With Quote
Old 11-07-2009, 03:09 PM   #20
Lew
 
Posts: n/a
Default Re: Way to optimize this?
Lew wrote:
>> Since you never use this 'null' value it's superfuous and you
>> shouldn't assign it..


Leif Roar Moldskred wrote:
> Well, you're not going to get away from the _assignment_ in Java,
> it's just a question of whether it's done implicitly (Object value
> or explicitly (Object value = null


Wrong. The implicit assignment doesn't exist for local variables. The OP's
example

Integer fileCount = null;

for a local variable would not have assigned 'null' if the declaration had been

Integer fileCount;

> As a matter of style, I prefer to write them out explicitly to separate
> "this variable is supposed to start as null" from "the programmer forgot
> to initialize this variable."


As a matter of avoidance of compilation error, a local variable must be
definitely assigned prior to use. If "the programmer forgot to initialize
this variable" the program would not compile when the variable was read.

Furthermore, the superfluous assignment of 'null' hides a failure to assign
the desired value, which would otherwise be a compiler error rather than a
runtime exception. As a matter of good engineering, which trumps "style", it
is better to catch mistakes at compilation than run time.

> For primitive types where an initial assignment at declaration can be
> superfluous, I still prefer to provide an initial value (if possible,
> something out of the range of what the variable will be assigned.) This
> is less a matter of style and more a matter of making bugs behave
> consistently. (With modern compilers and code analysers, this isn't
> really needed in Java -- the use of a variable before it's assigned to
> should be detected during coding -- but it's an old habit of mine and I
> still sometimes program in other languages so I don't see any reason to
> change it.)


In Java if you read a local variable before it's assigned the compiler barfs.
In some cases, it's even harmful to assign a superfluous initial value.

Integer fileCount = null;
try
{
Integer fileCount = expression();
}
finally
{
closeResources();
}
return fileCount;

I object to the superfluous assignment of zeroesque initial values to member
variables also, since any Java programmer already knows it happens on
instantiation. The overhead of the repeated 'null' assignment (or zero or
'false') is small, but why incur it at all if it isn't needed?

--
Lew


Lew
  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