Way to optimize this?

M

Marcin Rze¼nicki

That's not a valid rule.

Well, I checked docs for this tool. Some of its rules are confusing,
to say the least. Take:

<docs>
AvoidUsingVolatile


Use of the keyword 'volatile' is general used to fine tune a Java
application, and therefore, requires a good expertise of the Java
Memory Model. Moreover, its range of action is somewhat misknown.
Therefore, the volatile keyword should not be used for maintenance
purpose and portability.
</docs>

volatile has strict semantics - it is not used to 'fine tune'
anything, its range of action is completely known, it is as portable
as anything.

<docs>
ExcessiveImports

A high number of imports can indicate a high degree of coupling within
an object. Rule counts the number of unique imports and reports a
violation if the count is above the user defined threshold.
</docs>

Really?

<docs>
UseArrayListInsteadOfVector


ArrayList is a much better Collection implementation than Vector.
</docs>

Never-mind the semantics differ

<docs>
UseStringBufferForStringAppends


Finds usages of += for appending strings.
</docs>

See the generated byte code, at least two compilers do it for you.

Whole set of rules concerning braces is controversial, at least for me
- and any good IDE can do it for you anyway.

I think that using this tool isn't worth the trouble, as OP example
shows you spend more time fighting with it than you should, even when
your code is logically valid.
 
L

laredotornado

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()).uniqueResult();
     } 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()).uniqueResult();
     } finally {
       session.close();
     }
   }

no fileCoount variable worry about anywhere.

Thanks to all for their feedback. PMD does have some rather bizarre
rules. That said, I implemented Daniel's second solution and all
worked well.

- Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top