Starts with null



  • i am looking at some java code written by another developer on my project, long since gone. there are a lot of code samples of his along the lines of

    Object foo = null;
    foo = myManager.getImportantObject();

    where he creates a variable, sets it to null, then immediately assigns it something on the next line.

    so i am wondering if this is something he learned from some other language, or if there is some other viable reason why he would do it? it's not a big deal, i assume the java compiler optimizes it out, but WTF?



  • @feargal said:

    i am looking at some java code written by another developer on my project, long since gone. there are a lot of code samples of his along the lines of

    Object foo = null;
    foo = myManager.getImportantObject();

    where he creates a variable, sets it to null, then immediately assigns it something on the next line.

    so i am wondering if this is something he learned from some other language, or if there is some other viable reason why he would do it? it's not a big deal, i assume the java compiler optimizes it out, but WTF?

     

    IMO absolutely pointless. Would be different if the next line was inside a try-catch-block, like this example:

    Connection c = null;

    try {
        c = myManager.getConnection();
        ...
        ...
    }
    finally {
        if (c != null) {
            c.close();
        }
    }



  •  Forgive me if I'm wrong, but isn't it impossible/discouraged in VB(script) to Dim and assign in the same statement?

    So such code might be a holdover from previous long-time VB experience. 



  • I believe it is a "best practice" issue, I seem to recall learning that design in one of my programming books. Although as mentioned it would make sense if there was a try/catch block, possibly it is done in wait for said try/catch (possibly why said "best practice" exists) or said try/catch was removed.



  • I don't think so... If he couldn't assign them at all, then he couldn't assign them to null in the first place.

    VB.Net does allow people to dim and initialize on the same line, whereas vb6 and vbscript do not.  I would have set the object to what I needed it for, unless you have a reason not to. Either the try/catch above or something like this:

    someObject GetTheObject()

     {

     someobject newObject = null;

    If(Database.RunSomeQuery)

    {

        newObject = QueryResults.ConvertToSomeObject();

    }

    }

    sorry about the sloppy post, I'm not quite familiar on the tags.



  • I have some vague memory of a high school C++ class saying that it was bad to do that with pointers.  like the following happens on certain compilers....

    char foo[6] = "hello";

    char* bar = null;
    bar = foo;
    printf("%s", bar); // Prints "hello"

    char* bar2 = foo;
    printf("%s", bar2); // Prints gibborish because bar2 == &foo, when it should == foo.

    She was probably full of shit though.  That particular teacher definitely brought meaning to the phrase, "if you can't do, teach".  But I still got myself into the habit of always initializing things to null though.



  • It's good practice in C to always initialize a pointer, even if it's going to be set to something on the very next line (you never know when someone maintaining the code months or years later might put something else in-between the two lines), so it seems reasonable to assume that this is just a hangover from that.



  • It is definitely something he learned from some other language. In Java, it accomplishes nothing.



  • @VGR said:

    It is definitely something he learned from some other language. In Java, it accomplishes nothing.

    Does Java initialize all variables then? In C++ I can imagine doing it when some code might be inserted later on. If that piece of code throws an exception, at least your pointer has been initialized. It would however have no meaning for local variables, only globals or class members (unless there is some auto-wrapup for exceptions in Java).



  • usually all your pointers should be initialized to null to begin with in C++. Its just good practice. That way if it encounters a line of code like

     if(someObject != NULL)
    {

    delete someObject;

    someObject = NULL;

    }

    Its easier to prevent deleting an object twice. If you never set it to NULL, than it might even try to delete an object before it even allocated the object.



  • @TGV said:

    @VGR said:

    It is definitely something he learned from some other language. In Java, it accomplishes nothing.

    Does Java initialize all variables then? In C++ I can imagine doing it when some code might be inserted later on. If that piece of code throws an exception, at least your pointer has been initialized. It would however have no meaning for local variables, only globals or class members (unless there is some auto-wrapup for exceptions in Java).

     

    Java gives you an error if you try to do anything with a variable before assigning it.  Even something like:

    Integer x;

    if (x == null) ...

    Will cause javac to issue an error.



  • @TGV said:

    @VGR said:

    It is definitely something he learned from some other language. In Java, it accomplishes nothing.

    Does Java initialize all variables then? 

     

    Basically yes - instance and static fields are initialized to default values, and the compiler won't allow you to use uninitialized local variables.



  • @dhromed said:

     Forgive me if I'm wrong, but isn't it impossible/discouraged in VB(script) to Dim and assign in the same statement?

    So such code might be a holdover from previous long-time VB experience. 

    It's impossible in all pre-.NET VB derivates (VB6, VB script, VBA, etc...) but I'd be very astonished if it were discouraged in .NET again, given how muhc praise it got when it was introduced.

    But even if that style comes from too much VB before, it's still weird. After all, the guy did do an initial assignment in the Dim, the =null. If he had just copy-pasta'ed his existing VB code, wouldn't it rather be like

    Object foo;

    foo = myManager.getImportantObject();



  • It's because a lot of people consider it good practice to initialize all variables before use.  Same like something along the lines of:

    string baz = "";

    baz = DoSomethingThatReturnsAString(someParam); 



  •  If by good practice you mean retarded then yes this is good practice.



  • He could have been burned with variable scope, never figured out why, and ended up doing it this way every time. If you go from .NET where IF THEN blocks have their own variable scope and FOR loops have their own scope, then you move to javascript or actionscript and try to employ that same knowledge, you'll be scratching your head. I remember when I first started programming Flash. I had a hell of a time figuring out their definition of scope. shrugs maybe that's just me.


Log in to reply