views:

643

answers:

1

Is there a better/shorter way to handle (lots of) null references, for example when I'm using LinqToXML.

I wrote this extention for XElement that handles it quite nicely, but maybe there is another way?

And what about the function name? "And" isn't really descriptive.

public static class XmlExtentions
{
    public static T And<T>(this T obj, Func<T, T> action) where T : XElement
    {
        return obj == null ? obj : action(obj);
    }
}

internal class Program
{
    private static void Main()
    {
        //create example xml element
        var x = 
          XElement.Parse("<root><find><name>my-name</name></find></root>");

        //old way
        var test1 = x.Element("find");
        if (test1 != null) test1 = test1.Element("name");
        Console.WriteLine(test1);

        //using the extentions method
        var test2 = x.Element("find").And(findme => findme.Element("name"));
        Console.WriteLine(test2);

        Console.ReadLine();
    }
}

PS: I know I could use XPath for this example, but that's not always the case in more complex cases.

+4  A: 

The overall approach is reasonable - although I'm not sure about the Func<T,T> which seems a bit restrictive. If you are limiting to returning the same thing, I wonder if just accepting the name (string) as the second arg wouldn't be easier?

Re naming - perhaps borrow from LINQ? This is essentaially a Select - how about SelectOrDefault:

public static TResult SelectOrDefault<TSource, TResult>(
    this TSource obj, Func<TSource, TResult> selector) where TSource : class
{
    return SelectOrDefault<TSource, TResult>(
        obj, selector, default(TResult));
}

public static TResult SelectOrDefault<TSource, TResult>(
    this TSource obj, Func<TSource, TResult> selector,
    TResult @default) where TSource : class
{
    return obj == null ? @default : selector(obj);
}

(edit) maybe with the additional XElement specific:

public static XElement SelectOrDefault(
    this XElement element, XName name)
{
    return element == null ? null : element.Element(name);
}
Marc Gravell
I like the first example, although I could just edit my code to "where T : class" and have almost the same result. But the ability to return other types is a nice extra!
Ward Werbrouck