I had the opportunity to attend Martin Fowler’s Session in Bangalore last week where he talked about Domain Specific Languages.
Sendhil was telling me to read Martin Fowler’s Book on Domain Specific Languages for quite some time and for some reason I could not read. Fowler gave a good overview of DSLs in 20 minutes and I really liked the way he explained.
I did some reading about DSLs over the weekend and wanted to summarize my learning’s.
Language oriented programming (LOP) is a style of computer programming in which, rather than solving problems in general-purpose programming languages (like C# or Java), the programmer creates one or more domain-specific languages for the problem first, and solves the problem in those languages.
Wikipedia defines the concept of Language Oriented Programming as the approach to capture requirements in the user’s terms, and then to try to create an implementation language as isomorphic as possible to the user’s descriptions, so that the mapping between requirements and implementation is as direct as possible.
Let us use Regular Expressions as an example and explain what it means.
Assume, in your application you would like to validate e-mail address in multiple places. If we do not use Regular Expressions, then you have to write a code to validate the email address. It’s not something possible, but you have to write lot of code to accomplish this. There is no guarantee that this piece of code will be clean and easy to maintain.
Domain Specific Language is a programming language that is dedicated to a specific problem domain. You can also call it as mini-language. If you take the example of Regular Expressions, it’s a mini language specifically dedicated to Text processing (as a problem domain).
Let us say you are using MS CRM – Fetch XML to Retrieve the Marketing List a contact is subscribed to. The Fetch XML would look something like this.
<fetch distinct=”false” mapping=”logical”><entity name=”list”><all-attributes/><link-entity name=”listmember” to=”listid” from=”listid” link-type=”inner” alias=”listmember0″><link-entity name=”contact” to=”entityid” from=”contactid” link-type=”inner” alias=”contact0″><filter type=”and”><condition attribute=”emailaddress1″ operator=”eq” value=”firstname.lastname@example.org” /></filter></link-entity></link-entity></entity></fetch>
Same code can be written as the following using LINQTOCRM
var contactsMarketingLists =
from marketingList in xrm.MarketingLists
join mlm in xrm.MarketingListMemberRelationships
on marketingList.ListID equals mlm.MarketingList
join contact in xrm.Contacts
on mlm.Entity equals contact.ContactID
where contact.Email == “email@example.com”
Example borrowed from LINQ Queries using the XrmDataContext.
If you are developer, you will know which one is easy to maintain.
Are these new?
No, the term DSL is only used for last few years, but the concept is been around for quite some time.
SQL is one of the most used DSL in the world. Martin Fowler was mentioning about UNIX and the languages written in UNIX. People who were programming in LISP and Small Talk have been doing something like this for quite some time.
If you are a .NET Programmer, there is no way you would have not used WEB.CONFIG or APP.CONFIG files. Even though, the configuration is written in XML (General Purpose Language), the contents of these XML files need to conform to a specific Document Type Definition (DTD) or Schema Definition. The Schema or DTD defines the structure of the configuration XML file. Ideally if you look it is nothing but a DSL for configuring .NET applications.
Are you still not convinced?
Take the example of CSS. As a web developer, one would have used it at some point of time. Cascading Style Sheets as a DSL is focused on the Web Development as the domain. People who are specialized in this are called UI Designers. CSS as a DSL addresses the Problem domain of Page Styles, Layout etc…
Example of DSLs:
SQL, CSS, REGEX, HQL (Hibernate Query Language), LINQ, ANT/MS Build
Why do I really need a DSL? Why can’t I continue to do everything in my General Purpose Language?
In his book DSLs in BOO, Ayende has talked about the following as the reasons for developing your own DSLs
1. Making a technical issue or task simpler.
2. Expressing rules and actions in a way that’s close to the domain and understandable to businesspeople
3. Automating tasks and actions, usually as part of adding script-ability and extensibility features to your applications
In my opinion, the objective of DSLs is to create simple code which can be understood by your business analysts.
Say for example, you have written a code to parse Delimited files (Fixed Length), fetch and write it into a database. We have been writing code in our general purpose language for this for a very long time. We all know how the code looks like irrespective of how much care we take to develop it. If your business members have to review your code to figure out whether all the scenarios are covered, it will be very difficult to do it especially in the curly braces world (Java, C#) 🙂
Is there anything which is really stopping you from doing what we have been doing? No, the point is if simple and clear code is what you strive for, if communicating the Intent is more important for the business then DSLs are the way to go forward.
Can I accomplish everything in DSLs?
A domain specific language is a limited form of computer language designed for a specific class of problems.
No, by definition it’s a language with restrictions. You cannot do everything in a restricted language. Martin Fowler talked about creating Multiple DSLs for a project, combine them together & stitch them using a General purpose Language and deliver.
Ok, writing my own language sounds great conceptually. I do not think it’s everybody’s job. Don’t you require special skills to do it?
True, Martin fowler classified DSLs into 2 types.
1. External DSL
2. Internal DSL
External DSLs are the ones which operate outside your general purpose programming language’s scope. They are written from scratch. It starts with defining the syntax of your own language and defining the language capabilities. What it means is that you have great opportunity to define everything by yourself, but you also need to handle everything yourself.
Building this is kind of defining your own general purpose language which also means understanding of compilers etc. is important. If you are from the Application Development Environment I am not sure how much can be achieved in this.
SQL, REGEX, CSS, ANT are good examples for External DSLs.
Internal DSLs are developed using an existing general purpose language. LINQ and HQL are good examples of Internal DSLs. It’s developed on top of or using C# (General Purpose Language).
Martin fowler in his blog post defines Internal DSLs as particular ways of using a host language to give the host language the feel of a particular language. This approach has recently been popularized by the Ruby community although it’s had a long heritage in other languages – in particular Lisp. Although it’s usually easier in low-ceremony languages like that, you can do effective internal DSLs in more mainstream languages like Java and C#.
Dynamic Languages provides good support for writing DSLs (Haven’t seen much of this in the .NET world using DLR) and one doesn’t have to know the compiler design for writing these languages as the host language takes care of everything. Check this post in MSDN on Internal Domain Specific Languages by Jeremy Miller.
I also found one more category in Ayende’s Book. Graphical DSLs are DSL that uses shapes and lines to express intent rather than using text. If I think now, I will agree with his definition. Example for the Graphical DSL Tools are
2. SQL Server Integration Services
3. Windows Workflow Foundation
All these in theory sound good. Prakash, do you have any real time examples of it?
In our CP days we developed something called Query Object Model which made our job very easy to write queries.
An Example of QOM from our old demo
PeopleWhoDoNotWork => Where.Employee.Title.Contains(“Manager”).
In one of the product development engagement, the customer has built a DSL using XML and integrated it within Visual Studio.NET. All the UI Development happens through that DSL.
Sounds Great, Do you see any disadvantages?
Developers like developing DSLs. But when it comes to consuming, I have heard most of the developers complaining a lot about these. They are built on top of XMLs, I can write better language etc.
There is a significant learning curve in learning a new DSL. If your developers are not up to mark it will become a nightmare in terms of maintenance.
In Summary, I love the idea of having my own language for the development. In your organization, if you care for simple and maintainable code, they are the way to go forward.
If you are in Bangalore and if you feel that you have missed Martin Fowler’s Session, you can watch one of Fowler’s DSL talk in INFOQ site (Introduction to Domain Specific Languages)
Following are the material I read for this post.
First three chapters from this book DSLs in Boo: Domain Specific Languages in .NET
First Chapter from the book Groovy for Domain Specific Languages
Some useful posts
Domain Specific Language
Building Domain Specific Languages in C#
Overview of DSL Tools
7 recommendations for Domain Specific Language design based on Domain-Driven Design
The ‘Language’ in Domain-Specific Language Doesn’t Mean English (or French, or Japanese, or …
Writing Your First Domain Specific Language
Internal Domain Specific Languages