How Extension Methods can increase readability

In day by day work, a developer has to branch their logic checking for null, and it is obvious that checks for null are the most common checks in most projects.

The checks can be avoided by using Null Object Pattern or Specification Pattern both well documented by Martin Fowler. But the Null Object pattern can’t be reused everywhere, so still in some cases the null checks will exists. Specification pattern I consider is quite heavy just for null checks.

Let’s suppose that in UI layer we should display a list of users which have LastLoginDate null and has a Role assigned (Role property is not null):

void DisplayUserList()

        {

            IList<User> userList = UserService.GetAll();

            if (userList == null || userList.Count == 0)

            {

                return;

            }

 

            foreach (User user in userList)

            {

                if (user.LastLoginDate == null && user.Role != null)

                {

                    DisplayUser(user);

                }

            }          

        }

So as we can see there are boring and not readable checks, we can perform a small but useful refactoring and make a little bit more readable (and easier to type J ) by introducing few extension methods:

 

 static bool IsNull(this object obj)

        {

            return obj == null;

        }

 

        static bool IsNullOrEmpty<T>(this ICollection<T> list)

        {

            return list.IsNull() || list.Count == 0;

        }

       

        static bool IsNotNull(this object obj)

        {

            return !obj.IsNull();

        }

 Now the code looks as follows:

 

       void ExtentionDisplayUserList()

        {

            IList<User> userList = UserService.GetAll();

            if (userList.IsNullOrEmpty())

            {

                return;

            }

 

            foreach (User user in userList)

            {

                if (user.LastLoginDate.IsNull() && user.Role.IsNotNull())

                {

                    DisplayUser(user);

                }

            }          

        }

The conclusion is that extension methods not only can extend objects but also our imagination J “how we can improve our code”.

Note: The code above is not intended to show how is better to design but only to show how extension method can increase readability.

Thanks for your reading J

 

12 Comments

  • Meh, it's not that much easier. In fact, it's pretty obfuscating because you don't actually know what is being checked for null without looking into the method implementation itself. This is a dangerous path you go down.

  • Hi Bryan,
    It’s really good point Thank you for your comments,
    To answer I can say that we already using this kind of stuff in a real project and developers really know how to use and what it means, so I don’t think here are some problems with obfuscation because it’s lake any other method with meaningful name from infrastructure, the approach is to introduce a more readable fluency in code and reduce code duplication in case such as userList.IsNullOrEmpty() because we really often need to retype list == null || list.Count == 0 across the project to branch our logic.

  • I dont think that making an extension method for the object type is a best practice; on the contrary I think it is bad to define extension methods for object type.

  • I would rather see something like this:

    if (user.HasLoggedIn...)

    Instead of:

    if (user.LastLoginDate.IsNull()

    It’s more readable and makes the intent even more clear.

  • Hi Tore,
    Thank you for your first positive feedback :), and interesting point of view which is a little lesson for me. :)

  • > if (userList == null && userList.Count > 0)

    I don't think this is going to work...

  • Hi Fabrice,
    the line not but the line:
    >if (userList.IsNullOrEmpty()) - YES :)
    If to be serious, thank you, that’s my fault, I've updated the line.
    One my school teacher would say in this kind of situation: "I'm just checking if all are attentive" :)

  • Everything is very open with a very clear clarification of
    the challenges. It was really informative. Your website is extremely helpful.

    Thanks for sharing!

  • Write more, thats all I have to say. Literally, it seems as though
    you relied on the video to make your point. You definitely know what youre
    talking about, why waste your intelligence on just posting videos
    to your site when you could be giving us something informative to read?

  • It's hard to come by experienced people in this particular subject, however, you sound like you know what you're talking about!

    Thanks

  • For newest news you have to go to see web and on
    the web I found this site as a most excellent web page for hottest updates.

  • Hi there, of course this post is actually good and I have learned
    lot of things from it regarding blogging. thanks.

Comments have been disabled for this content.