Scott Dorman

blog

  Home  |   Contact  |   Syndication    |   Login
  513 Posts | 9 Stories | 462 Comments | 55 Trackbacks

News


Post Categories

Image Galleries



Creative Commons License


Microsoft MVP


MCP Profile


Subscribers to this feed

TwitterCounter for @sdorman

Locations of visitors to this page

View blog authority

Add to Technorati Favorites

Windows Live Alerts

Support This Site

IM me

Scott [MVP]

Get Free Shots from Snap.com

Community Credit Hall of Fame

Get Feedghost

AddThis Social Bookmark Button

Xobni outlook add-in for your inbox

TechEd Bloggers

Party with Palermo

PDC 2008

Windows Live Translator

Twitter












Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

My earlier post on the Null Object pattern led to a few critiques about the fact that I was presenting an extension method to do what is essentially a very simple logical test, and, as a result, doesn’t provide much value.

For reference, here is the original extension method from that post

public static class NullObjectExtenstions
{
    public static bool IsNull(this object source)
    {
        return (source == null);
    }
}

Yes, this is an extremely simple method and performs a very basic logical test. However, consider the static IsNullOrEmtpy function on String (taken from Reflector):

public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
    return true;
}

I would argue that this is a relatively simple method and performs two very basic logical tests. What is the benefit to using IsNullOrEmpty? In a word, consistency. By using IsNullOrEmpty, I know that every time I need to perform this test it is executing the exact same code in the same order. I don’t have to worry about someone accidentally testing “Length == 0” before the “!= null” and causing a NullReferenceExcpetion.

This same benefit applies to the IsNull extension method. It introduces that consistency (although I agree this test is still very trivial) for testing nullability. As I mentioned in my comments, the better choice for the extension method would probably have been an IsNotNull method. Here is a more complete NullObjectExtensions class:

public static class NullObjectExtenstions
{
    public static bool IsNull<T>(this T source) where T : class
    {
        return (source == null);
    }

    public static bool IsNotNull<T>(this T source) where T : class
    {
        return (source != null);
    }
}
Technorati Tags: ,
posted on Friday, July 04, 2008 9:51 AM

Feedback

# re: Null Object pattern follow up 7/5/2008 1:52 PM Anthony Trudeau
I'm sorry I have to disagree with your logic. Granted, IsNullOrEmpty is a pretty simple method; however, it does encapsulate an actual algorithm (simple as that algorithm might be). Where as adding an extension method to check if an object is null does nothing more than add another level to the call stack. Object already implements this through the Equals method (and of course you can use the == operator).

You indicate consistency as a reason for using the extension method, but the example you cite can be easily detected by thorough unit testing. I don't see using IsNullOrEmpty or == null as appropriate as being any less consistent.

However, if you're dead set on this approach maybe it would be better to have a IsValidState extension method -- checking for null would be part of it, but then it would check to see if the object implemented a particular interface vis a vis a Visitor pattern, and if so call that method to perform the state validation.

# re: Null Object pattern follow up 7/6/2008 5:55 AM Craig Nicholson
I agree with you Anthony. The value of adding extension methods is primarily to extend an existing class with a common pattern or algorithm for ease of use. But at the end of the day its up to personal preference. I personally wouldn't add such a simple object extension method due to the theoretical performance cost of calling it.

However taking it further an object extension that test if an argument to a method is null or out of range and throws the appropriate argument exception would be one place I'd use object extensions.

# re: Null Object pattern follow up 7/6/2008 9:58 AM Scott
Anthony, I'm not "dead set" on any approach. My original post was actually talking about the entire idea of a null object pattern where nullability is treated as a first-class citizen in the runtime. The idea behind the extension method was to introduce a pattern that could help thinking and treating objects in that way. Obviously, to introduce the real pattern would require runtime changes in the CLR, but the extension method helps promote the concept.

# re: Null Object pattern follow up 7/6/2008 10:09 AM Scott
Craig, you are correct in the statement you made about the value of extension methods. At the end of the day, it is all about personal preference since the IL generated by the compiler is exactly the same.

I don't think the theoterical performance cost of calling an extension method is worth the effort of even considering it as a reason not to use them.

The idea of using an extension method for argument validation is an interesting one, including the idea of testing to see if an argument is not null since that is the extension method presented in this post.

# re: Null Object pattern follow up 7/13/2008 1:54 PM Anthony Trudeau
Maybe that's where we disagree. The fact that null is a language construct indicates to me that it is a first class citizen.

# re: Null Object pattern follow up 7/14/2008 7:26 AM Scott
Anthony, I see where and why you would say that since null is already a language construct it is a first class citizen. However, if it were a first class citizen, then the rules of operator lifting would apply...whereby calling a method on a null object would result in a null not a NullReferenceException.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 1 and 3 and type the answer here: