Use extension methods to keep your code simple and DRY


In this article I’ll show you why and how to use generic extension methods to keep your code simple and DRY.

Table of contents

The Problem

Consider this simple model:

public class Format
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}   
    
public class Product 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public IEnumerable<Category> Categories { get; set; }
    public IEnumerable<Format> Formats { get; set; }

    public bool HasCategories
    {
        get
        {
            if(Categories == null)
                return false;

            return Categories.Any();
        }
    }

    public bool HasFormats
    {
        get
        {
            if(Formats == null)
                return false;

            return Formats.Any();
        }
    }
}

As you can see, I have created to properties HasCategories and HasFormats in the Product class to check if the collections Categories and Formats have any element, respectively.
The following ASP.NET MVC view shows how these properties can be used:

@model Product

<h1>@Model.Name</h1>

<p>@Html.DisplayFor(model => model.Description)</p>

<h2>Product Categories</h2>

<ul>
@if(Model.HasCategories)
    {
        foreach(var category in Model.Categories)
        {
        <li>@category.Name</li>
        }
    }
    else
    {
        <li>This product has no categories</li>
    }
</ul>

<h2>Formats Available</h2>
<ul>
@if(Model.HasFormats)
    {
        foreach(var format in Model.Formats)
        {
        <li>@format.Name</li>
        }
    }
    else
    {
        <li>No formats available</li>
    }
</ul>

There is a problem with this code though – the properties improve the code readability and can be reused, but you may have dozens or hundreds of classes that may need properties like these. It is a verious tedious and time consuming task to create such properties in all the classes we need – a better option would be to create an extension method to do that work for us.

Extension methods to the rescue

If you take a good look into the HasCategories and HasFormats properties you can see that the code is similar, the only thing that changes is the type of the properties.

We can then create a generic extension method to check if any object that implements IEnumerable (List, etc) is null or empty:

namespace Extensions
{
    public static class IEnumerableExtensions
    {
        /// <summary>
        /// Determines whether an enumerable is null or empty.
        /// </summary>
        /// <typeparam name="T">Type of the enumerable</typeparam>
        /// <param name="collection">The collection.</param>
        /// <returns>
        ///   <c>true</c> if the enumerable is null or empty; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsNullOrEmpty<T>(this IEnumerable<T> collection)
        {
            if(collection == null)
                return true;

            return !collection.Any();
        }
    }
}

There’s no more need to have the properties HasCategories and HasFormats so they can be removed from the Product class. The code now looks simpler and cleaner:

public class Product 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public IEnumerable<Category> Categories { get; set; }
    public IEnumerable<Format> Formats { get; set; }
}

Finally, we can change the ASP.NET MVC view to use the extension method:

@using Extensions
@model Product

<h1>@Model.Name</h1>
<p>@Html.DisplayFor(model => model.Description)</p>

<h2>Product Categories</h2>
<ul>
@if(Model.Categories.IsNullOrEmpty())
{
    foreach(var category in Model.Categories)
    {
    <li>@category.Name</li>
    }
}
else
{
    <li>This product has no categories</li>
}
</ul>

<h2>Formats Available</h2>
<ul>
@if(Model.Formats.IsNullOrEmpty())
{
    foreach(var format in Model.Formats)
    {
    <li>@format.Name</li>
    }
}
else
{
    <li>No formats available</li>
}
</ul>

Final thoughts

In short, you can (and should) use extension methods to keep your code DRY.

Basically you need to identity pieces of code that are generic enough and replace them with an extension method, if possible. You will have code that is easier to maintain and also more time to develop other funcionalities.

References

Advertisements

2 thoughts on “Use extension methods to keep your code simple and DRY

  1. Hi and thanks for this article, though your example might not be the best you can find. In this example, your root issue is not code duplication. Your root issue is allowing nulls. Allowing nulls forces you to resort to static methods, which are not OO and have all kinds of drawbacks and limitations (ask your favorite search engine). Your IsNullOfEmpty is a null-safe Any. Are you going to write a new extension method around every method of every object to null-check it ? Well that’s not very DRY. Keeping away from null as much as possible seems like a better idea, don’t you think ?

    • Hi Hugo,

      Thank you for your feedback.
      This extension method is meant to be used with collections only, not normal properties. No, I’m not going to write an extension method around every property to check if it’s null 🙂

      I do agree with you – you should develop the code in order to prevent null values, and that’s what I do. In the real world that’s not exactly what happens….
      Many times you are using 3rd part libraries or using the code that another team member wrote that may return nulls instead of empty collections, that’s why it makes sense to use an extension method like the one I wrote.

      I find myself writting code like this many times:

      if(product.Categories == null || !product.Categories.Any())
      {
      // ....
      }

      And I may need to do the same check for the same object/property in different places. That’s why I talk about code duplication.
      IMO that’s a very good reason for creating an extension so I can rewrite the previous piece of code like this:

      if(!product.CategoriesIsNullOrEmpty())
      {
      // ....
      }

      Best regards,
      Rui

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s