RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Polyglot Programming: Building Solutions by Composing Languages : Page 3

This article delves into the motivation, benefits, and challenges of writing applications using polyglot programming—leveraging the multi-language nature of the CLR to create simpler solutions to vexing problems.

Polyglot Programming and Domain-Specific Languages
The domain-specific language (DSL) concept (especially text-based languages) is the subject of much current research. DSLs solve some of the same problems that polyglot programming solves—building abstractions more suited to the problem solution. However, DSLs are even more specialized, created on a case-by-case basis for specific problem domains. For example, for the sample problem described earlier, you might build a DSL that handles multi-threaded scheduling for your application.

Here is an example of a simple DSL written as an internal DSL atop C# (an internal DSL is one that uses the underlying language as its base). At ThoughtWorks, we had an application that required elaborate descriptions of train cars for testing purposes. Originally, we wrote code like this:

   ICar car = new Car();
   IMarketingDescription desc = 
       new MarketingDescription();
   desc.Type = "Box";
   desc.Subtype = "Insulated";
   desc.Length = 50.5;
   desc.Ladder = "Yes"
   desc.LiningType = Lining.Cork;
   desc.Description = desc;
But the business analysts didn't like this. When we showed it to them, they weren't interested in trying to read it because it looked too much like C# code, and our business analysts didn't want to see code. So we rewrote it to look like this:

   ICar car = Car.describedAs()
We didn't use any special magic frameworks or extensions to C#. Instead of creating standard properties, we merely created "Get" properties that caused mutating side effects and created methods (each of which returned this) to set values. Add a little creative indentation and you have something much more readable. This is a very simple example, but much more nuanced ones are possible. In fact, LINQ is really just an internal DSL to handle querying structured data.

The polyglot programming and DSL approaches are more complementary than antagonistic. Nothing stops you from writing the DSL as an internal DSL (in other words, written using the syntax of the underlying language) using a functional language like F#. In fact, Scala, another functional language that runs on both the Java virtual machine and .NET, includes features that make it easy to write DSLs within the language.

Technology sometimes facilitates unanticipated opportunities. When Microsoft created the concept of IL and a multi-language runtime, the goal was to unify the programming models across languages. In fact, initially, mixing languages in a solution was discouraged for many of the reasons listed above. Yet, as developers build more and more solutions using the existing abstractions, they'll see more opportunities to work less and achieve better results. The topology of possible problems is far too large to think that a single computer language could ever be best for every solution. By building an environment that opens the door for building simpler solutions faster, we can walk through to a new style of development. It won't be without its headaches—but just as we use special purpose languages now, we can aggressively expand that concept and create even better abstractions in the future.

Neal Ford is software architect and meme wrangler at ThoughtWorks, a global IT consultancy with an exclusive focus on end-to-end software development and delivery of large-scale enterprise applications. He has also designed and developed instructional materials, magazine articles, courseware, video/DVD presentations, and is author and/or editor of five books, spanning a variety of technologies. He is an internationally acclaimed speaker, having spoken at over 100 developer conferences worldwide, and delivered more than 600 talks. Check out Neal's web site for more information.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date