Using NULLs as a return type

Todays Topic

Today I’d like to mention something that I see all too often in other developers code that I feel is asking for disaster. Todays entry is about using NULL as your return type. Although, it is easily done, it often not only indirectly causes new bugs, but also confuses other developers if you aren’t consistent, which I usually see happen with developers that return NULL. This is especially bad if the method you are working on that does this is inside of a framework that will be used by developers in their own projects (like an SDK).

Examples

There are plenty of examples that would prove my point here, but here are a few that get the point across.

Returning a List

Take the following Service layer call as an example

public List<string> GetTitleById(string id)
{
    if(String.IsNullOrEmpty(id))
      {
        return null

      }
    else
        {
            var list = new List<Title>();
            list.Add(new Title(){TitleName ="Software Developer"});
        }    

    return list;
}

Now lets say that you were to use the above code like the following

public void GetById(string id)
{
    var results =GetTitles(id);for( int counter =0; counter < results.Count(); counter++)  {
    MessageBox.Show(results[counter].Title);  }
}


If an empty or null Id is passed by the GetById method to the GetTitleById method, The results.Count() line would fail on a null reference because you are trying to access the property “Count” when the object that is returned is NULL. This can easily be remedied by doing something like the following:

public List<string> GetTitleById(string id)
{
    if(String.IsNullOrEmpty(id))
      {
        return new List<Title>();

      }
    else
        {
            var list = new List<Title>();
            list.Add(new Title(){TitleName ="Software Developer"});
        }    

    return list;
}

Using the same code to get the results would no longer fail and would just fall through the loop because counter == results.Count(), ‘0’.

Just food for thought next time you are coding something that will most likely be used by developers other than yourself. Even for yourself, you will see less bugs if you stick with this rule.

Hope You Enjoyed… Happy Coding!

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, ASP.NET, Best Practices, C#, Classes, Coding standards, Exception handling, Exceptions, MVC, Programming, Programming Paradims and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s