LINQ: HTF to WTF

In a very recent codereview with our team we were talking about LINQ versus the programming style that we are used to, lets call that ‘procedural programming’. As for our codereviews the most heated debates can be about readability of the code. Not about good or bad algorithms which are actually very important for performance, but programmers can be much more heated about readability. I consider this a very good thing because if code is readable, it means it is maintainable.

Back to our discussion I read somewhere that if code is written as a LINQ statement it is more about WHAT the code is doing than about HOW the code is doing what it is supposed to do. So one of the teammembers made a very nice comment during our codereview session that I had to write down for our codereview report. here’s that snippet from our codereview report:

• HTF to WTF: Procedural programming is about how the code gets the things done while LINQ is about what you want to get as results. LINQ can therefore be read easier than non-LINQ code.

Let’s have a look at a simple code example. A nice for-loop that clearly tells you HOW the computer is supposed to handle the algorithm:


///

/// Determines if for this order the USA text must be shown.
///

///
public virtual bool ShowUSAText()
{
foreach(OrderLine orderLine in OrderLines)
{
if (orderLine.ProductBooking.ShowUSAText())
{
return true;
}
}
return false;
}

That’s pretty clear isn’t it? The code explains how the machine is supposed to loop through all the orderlines, check each product for if some text is to be shown, and if for ANY product, the text is to be shown than it is going to return with the TRUE value. And if none of the products want to show the text well, the method simply returns FALSE.
Notice that I’m using the word ANY here, while that is currently not part of the code at all. But ANY is a very important word because it is telling is WHAT the code is actually about. Now LINQ (Language INtegrated Query) comes to the rescue:

///

/// Determines if for this order the USA text must be shown.
///

///
public virtual bool ShowUSAText()
{
return OrderLines.Any(orderLine => orderLine.ProductBooking.ShowUSAText());
}

That’s a nice LINQ expression! If ANY (yes, look, the word is right there on the screen) product wants us to show the USAText, that is what we are going to return.
And it also means that if none of the products wants to show the text, it will return false. That’s pure logic.

Now if that LINQ code seems a bit difficult to write, use the next version of Resharper. Because with the latest version of Resharper 5.0, you do not even have to know how to write LINQ from the top of your head, because Resharper 5.0 will happily suggest to change these kind of for-loops into LINQ for you:

Resharper 5.0 and LINQ

That’s really cool! Unfortunately, Resharper 5.0 is still beta and at work we still only have licenses for version 4.x.. have to ask to upgrade our license soon though, because as said at the top of the post: readable code is maintainable code.

Till next time,

Please follow and like us: