views:

187

answers:

4

So I just came back from a 'meting' about a fix that we had to do which would fix an issue we had in production. Before the code goes out, it has to be presented to all these bunch of people. hate it!

Broken code (pseudo):

SomeClassA object1 = null
while loop
{
    if (some condition){
      // do something
    }
    else {
      object1 = someOtherObject.getValueForObject1();
    }
Record Date = object1.getRecordDate(); //this gets moved in fix
}

Suggested Code fix (pseudo)

SomeClassA object1 = null
while loop
{
    if (some condition){
      // do something
    }
    else {
      object1 = someOtherObject.getValueForObject1();
      Record Date = object1.getRecordDate();   
    }    
}

Code fix after suggestions (pseudo):

while loop
{
    if (some condition){
      // do something
    }
    else {
      SomeClassA object1 = null
      object1 = someOtherObject.getValueForObject1();
      Record Date = object1.getRecordDate();   
    }    
}

Broken code gave a Null pointer exception because for a record the control never went into the else block...which in turn kept object1 as null.

I'm sure people out there have encountered this type of issue. what are your stories/or suggestions? is it always a good practice to declare variables when they are needed instead of on top (I think Effective Java says that).. Is it a bad practice to declare object as 'null'

A: 

If you need to use object1 outside of the else block (which is the issue in the first and second examples but is taken care of in the third), then it needs to be declared somewhere else in the code structure.

If you want to keep using null as the initialization value, just remember to check for a null value before using the object.

Justin Niessner
+6  A: 

Initializing with null here is a waste of time:

while loop
{
    if (some condition){
      // do something
    }
    else {
      SomeClassA object1 = null;
      object1 = someOtherObject.getValueForObject1();
      Record Date = object1.getRecordDate();   
    }    
}

Just do this:

while loop
{
    if (some condition){
      // do something
    }
    else {
      SomeClassA object1 = someOtherObject.getValueForObject1();
      Record Date = object1.getRecordDate();   
    }    
}

To answer your question, if a variable is not immediately used but is required in multiple scopes - I initialize it to null when I declare it.

Philip Wallace
+1 - I didn't notice what you saw in your suggestion, that going to null is useless in their fix.
James Black
Would there be any drawbacks if SomeClassA object1 = someOtherObject.getValueForObject1(); is changed to final SomeClassA object1 = someOtherObject.getValueForObject1();considering its value will not change ??
Omnipresent
A: 

A few tips:

  1. When you declare an Object, in Java, C++, C# it is allays null, so please get rid of the "= null" thing.
  2. Yes it is a very good idea to minimize the scope for every declared object. This means that if you need an object only for the else statement then declare it in the else statement.
Haim Bender
Initializing to null is necessary to prevent warnings in some cases (like for instance when you have the assignment in an if block but the declaration outside)
Ryan Brunner
It is also required if you are going to have a finally block in order to do some cleanup.
James Black
"When you declare an Object, in Java, C++, C# it is allays null". No it isn't.
Joren
@Ryan, if you have assignment in a if block, then you should get the warning, and fix your code.@Joren, yes it is... Every object is initialized as null.
Haim Bender
You first point is not correct (as others already said). For initialization of local variables in C# see e.g. here http://stackoverflow.com/questions/1542824/c-initialization-of-instance-fields-vs-local-variables.
0xA3
oops sorry my bad.
Haim Bender
+1  A: 
  1. it is a good practice to declare variables when they are needed instead of on top (I think Effective Java says that).. minimize the scope.

  2. it is not a bad practice to declare object as 'null'. Most modern compiler will automatically optimize the object to null if a user does not do so. so it is good to assign to null.

Henry Gao