I have an old habit of avoiding calling references multiple times, both for easier to read/maintain code, and for possible efficiency. But I'm wondering which is more efficient (memory, performance, both?).
For example when working with XML using a VTD parser
if( vn.toString( vn.getCurrentIndex() ).equalsIgnoreCase( "broken-label" ) == false )
{
do
{
if( parentNode != null )
{
currentNode = parentNode.addChildNode( vn.toString( vn.getCurrentIndex() ) );
}
else
{
currentNode = new xmlNode( vn.toString(vn.getCurrentIndex()), null );
treeNodes.add( 0, currentNode );
}
Does not store the value, perhaps saving some overhead for creating space to save a local variable and also lowering the burden on the garbage collector (assuming this section of code is repeated thousands of times in quick succession.
My habit of cleaner/efficient code would be to replace the above with the simple change of
String label = vn.toString( vn.getCurrentIndex();
if( label ).equalsIgnoreCase( "vsled-image" ) == false )
{
do
{
if( parentNode != null )
{
currentNode = parentNode.addChildNode( label ) );
}
else
{
currentNode = new xmlNode( label, null );
treeNodes.add( 0, currentNode );
}
While this is obviously easier to read and maintain. Are there any non-human benefits?
)
and(
in various places that you might want to clean up. Your indenting is inconsistent (and deep) - you might want to consider sending your code through prettyprinter.de (might change from your style, but it will make it consistent. As an aside,if(something == false)
makes me sad (tryif(!something)
instead). This becomes possibly important if someone did something silly likenew Boolean("false")
and the autoboxing is done on the wrong operand)