RSS
Facebook
Twitter

Tuesday, June 07, 2011

Using null-coalescing to find item in multiple collections

A co-worker showed me this:

var result = collection1.GetObjectByAtSpecialLocation(location) ??
        collection1.GetObjectByAtLocation(parent, location) ??
        collection2.GetObjectByAtSpecialLocation(location) ??
        collection2.GetObjectByAtLocation(parent, location);

I’m still trying to decide whether it’s a good practice and if it’s readable or not…

In case you’re not familiar with ?? operator a.k.a null-coalescing operator it’s basically returns a default value if the left side is null so the following line means return empty string if name is null:

string result = name ?? string.Empty;

And so the code at the beginning of the post can also be written as:

var result = collection1.GetObjectByAtSpecialLocation(location);
if(result == null) 
{
    result = collection1.GetObjectByAtLocation(parent, location);
} 

if(result == null) 
{
    result = collection2.GetObjectByAtSpecialLocation(location);
} 

if(result == null) 
{
    result = collection2.GetObjectByAtLocation(parent, location);
}

Although most developers would agree that it’s easy enough to understand it’s far from being a good piece of code.

I also think that the reason that there was a need to check if an item exist over two different collections in two different ways indicates a design flow – but since we’re not living in a perfect world right now it’s impossible to merge those two collections and regardless of effort there was a good reason that they are different in the first place so I’m have to find an item in four different ways.

At first when I saw the code I cried abuse – it seemed to me that there bound to be a better way to do so without nesting four calls but after a while this solution seemed almost elegant.


What do you think?

2 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...