Ah, string.IsNullOrEmpty. It was love at first sight when I found that simple yet extremely useful method. It's quite often you want check if a string is null or empty. Instead of checking if the string is null and then checking if it is of zero length (or god forbid checking if it equals string.Empty) you can call this this method and it does it for you. Not only does it save you from unnecessary keystrokes, it also increases readability.
string s = null;
if(!string.IsNullOrEmpty(s))
PerformWork(s);
instead of
string s = null;
if(s != null && s.Length > 0)
PerformWork(s);
Well, you probably get my point, it's awesome. But I found that I still write if-statements checking for nullity and length of lists, hashsets and other collections. I can't quite figure out why the BCL team didn't include a corresponding method for collections (or perhaps they did? Enlighten me plix!). Don't despair though, uncle Markus is here to help.
using System.Collections;
namespace freakcode.Utils
{
public static class Collection
{
/// <summary>
/// Checks if the supplied collection is null or empty
/// </summary>
/// <param name="il">The collection</param>
/// <returns>True if the collection is null or empty, false otherwise</returns>
public static bool IsNullOrEmpty(ICollection ic)
{
return (ic == null || ic.Count == 0);
}
}
}
It's so simple it's almost embarrassing to post but it sure increases readability and it's a really great method to integrate and enforce in you own repository of utility methods.
var employees = new List<string>() {"Bob", "Alice", "Tom" };
if(Collection.IsNullOrEmpty(employees))
FireTheirAsses(employees);
You could rename/copy this to List.IsNullOrEmpty if you think that produces cleaner and more readable code.
4 comments:
As earlier stated, this small helper method rocks. But, I was just wondering; shouldn't it read:
return (ic == null || ic.Count == 0);
Count can't be negative, right? I don't know which of the tests that is performed first ("less than" or "equal to"), but if it starts with "less than", you would save a couple of CPU cycles =)
Cheers! :D
Nice catch, right you are of course, there's really no need to check for negative values in collections and I've updated the post.
To answer your question though. If you look at the IL generated from the >= statement you'll see that it (as one could expect) calls the clt instrucion (compare less than) and if that succeeds it calls the ceq (check equality) instruction.
I head a fun thought though.. You could just check for Count > int.MinValue =)
You could also turn this into an extension method:
public static bool IsNullOrEmpty(this ICollection ic)
{
return (ic == null || ic.Count == 0);
}
Then you can write:
var employees = new List<string>() {"Bob", "Alice", "Tom" };
if(employees.IsNullOrEmpty())
{
FireTheirAsses(employees);
}
Which then reads the same regardless of the collection implementing ICollection.
Yes I know it would make a candidate for and extension method and I thought about it when I wrote the post but I don't feel comfortable using extension methods that accept null parameters since I feel that it breaks common practice and relaxes the view on calling stuff on objects that might be null.
It's just not obvious enough that you're actually calling a static method and passing in null as the first parameter.
Post a Comment