Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have some static helper class:

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

    public static bool NotNull<T>(this T value) where T : class
    {
        return (!value.IsNull<T>());
    }

    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }

    public static bool NotNullAndNotEmpty(this string value)
    {
        return (!value.IsNullOrEmpty());
    }

    public static bool NotNullAndNotEmpty(this ICollection value)
    {
        return (value.NotNull<ICollection>() && (value.Count > 0));
    }

    public static bool IsNullOrEmpty(this ICollection value)
    {
        return (!value.NotNullAndNotEmpty());
    }
}

It is comfortable (habit) for me to use this helper, but I am not sure, that it is correctly (worth it), how about performance? probably there are any other reasons don't use it?

Thanks.

share|improve this question
Isn't IsNullOrEmpty just replacing the same exact extension? – Fuji Dec 7 '11 at 14:27
I don't see any problems with this code. – kol Dec 7 '11 at 14:28
1  
Perf is a non-issue. Having somebody else read your code and have no clue why it works is an issue. These extension methods look like instance methods in the code, seeing them being use on null objects is a nasty speed-bump. That extension methods support a null object is frankly a bit of a bug, not a feature, given that this is so very unlike the way the rest of the language works. But I'm sure you'll disagree :) – Hans Passant Dec 7 '11 at 14:29
@Fuji String.IsNullOrEmpty is static. Helper.IsNullOrEmpty can be called as if it was a non-static method. – kol Dec 7 '11 at 14:29
@kol and yet, it is still a static method, in every sense, really. – Andrew Barber Dec 7 '11 at 14:34
show 5 more comments

migrated from stackoverflow.com Dec 7 '11 at 16:26

3 Answers

up vote 7 down vote accepted

This gets to be a sticky point. There are those who will argue fervently that an extension method should never work on a null reference, and there are those who are fine with that.

Personally, I don't mind extension methods on null if the name is a clear indicator that null is a possibility (like IsNullOrEmpty) but as I said, that's my opinion and may not match others.

Don't worry about the performance, method calls are not a serious impact and check methods like this eliminate some of the readability concerns over compound expressions when maintaining the code. After all, that's what string.IsNullOrEmpty() does, but it does it through a static method instead of an extension method.

So the only concern, really, you may get push back that these are extension methods as opposed to ordinary static methods, but you'll get differing opinions on whether calling an extension method on null is bad/okay.

One could argue Microsoft disallows LINQ extension methods on null sequences, but then again Microsoft also doesn't disallow extension method calls on a null source in general.

So, the choice is yours, I don't have an issue with them since the names clearly indicate null is an accepted possibility, but I can see both sides of the argument.

On a side note, I'd keep your naming consistent:

  • NotNull -> IsNotNull
  • NotNullAndNotEmpty -> IsNotNullAndNotEmpty
share|improve this answer
I'd really like to see someone's argument on why it is fundamentally wrong to have an extension method for potentially null values. – oscilatingcretin Dec 7 '11 at 15:01
@oscilatingcretin: I think the main concern is that extension methods are designed to look like instance methods, but in that particular aspect they differ from instance methods, thus it could be misleading as a maintenance/readability issue. I'm not saying that's a reason not to do it, but you do get that argument a lot. – James Michael Hare Dec 7 '11 at 15:04
But an instance method can still be called when the object is null. Either way, you get a null reference exception regardless wehether or not it is an instance method or extension method. Maybe I am not grasping the merits of the argument. – oscilatingcretin Dec 7 '11 at 15:10
Welcome :) value.NotNull() extension method cannot be used in Linq2Sql: ...Where(i => (i != null)) => works ok, ...Where(i => i.NotNull()) => occurs run-time exception. – Maxim Polishchuk Dec 7 '11 at 15:10
@oscilatingcretin: Not necessarily, there's nothing that says your extension method can't handle null (like his does), but an instance method can never handle null was my main point. Also, an extension method should never directly throw a NullReferenceException, that's a system exception that only the CLR should throw. Unless you meant indirectly by dereferencing, then sure! – James Michael Hare Dec 7 '11 at 15:13
show 2 more comments

I don't see exactly what's the win for you with this helper class. Most of the methods you have shown seem like some overhead. For example:

public static bool IsNullOrEmpty(this string value) 
{ 
    return string.IsNullOrEmpty(value); 
} 

Although it will provide you a fluent syntax in your code. Performance will not bite you, your not doing very heavy things inside your extension methods.

share|improve this answer
1  
It can be called as if it was a non-static method: s.IsNullOrEmpty(). I guess he prefers this format over string.IsNullOrEmpty(s). – kol Dec 7 '11 at 14:49
  • IsNull: What does this buy you? Just say if (whatever == null)
  • NotNull: Same as above. What's so bulky about if (whatever != null)?
  • IsNullOrEmpty: What does this buy you? You're just putting existing logic into a helper method without any special supporting logic.
  • NotNullAndNotEmpty: Same as previous. What's wrong with !string.IsNullOrEmpty(value)?
  • NotNullAndNotEmpty(this ICollection value): Why would you name it like this? Call it IsNullOrEmpty so you can test for a positive.
  • IsNullOrEmpty: Why on Earth are you convoluting your code with this? All you're doing is creating a method that calls NotNullAndNotEmpty with a !. Ditch this method, rename the previous like I suggested, and just go like !IsNullOrEmpty when you need to check for a false value.

Not to be rude or anything, but I think your helper methods really don't help at all since they're only encapsulating existing logic. The only one that has merit is the logic within NotNullAndNotEmpty for ICollection (although you need to rename it like I suggested).

share|improve this answer
There is just one reason, I am typing whatever.IsNull() (and other) faster than whatever == null, because VS intelligence helps me. – Maxim Polishchuk Dec 7 '11 at 15:15
I can see how it could offer a linear form of coding by eliminating expressional forethought. I think your methods are okay if you're a lone developer for your own project. However, if I were on a team of developers and saw one developer writing "self-helper code", I'd never use them since they really don't buy anything truly useful for the big picture. – oscilatingcretin Dec 7 '11 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.