Click here to Skip to main content
Click here to Skip to main content
Technical Blog

A lot about Nothing ( or null)

, 17 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
I believe that the code you write should express your intended functionality in a manner that reads almost like a story.  To achieve this, you need to use a coding style that includes descriptive names for methods, properties, and fields.  One area that a lot of people don’t consider is what their c

I believe that the code you write should express your intended functionality in a manner that reads almost like a story.  To achieve this, you need to use a coding style that includes descriptive names for methods, properties, and fields.  One area that a lot of people don’t consider is what their code says when they use null in C# or Nothing in VB.   Used incorrectly, it can result in confusing code and subtle errors.

So lets start our null using code to English translator and examine some code idioms.

Initializing a variable to null

This is the simplest of statements:

ISpoon aSpoon = null;

This means “I’m probably going to use a spoon at some point in this class or method, but I haven't decided which one, haven’t been given one, or haven’t made one yet.”  Another way of say this is “There is no spoon”.

Notice that the variable was defined in terms of the interface ISpoon. The reason for this is that there are many different types of spoon (silver, coffee, sugar, serving, wooden, …) and I want to be able to use the most appropriate for a given situation.

Assigning null to a variable

Take the following code:

ISpoon aSpoon = GetAWoodenSpoonFromTheDrawer();

theCook.StirsSauceWith(aSpoon);
aSpoon = null;

In this situation you have gotten a spoon and used it.  The assignment says “I’m done with this spoon and don’t care about it anymore.  I’ll just drop it wherever, and the cleanup crew ( the garbage collector in .NET) will pick it up, wash it, and put it back where someone else can use it”.

Returning null from a method

Methods will typically return either a single value or a collection of values.  For the single value returning method, returning null will mean something different depending on the intent of the method.  For example,

ISpoon aSpoon = GetAWoodenSpoonFromTheDrawer();

would mean to me that there wasn’t a wooden spoon in the drawer.  Now you will have to check that aSpoon is not null before using it and chose a different type of spoon, or get a wooden spoon elsewhere.  Personally I find this type of code detracts from the flow of the code’s story and prefer to throw an exception  as the exception can tell you what went wrong.  So instead of

ISpoon aSpoon = GetAWoodenSpoonFromTheDrawer();

// couldn't get a spoon from the drawer
if (aSpoon == null)
    aSpoon = FindAWoodenSpoonElsewhere();

// use the spoon if one was found.
if (aSpoon != null)
{
    ...
}

I would write

ISpoon aSpoon = null;
try
{
    ISpoon aSpoon = GetAWoodenSpoonFromTheDrawer();
}
// there isn't a spoon in the drawer
catch (NoSpoonFoundException)
{
    // look elsewhere
    try
    {
        aSpoon = FindAWoodenSpoonElsewhere();
    }
    // No spoon found anywhere
    catch(NoSpoonFoundException)
    {
        aSpoon = BuyAWoodenSpoon();
    }
}
// there was a spoon, but it was dirty
catch(DirtySpoonException)

{
    WashDishes();
    aSpoon = GetAWoodenSpoonFromTheDrawer();
}

 

// use the spoon if one was found.
if (aSpoon != null)
{
...
}

For the case where the method returns a collection of items, as in

ICollection<ISpoon> mySilverSpoons = GetAllSpoonsThatAre("silver");

returning a null does not say “There are no spoons”.  Rather, it says “I couldn’t create a collection of spoons”.  In this case I would return a collection with no items.  As before, errors should throw exceptions. This means that you can safely iterate the collection returned.  The usage pattern would look like:

try
{
    ICollection<ISpoon> mySilverSpoons = GetAllSpoonsThatAre("silver");

    foreach(var spoon in mySilverSpoons)
    {
        Polish(spoon);
    }
}
catch(MissingSilverwareBoxException)
{
    CallPolice();
}
catch(NoPolishAvailable)
{
    Order("Polish");
    Reschedule("Polish Silver Task");
}

Due to time constraints, I’m not going to address null parameters in this blog.

I hope this has given you something to think about when architecting/designing you applications.  Just remember Nothing (or null) can hurt you if you don’t think about Nothing.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Matthew Dennis
Software Developer (Senior) The Code Project
Canada Canada
Matthew works on improving the performance and experience of the Code Project site for users, clients, and administrators.
 
Matthew has more years of software develeopment, QA and architecture experience under his belt than he likes to admit. He gradutated for the University of Waterloo with a B.Sc. in Electrical Engineering. He started out developing micro-processor based hardware and software including compilers and operating systems.
His current focus is on .NET web development including jQuery, Webforms, MVC, AJAX, and patterns and practices for creating better websites.
He is the author of the Munq IOC, the fastest ASP.NET focused IOC Container.
His non-programming passions include golf, pool, curling, reading and building stuff for the house.
Follow on   Twitter

Comments and Discussions

 
GeneralNot really PinmemberDaProgramma23-Mar-10 1:41 
GeneralRe: Not really PinstaffMatthew Dennis23-Mar-10 7:10 
GeneralRe: Not really PinmemberDaProgramma23-Mar-10 8:18 
GeneralSpoon-power! PinmemberCurtainDog22-Mar-10 22:26 
GeneralRe: Spoon-power! PinstaffMatthew Dennis23-Mar-10 7:03 
GeneralI don't agree with using try catch PinmemberMicke D22-Mar-10 13:33 
GeneralRe: I don't agree with using try catch PinstaffMatthew Dennis22-Mar-10 14:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 17 Mar 2010
Article Copyright 2010 by Matthew Dennis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid