Saturday, 20 December 2008

Thursday, 11 December 2008

During a code review

My code may not be the best in the world, but yesterday I came across the following snippet:

            DrawingImage image = null;
IDictionaryEnumerator enumerator = dictionary.GetEnumerator();
if (enumerator != null) {
enumerator.Reset();

while (enumerator.MoveNext()) {
DictionaryEntry key = (DictionaryEntry)enumerator.Current;
image = key.Value as DrawingImage;
if (image != null) break;
}
}

return image;


Which after thinking about reduced to this:



            return dictionary.Values.OfType<DrawingImage>().FirstOrDefault();



Interesting? No not very except for the following thought, as we move to a world where we write less and less imperative code and more and more declarative code, some of us (esp. me) really have to give up control to other more talented programmers out there a just use their code 'declaratively'.