This post was spawned as I was reviewing the latest Oxite source code on CodePlex. I’ve been reading any source code I could get my hands on lately looking for inspiration to cure my “winter coding boredom.” While it isn’t winter yet, it is fast approaching, which means less outdoors time and more computer time. I try to blog more, update my open source projects with a new release, and with any luck begin a new project worth getting passionate about.

Today I'm going to hit the ground running with a short tip, again, on the yield keyword.

The following code is an excerpt from the Oxite Blog class. Blog as it turns out, is a Cacheable Entity (ICacheEntity) where the designer of the interface determined that cached entities can specify any dependencies they may have. Clearly, the developer of the ICacheEntity interface understood iterators and realized that the consumer of any ICacheEntity requires nothing more foreach’ing over the dependencies. For this reason, the method does not return an IList<T>, an ICollection<T>, or (heaven forbid) a List<T>.

Unfortunately, the developer writing the Blog class did not take advantage of the (albeit minor) utility provided. The developer chose to new up a List, insert the single item, then return the list -- as seen in the following code.

IEnumerable<ICacheEntity> ICacheEntity.GetCacheDependencyItems()
{
    List<ICacheEntity> dependencies = new List<ICacheEntity>();

    dependencies.Add(Site);

    return dependencies;
}

We've all written code like this. The bad habit may even be so ingrained into our developer DNA that we don't even realize it. Long story short, if it returns IEnumerable, you should probably use the yield keyword!

The above code can simply be re-written as follows. The benefits of this should be clear: fewer lines of code, easier readability, greater performance (minor of course, but still).  Since this topic has been covered at great length on the blogosphere I won't go into any more detail, but just in case, here is a quick article I found that should help explain the benefits.

IEnumerable<ICacheEntity> ICacheEntity.GetCacheDependencyItems()
{
     yield return Site;
}

 

Bonus Protip

I actually stumbled onto something small but nice while reading the Oxite code. Enumerable.Empty.

IEnumerable<Product> products = Enumerable.Empty<Product>;

 

Technorati Tags:

Comments

# re: Yet Another Yield Rant

Avatar
mob
9/22/2009 9:42 AM
Ooo, I never knew about Enumerable.Empty. Thx!

# re: Yet Another Yield Rant

Avatar
Dean
11/9/2009 11:18 PM
Nice - i just found your site and think ill use it and maybe drop by a user group meeting next month to keep up on .Net stuff..

Myself- my MS years be damned- Ive been Ruby (and Rails ,begrudgingly) for 2 years now. Haven't looked back but I like to see MS's take on enumerators etc.. yield is totally a part of my daily vocabulary now.. Cheers..

D

# re: Yet Another Yield Rant

Avatar
Ciddan
11/12/2009 4:03 AM
Enumerable.Empty<T> is a beautiful thing.
Comments have been closed on this topic.