Thursday, March 06, 2008

LINQ to Life - The Game of Life via LINQ

CodeProject - LINQ to Life

"Believe it or not, LINQ is not just about data access. LINQ makes a profound impact on every day code. Remember, the days trying to find an element in a list by looping through elements, setting some flag (e.g., found = true;), and calling break to get you out of the loop? Those days are over thanks to LINQ and the powerful features that come along with it including lambda expressions, type inference, and extension methods. This article will use a classic program, Conway’s Game of Life, as a chassis for a LINQ engine. The purpose is to show how writing query expressions adds flexibility, readability, and power to code that is typically full of loops and branches that add to the cyclomatic complexity.

Click here to watch the screen cast

...

Conclusion

I may never write a for loop again. At least not while trying to find a particular element within a collection. LINQ expressions, extension methods, lambda expressions, and type inference are just a few of the powerful new features of 3.5 that make C#/VB.NET so much more expressive: not just for your typical database backed LOB application, but for everyday coding.

In this article, I only touched on some of the critical places where I used LINQ. Please download the source for this project and look at even more LINQ-y goodness. There is even an example of using the Enumerable.Range method that provides Ruby style sequence generation. Warning: I didn’t spend much time on the GUI itself. The code would also make a good basis for a round or two of code golf, as the point of this article is to show how readable and clear LINQ can make your code."

This article's title is what grabbed my eye, but the code is what kept me reading it...

1 comment:

Anonymous said...

Thanks for quoting me. I really appreciate it. Very satisfying that you and others found the code readable and understood my point. Thanks again!