Impressions of LINQ, C# 3
Part I: Lambda expressions, initial impressions.For me, the most important new technology unveiled at PDC was LINQ, and the underlying language enhancements that make it possible.
It’s been about two weeks since PDC, so I’ve gotten caught up (sort of) with work and I’ve had a little time to let the new concepts sink in. However, I’ve had precious little time to actually write code based on C# 3.0, so this entry is based on my initial impressions, and not a lot of detailed research. (Yes, I’ll be doing much more detailed research, but I haven’t had the time yet. Like everyone else, I’m learning all these new tools.)
I’m going to discuss these features from the standpoint of the C# language, rather than the LINQ use cases. The LINQ use cases are the driving force behind these new features, but I believe the C# community will find many uses for object initializers, query expressions, extension methods, lambda expressions, anonymous types, and even local variable type inference. We’ll likely stumble on a number of anti-patterns for these tools in our quest for great new uses. It’s going to be a fun time.
Lambda Expressions
Everyone, repeat after me: “Functional programming is not pure evil.” Feel better? Good. Functional programming idioms can be very powerful. However, Functional Programming is rarely the simplest path to the simplest solution. (Anyone that used emacs and wrote extensions in e-LISP knows what I mean.)
That is, in my opinion, what will be incredibly cool about Lambda Expressions in C#: You can add functional programming idioms to an object oriented language. You’re not constrained to a single set of design patterns. Developers will need to grow some new skills in order to choose the right idiom for the right problems. I’m not yet sure how easy that will be. Clearly, the query expressions are built using Lambda expressions. It seems that Lambda expressions might be able to serve where C++ function objects were used. At least, that’s one of the things I’d like to try.
In addition, functional programming is a powerful tool for very dynamic systems. You are essentially treating code (or algorithms) as data. Would it be possible to mix remoting and lambda expressions to do powerful things? Or, is it too dangerous from a security standpoint?
The downside of lambda expressions, and functional programming in general, is the complexity the follows from the syntax, and the expressiveness. One line of code compiles into function calls that call other methods that change program state that introduce bugs that are hard to find. It’s harder to examine code and quickly see what’s happening. That’s going to be where the C# community develops best practices for how to leverage this feature to create new exciting code, without creating mountains of babel.
Well, back to work. I’d meant to write a blog entry on everything new in C# 3.0. That’s not going to happen. However, the great thing about blogs: I can just continue to add more content here. I’ll be back to discuss other C# features.