[hibernate-issues] [Hibernate-JIRA] Created: (OGM-63) Remove unused code branches and unnecessary null checks

Juraci Paixao Krohling (JIRA) noreply at atlassian.com
Sat May 7 10:06:59 EDT 2011


Remove unused code branches and unnecessary null checks
-------------------------------------------------------

                 Key: OGM-63
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/OGM-63
             Project: Hibernate OGM
          Issue Type: Improvement
          Components: core
    Affects Versions: 3.0.0.Alpha1
            Reporter: Juraci Paixao Krohling
            Assignee: Juraci Paixao Krohling
             Fix For: 3.0-next


I ran the FindBugs code analysis tool and noticed that there are some unnecessary null checks and unused code branches in Hibernate OGM Core module, like the following example from OgmLoader:

{code:title=OgmLoader.java}
//we don't load more than one instance per row, shortcircuiting it for the moment
final int[] collectionOwners = null;

for ( int i=0; i<collectionPersisters.length; i++ ) {
	final CollectionAliases[] descriptors = getCollectionAliases();
	final boolean hasCollectionOwners = collectionOwners !=null &&
			collectionOwners[i] > -1;
	//true if this is a query and we are loading multiple instances of the same collection role
	//otherwise this is a CollectionInitializer and we are loading up a single collection or batch

	final Object owner = hasCollectionOwners ?
			row[ collectionOwners[i] ] :
			null; //if null, owner will be retrieved from session

	final CollectionPersister collectionPersister = collectionPersisters[i];
	final Serializable key;
	if ( owner == null ) {
		key = null;
	}
	else {
		key = collectionPersister.getCollectionType().getKeyOfOwner( owner, session );
		//TODO: old version did not require hashmap lookup:
		//keys[collectionOwner].getIdentifier()
	}

	readCollectionElement(
			owner,
			key,
			collectionPersister,
			descriptors[i],
			resultSet, //TODO CURRENT must use the same instance across all calls
			session
		);
{code} 

Note that {{collectionOwners}} is always {{null}}. Thus, {{hasCollectionOwners}} is always {{false}}, causing {{owner}} to always be {{null}}. Then, the {{if ( owner == null ) }} will always be evaluated as true, turning the else part to be unnecessary. The above code can be rewritten as:

{code:title=OgmLoader.java}
final CollectionAliases[] descriptors = getCollectionAliases(); // this line was inside the for loop... it seems it can be outside
for ( int i=0; i<collectionPersisters.length; i++ ) {
	final CollectionPersister collectionPersister = collectionPersisters[i];

	readCollectionElement(
			null,
			null,
			collectionPersister,
			descriptors[i],
			resultSet, //TODO CURRENT must use the same instance across all calls
			session
		);
}
{code} 

This JIRA is to track similar issues to the example above. 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list