Early Programmers: Initializing an empty object to be replaced

This is my first programming post and I thought I would start with something that I see all the time with programmers who are just seasoning. This is a common mistake I used to make as well and I believe it stems from the miss understanding of how variables and references relate to one another. Thankfully .NET based languages (including those which compile down to CLI) manage object deletion automatically. Otherwise you would have a pretty serious memory leak.

Lets look at a common example. We are using C# here but Java should look almost one to one. For instance if we have a simple class:

class SomeObject
{
     string someString = "";
     int someInt = 0;
}

and so I implement it as follows

public void main() //Lets assume this is our entry point
{
      SomeObject obj = new SomeObject();

      //someBLL is an arbitrary static class that gets your data from the database or some other source and creates a new object to hold that data

      obj = SomeBLL.fetchObjectData();
}

What has happened here is a new object reference has been created, pointing to an object, only to be repointed to another object. Considering the new object:

     SomeObject obj = new SomeObject();

is quickly overwritten by some new object that someBLL.fetchObjectData(); returns. As mentioned before, C# has a garbage collector to deal with objects that are being disposed, but in reality this is a memory leak, garbage collector or not. For those who are just starting programming in C# (and Java), there are a few things that needs to be outlined:

  • “obj”, as I have named it above, is a reference to an object in the memory. It is not actually the object itself, but rather points to some location in memory at which the object is located
  • If you use the same reference again, for instance in obj = someBLL.fetchObjectData(); as mentioned above, the variable “obj” now points to a new location in memory for a completely different object.
  • When you re point your reference, you leave the old object still in memory taking up space.

So what is the solution to this? It’s very simple. Simply don’t create a new object before repointing your reference somewhere else. To correct the code above, we can do the following:

 public void main() //Lets assume this is our entry point
 {
      SomeObject obj = SomeBLL.fetchObjectData();
 }

However it is common that doing something like this is not quite so simple. For instance, what if I need to decide from where my data is coming from? I am getting the same object type back from either, but they load the object with different data. Consider the following code:

//Using an int here might not be most preferred (use an enum instead), but it will suffice
pubic SomeObject getMyData(int dataType)
{
      SomeObject obj;
      switch (datatype)
      {
           case 0:
           {
                obj = SomeBLL.getDataA();
           }
           case 1:
           {
                obj = SomeBLL.getDataB();
           }
      }
      return obj;
}

With the code above, it is very tempting to initialize the “obj” reference with a default value. Before it hits the case statement, it equals “null” (null == 0 in C/C++). However, if you do initialize it with a blank object you are more than likely to run into a deceivingly existing data, even when you have passed in 3 into your function. If you use the function wrong, you’re better with your code returning a null object. Otherwise you will end up with an empty object eventually somewhere down the road and its much harder to track down why you have empty objects.

Regardless, this is the approach you want to maintain. Provided vast memory sizes and a garbage collector I have encountered this mistake even with somewhat seasoned programmers. For some it might be a tough habit to break, but we must never forget we cannot pollute the heap with empty objects; otherwise its bad programming.

Leave a comment