List Extension to detect nulls and more than zero

So as we all know, us developers are lazy to fault. Now, when I say this, I must admit, usually this is not a bad thing when it comes to software development. If not for being lazy, most design patterns would cease to exist and acronyms like DRY(Don’t Repeat Yourself) and KISS (Keep it Simple Stupid) wouldn’t exist. Now I know there are plenty design patterns that on their very existence don’t use the DRY method, but in the end, they usually makes some other process simpler or you end up creating less code ( For example, Unit Testing causes more code, less bugs). These in essence exist because as developers, we don’t like to see when things are duplicated over and over again just to alter one parameter or a small logical change. Something inside us turns on a light bulb and then makes us want to scream and run… well maybe not quite that bad…

I can sense everyone reading this nodding their head Yes I know the feeling and it drives me batshit crazy.

There are many different ways in OOP that we implement the DRY concept in various design patterns and tools within frameworks. As you know, my primary language of choice is .NET C#. Well, since about C# version 3.0 I believe, there is this awesome feature called extension methods. This what we are going to talk about today. I did a similar post on Extension Methods where I go into more depth on the topic here. Today we will just discuss extending the useful the String convenience methods.

 

Making the powerful more powerful

I think we can all agree that the code below:

if(someStringList != null && someStringList.Count != 0)
{
    // Do Stuff
} 

Is not as clean to read (and Any() is faster as well)

if(someStringList != null && someStringList.Any())
{
    // Do Stuff
} 

What if we could clean this up further? Well, we can

  public static class ListExtensions
  {
      public static bool CheckForValue<T>(this List<T> list) where T : class
        {
            if (list != null && list.Any())
                return true;

            return false;

        }

    }

In the above code, I created a public static class called ListExtensions. In the past, I did a PagedList to SelectList as well as an introduction to Generics ( T ). Check them out in my archives! Both are good reads! Anyway, this static class implements a static method called ChecForValues. This method takes in the list in question and does the second piece of code above for you. Is it a performance enhancer? No, Is it a time saver? I think so.  Think of how many times you type that to guard against exceptions.It also is easier to look at and immediately know what it is trying to accomplish. I hate methods called DoThis() or variables x and y. We will leave that topic for another day Smile

To use this code it is as simple as the following:

var myPeps = new someListObject<Person>();

if(CheckForValues(myPeps)
{
    // Do your logic here
}

It’s that simple. One rant I will get up on my soapbox about in general is the use of .Count() versus .Any(). If you have 500 records, this might not be a big issue, once you get up into the larger thousands to hundreds of thousands of records, this can be a much bigger issue because the Count method will count all records and then decide if the count is not equal to 0. With the Any method, as soon as it finds a value, it returns true and the code moves on. Just have to say that every time I talk about the Any method.

 

Off my soapbox.. and Happy CodingSmile

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, ASP.NET, C#, Classes, Computer Technology, Data Mapping, DataAnnotation, Generics, MVC, Performance, Programming, Programming Paradims and tagged , , , , , , , , . Bookmark the permalink.

One Response to List Extension to detect nulls and more than zero

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s