Ludovic's weblog

Parameters' validation

Using C# linq to achieve better readability

In a large scale application, you often use some kind of defensive programming techniques such as input validation to ensure that your method can run correctly and produce the desired output.

This simple random bit of code provides you a clean and elegant way of validating that your collections are neither null nor empty.

Bonus: It also works with strings.

Before

MyClassConstructor(string name, IEnumerable<int> values)
{
  if (string.IsNullOrEmpty(name))
  {
    throw new ArgumentNullException("name");
  }

  if (values == null || values.Any() == false)
  {
     throw new ArgumentNullException("values");
  }
}

The bit

public static class Extensions
{
   public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection)
   {
      return collection == null || collection.Any() == false;
   }
}

After

MyClassConstructor(string name, IEnumerable<int> values)
{
  if (name.IsNullOrEmpty())
  {
    throw new ArgumentNullException("name");
  }

  if (values.IsNullOrEmpty())
  {
     throw new ArgumentNullException("values");
  }
}