HumbleBlogger

Monday, May 29, 2006

Language Wars Redux - The Imminent Approach of LINQ

On the plane trip back from JavaOne I sat across the asile from a MS C# compiler developer. We had a lively conversation the entire trip. This ultimately prompted me to look into something called LINQ.

Language Wars Redux

Well, here we are in the Java community having gone through many months of absorbing the new language features in Java 5. We've been challenged and then excited by the phenomena of the dynamic scripting languages (and their frameworks) such as Ruby on Rails and even JavaScript in the context of AJAX. At JavaOne we were given a preview of Dolphin where modifications to the JVM will make it more facile to integrate dynamic scripting languages. A strong case was made for potentially integrating XML as a first class type in the Java language. We were wowed by the Groovy scripting language demonstration and excited by the prospect of the ranks of the Java platform numbers swelling as Project Semplice BASIC offers to give VB6 developers a home running code on the JVM.

These have been some exciting and heady times for the Java language and perhaps more especially the JVM platform. (At JavaOne it was clear that Sun has finally come around to fully embracing the notion of the JVM as a universal platform that should do a good job of accommodating other languages besides Java - the marketing position that Microsoft has held toward the .NET platform from day one.)

Alas, there may be some storm clouds appearing on the horizon.

If you haven't heard of it yet, the day will come when we've all heard of Microsoft's LINQ feature for the .NET platform. .NET Language Integrated Query (LINQ) is more fundamentally groundbreaking in language design than, say, a feature such as generics. And its impact on programmers and how they achieve their end goals will be more dramatic than such phenomena as Hibernate or Ruby on Rails and the revolution of thinking those entailed (ORM/EJB3 db-independent persistence and ActiveRecord dynamic types synthesized on the fly).

There has arisen a mode of thinking in Java land that by embracing dynamic scripting languages we can essentially address shortcomings in Java or bolster Java with exciting new capabilities. The scripting language Groovy is perhaps the ultimate expression to date of this line of thinking. It is an appealing notion - why add new features to Java, such as an intrinsic XML type, when Groovy already has a great markup language feature that makes working in XML very groovy indeed? Attention could instead be focused on making the integration of Groovy more seamless with Java and the JVM - and even making it the flagship scripting language such that it is there out of the box when the JDK/JRE are downloaded (with no additional installation steps required to make Groovy accessible). Given that Groovy scripts can be compiled into .class files it would seem to be the natural vehicle in which to address neat new language capabilities, as it is not limited to just pure scripting such as the likes of a BeanShell approach.

The new challenge: When Microsoft declares C# 3.0 to be production ready, it will have features that essentially leap frog the likes of: EJB3 ORM and its portable query language; many of the features entailed by the dynamic scripting languages - such as the Ruby on Rails ActiveRecord concept; and it will have a very facile ability to work intrinsically with XML. The LINQ feature will essentially unify the representation of query access of data across the domains of relational database, XML DOM, and in-memory collections/object graphs. It will be a language intrinsic capability for universal query. A day will come where it will even be used in every day programming situations - say, query all the fields of a visual form beginning with some name prefix string, and then perform a common operation on all those widgets that the query selected. One of Anders Hejlsberg's favorite demos is illustrating how LINQ can be used to query (and filter with clauses) out of type system reflection information. Thus LINQ is not just for relational database access, nor is it just a replacement for XPath, nor it it just an in-memory database query language. It embraces all of those as it is general purpose - and even facilitates selecting and migrating data between these various domains, i.e., select a data subset from a database while populating it into an XML DOM with trivial effort on the part of the programmer.

The common wisdom preached by the Ruby advocates and exemplified by its neatest accomplishments, such as ActiveRecord, is that such can only be accomplished by the dynamic, loosely typed scripting languages. However, the LINQ feature in C# 3.0 will turn this thinking completely on its head. It will bring in to question whether we really have to throw strong typing away in order to do these neat new things. LINQ creates tuples or anonymous types but it is able to retain type checking. The new feature of implicitly typed local variable declarations make it possible to hold a reference to these tuples in order to access and manipulate them. Unlike EJB3 query language, a LINQ query expression is type checked by the compiler. The end result is the simplicity style of the dynamic scripting languages but with the full rigor of a traditional strongly typed language. Alas, with C# 3.0 the use cases for these dynamic scripting languages just shrank a good deal. We're basically left with just the use cases of BeanShell scripting.≠ (Well, there are the first class regular expression features of Groovy as well.)

The LINQ feature draws on language enhancements that were added in C# 2.0 and of course various new additions in C# 3.0. To fully understand LINQ, start by reading the spec doc for C# 2.0 and then move on to the spec doc for C# 3.0.

In the on-going saga of the Language Wars, Microsoft's new LINQ feature looks poised to kick butt and take names. For in the meantime, over in the Java community, EJB3 persistence and its portable query language syntax will be regarded as the height of Java technology for query. There will still just be XPath for XML, and nothing at all for in-memory object graphs. Tuples? Forget it - will have to bail out to a dynamic scripting language for that kind of thing.

If you came back from JavaOne all pumped up about the state of Java and the JVM as a platform, you've now got yet another anxiety imminently approaching.

5 Comments:

  • It should be pointed out (given the comments on Javalobby) that anonymous types cannot be returned from a method except as object. This is true because the type name is compiler-generated, and therefore unknowable to the developer.

    So there's limited impact on having to debug someone else's code that uses anonymous types. Fears to the contrary are generally based on this misunderstanding.

    The solution would involve created some well-known type and project into that. For example, "... select new CustomerNameAndAge { Name = c.name, Age = c.age }".

    I'm glad you're as interested in LINQ as those of us who are working to make it happen. I've been programming for over twenty years and it's the most exciting thing I've seen since .. well .. my first BASIC program.

    By Anonymous kfarmer, at 3:59 PM  

  • For that matter, the comprehension syntax isn't even SQL. It's just a simple, declarative means of writing chained method calls. It's not a requirement to use the comprehensions, provided you write the lambdas.

    Perhaps it's a matter of seeing similar keywords and presuming too much about how it works.

    Incidentally, here's another database layer, that's also adding LINQ support: http://blog.genom-e.com/default.aspx

    By Anonymous kfarmer, at 4:04 PM  

  • Thanks for the clarifications, kfarmer (you must be a colleague to a certain C# compiler developer I know).

    I think there's been some hasty reaction to the LINQ feature on the part of many of the JavaLobby postings (along with a bit of usual knee-jerk anti Microsoft sentiment). It will probably take a while for sufficient familiarity to sink in before more measured appraisals appear.

    LINQ is one of the most innovative capabilities I've seen added to a mainstream programming language - well since I switched from C/C++ to the garbage collected languages. I've been radically more productive in Java and C# vs the prior generation. Is perhaps possible LINQ could have that manner of visceral impact on productivity.

    Besides the integrated query aspect, though, I'm interested in the tuple feature. As I noted on a JavaLobby posting to the LINQ thread, in the past I've designed apps that had what are effectively data types created at runtime based on templates and customized by user input. The anonymous type feature might bestow some type unity on such an application architecture such that the intrinsic, conventional types of the application and the end-user types are all based on the very same underlying type machinery.

    By Blogger rogerv, at 7:00 AM  

  • For folks that want to get in on the fray, here's a link to the JavaLobby thread for this same article where I cross-posted it:

    Language Wars Redux - the imminent approach of LINQ

    By Blogger rogerv, at 7:08 AM  

  • hi...i got to this blog after reading some of your very illuminating articles on JMS. I do have some doubts regarding the implementation of JMS for use in a distributed application that I am building - I would really appreciate any suggestions you might be able to give me, sine you appear to be an industry guru wrt JMS.
    my email: bikirme@gmail.com

    By Blogger Kiran Kalyan, at 4:34 PM  

Post a Comment

<< Home