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