5,666,132 members and growing! (14,122 online)
Email Password   helpLost your password?
Languages » C# » Utilities     Beginner License: The Code Project Open License (CPOL)

NonNullable Class Wrapper

By PIEBALDconsult

A wrapper to place the burden of checking a class reference for null on the calling method rather than the called method.
C#.NET 2.0, .NET, Dev

Posted: 11 Apr 2008
Updated: 11 Apr 2008
Views: 4,754
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 5.35 Rating: 4.44 out of 5
1 vote, 6.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
6 votes, 37.5%
4
9 votes, 56.3%
5

Background

A class reference may be null, but in many cases a method that accepts a class reference as a parameter requires that the reference be non-null.

(For the example snippets, we'll assume that returning 0 or -1 when a null is passed is not desirable.)

The naive thing to do in such cases is to simply allow .NET to throw a NullReferenceException the first time it is dereferenced:

public int CountOccurrences ( string String1 , string String2 )
{
    // Count the number of times String2 occurs in String1 and return that value
}

But this doesn't tell the calling method which value is null.

A smarter technique is to test the passed references, and throw a more informative exception:

public int CountOccurrences ( string String1 , string String2 )
{
    if ( String1 == null )
    {
        throw new System.ArgumentNullException 
            ( "String1" , "You must provide a string to search" ) ;
    }

    if ( String2 == null )
    {
        throw new System.ArgumentNullException 
            ( "String2" , "You must provide a string to find" ) ;
    }

    // Count the number of times String2 occurs in String1 and return that value
}

But this performs the check on each parameter every time the method is called.
In most cases, this is fine, but in some cases this seems unnecessary. Consider a usage of this method that is used to count all the occurrences of some string in the lines of a text file. In such a case, after reading each line, that line and the string to find are passed to this method whereupon the references are checked for null. That seems reasonable, but the second parameter will be checked on each call even though it doesn't change.

The test for null doesn't impose much of a performance hit, so this isn't really a performance issue. I'm more concerned here with readability and maintainability; the second snippet above is safer than the first but is perhaps harder to read and maintain.

What if we could write the method as simply as the first example, but with the safety of the second? Maybe something like this:

public int CountOccurrences ( NonNullable<string> String1, NonNullable<string> String2 )
{
    // Count the number of times String2 occurs in String1 and return that value
}

Implementation of My NonNullable<T> Structure

Unlike classes, structs can't be null, so I'll use a struct to wrap the class reference. Furthermore, wrapping a struct in a NonNullable<T> would be needless. So the beginning of our struct looks like:

public struct NonNullable<T> where T : class
{
    // Members
}

The struct needs only one field; it'll hold the class reference. The value won't change, so we can make it readonly, therefore we can also make it public and avoid writing a property for it:

public readonly T Value ;

Only one constructor is required; it'll simply perform the check for null and throw or store:

public NonNullable
(
    T Value
)
{
    if ( Value == null )
    {
        throw ( new System.ArgumentNullException ( "Value" , "That value is null" ) ) ;
    }

    this.Value = Value ;

    return ;
}

On second thoughts, many people don't like constructors that throw exceptions, so let's write one that won't throw. This requires that we have a fall-back position that will still yield a valid instance. Something like this ought to do:

public NonNullable
(
    T              Value
    ,
    NonNullable<T> IfNull
)
{
    if ( Value == null )
    {
        this.Value = IfNull.Value ;
    }
    else
    {
        this.Value = Value ;
    }

    return ;
}

Every type should override ToString():

public override string
ToString
(
)
{
    return ( this.Value.ToString() ) ;
}

By adding an implicit conversion to NonNullable<T>, the calling method need not know what's going on:

public static implicit operator NonNullable<T>
(
    T Value
)
{
    return ( new NonNullable<T> ( Value ) ) ;
}

However, there is a performance hit involved. Worse than that, the guidelines for implicit conversions state that they shouldn't throw exceptions, and this one may. And, as with the naive non-checking method, the caller won't know which parameter was null.

Add an implicit conversion from NonNullable<T> for convenience:

public static implicit operator T
(
    NonNullable<T> Value
)
{
    return ( Value.Value ) ;
}

Lastly, a pair of Coalesce methods to wrap the first non-null reference among those provided:

public static NonNullable<T>
Coalesce
(
    params T[] Values
)
{
    return ( Coalesce ( (System.Collections.Generic.IEnumerable<T>) Values ) ) ;
}

public static NonNullable<T>
Coalesce
(
    System.Collections.Generic.IEnumerable<T> Values
)
{
    if ( Values == null )
    {
        throw ( new System.ArgumentNullException 
            ( "Values", "No values were provided" ) ) ;
    }

    foreach ( T t in Values )
    {
        if ( t != null )
        {
            return ( new NonNullable<T> ( t ) ) ;
        }
    }

    throw ( new System.ArgumentException 
        ( "No non-null values were provided" , "Values" ) ) ;
}

Using the Code

Methods can be written as in the third snippet above:

public int CountOccurrences 
    ( NonNullable<string> String1 , NonNullable<string> String2 )
{
    // Count the number of times String2 occurs in String1 and return that value
}

And because I included the implicit conversion, the calling method doesn't need to know what the called method is doing. In fact, the called method could be rewritten to use NonNullable parameters without affecting the caller.

A better practice, when calling a method that takes NonNullable parameters is to wrap the value separately from making the call, especially when a value is used many times without changing.

...

int count = 0 ;
string line ;

// Check this value only once!
NonNullable<string> safetext = new NonNullable<string> ( text ) ; 
NonNullable<string> safeline

while ( ( line = file.Read() ) != null )
{
    safeline = new NonNullable<string> ( line )
    count += CountOccurrences ( safeline , safetext ) ;
}

...

(Note to self: Test with in, out, and ref parameters.)

Performance

As stated above, this technique is not intended to improve performance; it is meant to reduce code and therefore maintenance. Having said that; my testing has shown a modest improvement in performance at best and a considerable performance hit at worst. After ten million calls to a method-that-takes-a-string:

x 10000000 00:00:00.1204537  // with no check
x 10000000 00:00:00.1416811  // check the parameter on each call
x 10000000 00:00:00.3597213  // implicit conversion to NonNullable<string> on each call
x 10000000 00:00:00.2382019  // direct call to the NonNullable<string> constructor 
                             // on each call
x 10000000 00:00:00.1083238  // call the NonNullable<string> constructor once

History

  • 2008-04-11: First written

License

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

About the Author

PIEBALDconsult


BSCS 1992 Wentworth Institute of Technology

Originally from the Boston (MA) area. Lived in SoCal for a while. Now in the Phoenix (AZ) area.

OpenVMS enthusiast, ISO 8601 evangelist, photographer, opinionated SOB

---------------

ZagNut’s Law: Arrogance is inversely proportional to ability.

"Omit needless local variables." -- Strunk... if he'd taught programming

"Software Engineers don't have Trophy Wives; they have Presentation Layers."

"We learn more from our mistakes than we do from getting it right the first time."

"I'm an old dog and I like old tricks."

"Sometimes the envelope pushes back and sometimes you get a really nasty paper cut."

"A method shall have one and only one return statement."

My first rule of debugging: "If you get a different error message, you're making progress."

My golden rule of database management: "Do not unto others' databases as you would not have done unto yours."

My general rule of software development: "Design should be top-down, but implementation should be bottom-up."

"Today's heresy is tomorrow's dogma."
or
"Today's dogma is yeterday's heresy."

"The registry is evil."

"Every tool is a hammer."

"You can reinvent the wheel, but you shouldn't reverse-engineer it."
Occupation: Software Developer (Senior)
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralExcellentmember-Dy2:09 14 May '08  
GeneralRe: ExcellentmemberPIEBALDconsult14:01 14 May '08  
GeneralNicemvpPete O'Hanlon10:46 13 Apr '08  
GeneralRe: NicememberPIEBALDconsult10:57 13 Apr '08  
GeneralUnfortunately, NonNullable.Value can be null [modified]memberDaniel Grunwald2:24 12 Apr '08  
GeneralRe: Unfortunately, NonNullable.Value can be null [modified]memberPIEBALDconsult4:54 12 Apr '08  
GeneralRe: Unfortunately, NonNullable.Value can be null [modified]memberPIEBALDconsult5:28 12 Apr '08  
AnswerRe: Unfortunately, NonNullable.Value can be nullmemberPete Appleton0:02 15 Apr '08  
GeneralRe: Unfortunately, NonNullable.Value can be nullmemberMember 431986514:59 4 May '08  
GeneralRe: Unfortunately, NonNullable.Value can be nullmemberDean Goddard15:06 4 May '08  
GeneralRe: Unfortunately, NonNullable.Value can be nullmemberPIEBALDconsult17:51 4 May '08  
GeneralInteresting!supporterMarc Clifton12:41 11 Apr '08  
GeneralRe: Interesting!memberPIEBALDconsult13:00 11 Apr '08  
GeneralRe: Interesting!supporterMarc Clifton13:21 11 Apr '08  
GeneralRe: Interesting!memberPIEBALDconsult14:27 11 Apr '08  
GeneralRe: Interesting!memberPIEBALDconsult15:58 11 Apr '08  
GeneralI wrote exactly the opposite code last nite!member leppie 12:22 11 Apr '08  
GeneralRe: I wrote exactly the opposite code last nite!memberPIEBALDconsult12:37 11 Apr '08  
GeneralRe: I wrote exactly the opposite code last nite!member leppie 12:55 11 Apr '08  
GeneralRe: I wrote exactly the opposite code last nite!memberPIEBALDconsult13:01 11 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Apr 2008
Editor: Deeksha Shenoy
Copyright 2008 by PIEBALDconsult
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project