Avoid returning "null" and use the Null Object pattern?

When I build applications and add methods to return a list of objects, I make it robust. So I always return an instance of the list instead or returning null. The reason is because I like the use of Count and also use foreach. I don't want to add extra code to see if the method returns null. For example:

public IList<string> GetCultures()
{
    IList<string> cultures = new List<string>();

    //...

    return cultures;
}

 

Then when I make a call to the GetCultures method I can see if it contains any cultures by using Count, and if I need to list Cultures I can also use foreach without getting a null exception.

IList<string> cultures = GetCultures();

if (cultures.Count > 0 )
   //..


foreach (var culture in GetCultures())
   //..


If I prefer Robustness before Correctness I return null if I only need to return a single object, for example:

public static Culture GetCulture(string cultureCode)
{
    Culture culture = null;
            
    //..

    return culture;
}

 

In this case I need to see if the returned object is null or not before using it.

Culture culture = GetCulture();

if (cultures !=null)


I could also throw an exception if the cultureCode could not be located, in that way I will never return null. But I try to avoid throwing exception and use try catch block when I try to get an object. The reason is performance and also skip adding several try catch block in my code.

public static Culture GetCulture(string cultureCode)
{
    Culture culture = null;
            
    //Can't locate the culture throw an exception

    return culture;
}

 

Culture culture = null;

try
{
    culture = GetCulture();
}
catch
{
    //...
}


I only throw an exception when it's really an exception, for example the cultureCode was in a correct format or wasn't specified etc. Everything is based on the type of application, and if Robustness is more important than Correctness. Another solution is to use the Null Object Pattern and always return an instance of an object.

Culture culture = GetCulture();

if (cultures.IsNull)


Or use the Cultures other members and not get a null exception. The Null Object Pattern is also useful if I always want to return a default value. For example if I want to get a Customer and the Customer can't be located, I could return a John Doe ;) I don't use the Null Object Pattern so often in my applications.

What solution do you use or prefer when returning a list or single object and why?

Published Thursday, May 22, 2008 10:57 AM by Fredrik N

Comments

# re: Avoid returning "null" and use the Null Object pattern?

Thursday, May 22, 2008 6:29 AM by Egil

As I am reading you post, I start thinking about creating an extension method on object that would let me do object.IsNull(), it would simply be an overload of object == null, but would read better. Sort of like Nullable types HasValue.

How would the Null Object look like? Something like NullObject<T>?

# re: Avoid returning "null" and use the Null Object pattern?

Thursday, May 22, 2008 7:25 AM by Vimpyboy

Egil: I was thinking about the same thing as you. I guess I will write an extension method for object that has an IsNull method.

Fredrik: I totally agree with you and I do the same thing as in your examples. I prefer to return an empty collection instead of null.

OrderCollection orders = Order.GetOrders();

foreach (Order in orders) //returns an exception if null, is ignored if empty

 Order.Delete();

I prefer to not have any orders deleted than get an exception. :-)

# re: Avoid returning "null" and use the Null Object pattern?

Thursday, May 22, 2008 7:42 AM by sheshe

I Think throw exception is Better than another one.

because every one can read my code and understand it.

in Layered Architecture We Can Read Exceptions And Fix Them.

# re: Avoid returning "null" and use the Null Object pattern?

Thursday, May 22, 2008 7:47 AM by Luis Abreu

Hello.

Well, most of the times, I'm using your approach and I'm returning null for non existing single objects and an empty list for a non existing collection of objects. Null objects are great, but I'm not sure if they're useful on the UI (which is where most of my objects end up)...

# re: Avoid returning "null" and use the Null Object pattern?

Thursday, May 22, 2008 10:47 AM by Israel Aece

Hello,

I'm using the following technique in collections (code snippet can help):

if(coll != null && coll.Count > 0) {...}

Is there a overhead in instance a empty List<T>?

# re: Avoid returning "null" and use the Null Object pattern?

Sunday, May 25, 2008 9:41 AM by Scott

I think both approaches have good and bad features. The nice thing about using the Null Object pattern is that it makes null a first-class citizen in the language.

# re: Avoid returning "null" and use the Null Object pattern?

Monday, May 26, 2008 5:52 PM by Dani

I don't think that it is a good behavior to ask an object if it's null or not... it's just misleading. In fact, that means that the object itself (which is not existent if it's null) knows about his null-state.

# Tips för returvärden från metoder - Fredrik Normén

Tuesday, May 27, 2008 5:13 AM by Johan Lindfors

Fredrik Norm&#233;n som &#228;r MVP f&#246;r sina insatser runt ASP.NET tar i ett mycket bra blogginl&#228;gg

# re: Avoid returning "null" and use the Null Object pattern?

Tuesday, May 27, 2008 4:36 PM by Fredrik

While I can see the advantage of the Null Object Pattern, I am not quite a fan of it. I don't always do like this (I tend to dislike you-must-always philosophies, since there will sometimes be cases where they simply do not fit in): if a method's return type is a list of some kind, it should return such an instance that may or may not be empty. If a method's return type is a single object, it should throw an exception if the returned object would be vital for the continued execution of the program. If the return type is a single object, that is not vital for continued execution, a null reference could do fine.

Now, how do I decide whether a certain object is vital for the caller or not? That's not always obvious and when in doubt, I think I prefer to throw an exception. The main reasons are that exceptions are obvious; they force the developer of the calling code to make an active decision. It makes erroneous calls clear earlier in the development cycle, so that you can write the proper checks to avoid them at a higher level in the code. Also, a null reference can travel some distance from the call that returned it until it actually causes some trouble, and so it may be trickier to troubleshoot. But I would say that an object instance, having IsNull = true might travel even further, being even trickier to troubleshoot.

# re: Avoid returning "null" and use the Null Object pattern?

Friday, May 30, 2008 5:08 AM by Anders Josefsson

It is obvious that you should return an empty list when the function worked as intended and that there is not any objects to be fetched, but if something goes wrong in that function, it is probably better to give the user of the code a hint. The bool TryX(out val) pattern were you have a logger in place is probably the optimal solution.

IsNull() or == null is probably not the same when it comes to performance. == null is a more common pattern than IsNull() too (espcially as extension methods is a rather new addition to C#).

# re: Avoid returning "null" and use the Null Object pattern?

Thursday, June 19, 2008 8:50 AM by Martin Bring

When returning a single object I return null if not found. Using "Null Object Pattern", in this case, just adds a "Special Case" to check for instead of null.

To "return" a collection I prefer to pass it as a parameter, instead of returning it, and fill the list in the method. That will be easier to understand for the developer, especially if you name the method ok :). He/she creates the list him/herself and knows it is not null (you cannot even pass a null reference as "ref") and can use Count afterwards.

IList<string> cultures = new List<string>();

FillCurrentCultures(ref IList<string> cultures);

if(cultures.Count>0){

// ...

}

# re: Avoid returning "null" and use the Null Object pattern?

Sunday, June 22, 2008 1:16 PM by mehfuzh

I prefer , returning empty list rather null, i guess .net framework design guidlines like empty list as well :-)

# re: Avoid returning "null" and use the Null Object pattern?

Sunday, July 13, 2008 8:27 PM by Kraig

Fredrik,

I would like to pick your brain about some code that you wrote a few years ago and placed in the following blog about expanding and collapsing rows in a gridview:

forums.asp.net/.../717788.aspx

It is awesome code. So simple. I am wondering if you could give me some help with Expand All / Collapse All functionality. I can't figure out how to do it. Thank you so much.

# object null or not object

Tuesday, July 29, 2008 3:41 AM by object null or not object

Pingback from  object null or not object

Leave a Comment

(required) 
(required) 
(optional)
(required)