Re: general performance question



Matt Humphrey wrote (restoring the attribution that was elided):
Clearly there's no sense at any time in creating an object and then setting
it to null

grasp06110 wrote:
Sometimes I declare a variable that is to point to a resource and set
it to null so that the resource can be released in a wider scope. For
example:

Connection conn = null;
try {
conn = ConnectionFactory.getConnection();
doSomethingWithTheConnection(conn);
} finally {
if(conn != null) {
conn.close();
}
}

In other words, you have a sound engineering reason for where you place your variable declaration - scope.

Declare the variable at the closest point within the appropriate scope to its use. Do NOT initialize the variable to null unless null is a valid value for it at the point of declaration, as in grasp's example. This is the best practice as recommended by Joshua Bloch in /Effective Java/, for example.

I would work that particular example a little differently myself. I'm loathe to assign to a variable twice when it only needs one value:

Connection conn; // declared with appropriate scope
try
{
conn = ConnectionFactory.getConnection();
if ( conn == null )
{
throw new SQLException( "No connection" );
}
}
catch ( SQLException exc )
{
logger.error( exc );
return;
}

assert conn != null; // invariant

try
{
doSomethingWithTheConnection(conn);
}
catch ( SQLException exc )
{
logger.error( exc );
}
finally
{
try
{
conn.close();
}
catch ( SQLException ex )
{
logger.error( ex );
}
}

--
Lew
.



Relevant Pages

  • compiling kernel
    ... scripts/kconfig/qconf.h:51: error: `e' was not declared in this scope ... scripts/kconfig/qconf.h:73: error: `int updateList' redeclared as different ... scripts/kconfig/qconf.h:8: error: forward declaration of `class ConfigList' ... ConfigLineEdit' ...
    (alt.os.linux.suse)
  • Re: Check whether an argument in command line is a number
    ... as part or all of their makeup: The compound statement in its entirety ... declarators of ordinary identifiers with block scope, ... each time the declaration is reached in the order of execution, ... also a block whose scope is a strict subset of the scope of the ...
    (comp.lang.c)
  • Re: Declaration statement
    ... as declarations appearing at file and block scope... ... other declaration overrides it -- because the rules of the C ... way, and in the days before the first C standard (ANSI X3.159-1989, ... different C compilers had different rules. ...
    (comp.lang.c)
  • Re: type/rank mismatch error
    ... | procedures that are *NOT* in the same scope as the USE statement. ... See host association. ... declaration overrides the host-associated declaration. ...
    (comp.lang.fortran)
  • Re: For Loops and Variables
    ... scope, I prefer a slightly longer identifier if I have something worth ... // use dotProduct. ... There really isn't anything worth saying about i that is not already ... going to have that declaration in front of them when looking at a use. ...
    (comp.lang.java.programmer)