views:

452

answers:

5

Martin Fowler's Refactoring discusses creating Null Objects to avoid lots of

if (myObject == null)

tests. What is the right way to do this? My attempt violates the "virtual member call in constructor" rule. Here's my attempt at it:

    public class Animal
    {
        public virtual string Name { get; set; }
        public virtual string Species { get; set; }
        public virtual bool IsNull 
        { 
            get { return false; }
        }
    }

    public sealed class NullAnimal : Animal
    {
        public override string Name
        {
            get{ return "NULL"; }
            set { ; }
        }
        public override string Species
        {
            get { return "NULL"; }
            set { ; }
        }
        public override bool IsNull
        {
            get { return true; }
        }
    }
+15  A: 

Go look up the amount of pain that interesting concepts, such as DbNull, have caused and think about if this is actually a good idea.

Protip: if you are constantly checking for null references, you probably should rethink the API a bit to help preclude null objects closer to the top of the stack.

Protip II: having something throw an exception when there is an unexpected null is actually fine and dandy. Things should go boom if you have nulls where there shouldn't be null.

Wyatt Barnett
+1. Null pattern is an anti-pattern IMO. Throw for unwanted nulls and check for nulls when they're allowed.
Randolpho
The idea of the NullObject pattern is that things don't go boom - especially in producton.
Supertux
@Wyatt: http://jeremyjarrell.com/archive/2007/08/01/46.aspx has a nice demonstration of the benefits of null objects...but I'm not a big fan of them, personally.
Brian
There is a place for null objects, such as sentinel nodes in a red-black tree, but I'm not convinced that they're generally a good idea, for the reasons mentioned by Randolpho.
Steven Sudit
@supertux: which is better--app crashing and not sending bad data or sending bad data because you have some wierd anti-null fetish?
Wyatt Barnett
@Wyatt It depends on the software really, in some cases, perhaps you wouldn't use a NullObject pattern. I'm not a fetishist - the question was about NullObjects. For example, in aviation or medical software - app-crashing is probably a bad thing. But in My Animals Project probably not so bad.
Supertux
It's not some kind of "anti-null fetish". Calling it that not only disrespects the original poster, but there are some cases where an API could benefit by being forgiving. Comparing to DBNull / null isn't accurate, as DBNull does not inherit from null. Since NullAnimal is an Animal, passing NullAnimal to something expecting an Animal shouldn't cause problems. You can't say that about something that passes DBNull to something that expects null.All that said +1 for prompting thought on whether it's a good idea. In most cases, I tend to agree it is not.
Paul Hooper
+3  A: 

Please see these links for both the design pattern itself (Null Object Design Pattern) and an implementation (in C#):

The Design Pattern:

Null Object pattern - Wikipedia
Null Object Design Pattern
Introduce Null Object (contains some implementation code)

An Implementation:

The Null Object Pattern

CraigTP
+2  A: 

The point of the Null Object pattern is that it doesn't require a null check to prevent a crash or error.

For example if you tried to perform an operation on the Species property and it was null - it would cause an error.

So, you shouldn't need an isNull method, just return something in the getter that won't cause the app to crash/error e.g.:

public class Animal
{
    public virtual string Name { get; set; }
    public virtual string Species { get; set; }
}

public sealed class NullAnimal : Animal
{
    public override string Name
    {
        get{ return string.Empty; }
        set { ; }
    }
    public override string Species
    {
        get { return string.Empty; }
        set { ; }
    }
}
Supertux
Fine, but this still violates the virtual member call in constructor rule.
Sisiutl
Technically not, because there is no constructor.
Paul Hooper
+1  A: 

You only use this approach if it is appropriate. Your example of an Animal object might not be a good example because it doesn't present an appropriate case where you would use this approach. For example:

Animal animal = new Animal();

if (animal.tail == null)
{
    //do nothing because wagging a tail that doesn't exist may crash the program
}
else
{
    animal.wagTail();
}

In this example, you should build the Animal object so that if the animal doesn't have a tail, it can successfully handle the wagTail() command without crashing.

Class Animal
{
    Tail tail;

    void wagTail()
    {
        if (this.tail == null)
        {
            //do nothing
        }
        else
        {
            this.tail.doTheWag();
        }
    }
}

Now you don't need to do a null check, but can just call animal.wagTail() regardless of whether the animal has a tail or not.

tyriker
irregardless /facepalm
Ron Warholic
I'm online and look like an idiot. What else is new?
tyriker
Fixed it for you.
Dour High Arch
+2  A: 

I tend to agree with Wyatt Barnett's answer in that you should show restraint when creating these kinds of "null" objects. That said, there are some nice reasons for doing so. On occasion.

I also tend to agree with Supertux's answer in that the whole point of a null object is to not need to check whether or not it is null, so you should lose the IsNull property. If you really feel you need the IsNull property, then read Wyatt's response again and reconsider.

And thank you CraigTP for the nice links for more info. Good stuff.

Now I will assume that in your real code you actually have a constructor that is attempting to set the values of Name or Species (whatever your real code equivalent might be called). Otherwise, why would you get the "virtual member call in constructor" warning/error? I've run into a couple of similar problems when using the newfangled MyProperty { get; set; } shortcut myself (particularly when used in structs, and don't get me started about serialization versioning). Your solution is to not use the shortcut, but instead do it the old-fashioned way.

public class Animal {
 protected Animal() { }

 public Animal(string name, string species) {
  _Name = name;
  _Species = species;
 }

 public virtual string Name {
  get { return _Name; }
  set { _Name = value; }
 }
 private string _Name;

 public virtual string Species {
  get { return _Species; }
  set { _Species = value; }
 }
 private string _Species;
}

public sealed class NullAnimal : Animal {
 public override string Name {
  get { return String.Empty; }
  set { }
 }
 public override string Species {
  get { return String.Empty; }
  set { }
 }
}

This solves the problem of setting your virtual properties in the constructor. Instead, you are setting your private field values (something you don't have the ability to reference if you use the shortcut). For extra credit, compile both methods, and use the Reflector to look at the resulting assemblies.

The more I use the { get; set; } shortcut, the more I dislike it.

Paul Hooper