If you are dabbling with Domain Specific Languages, especially on the JVM, then read on. I recently had the opportunity to pose a few questions to Debasish Ghosh. Debasish Ghosh is the chief technology evangelist at Anshinsoft, where he specializes in leading delivery of enterprise-scale solutions for clients ranging from small to Fortune 500 companies. His research interests are functional programming, domain-specific languages, and NoSQL databases. is a senior member of the ACM and author of the book DSLs In Action, to be published this year by Manning. You can read his really insightful programming blog at http://debasishg.blogspot.com.
Manning also has kindly made a free chapter available from the book too. Thanks Manning!
Get 40% off the DSLs in Action ebook, pbook, or MEAP. Use the special DZone coupon code dzone40.
Aslam Khan: Domain Specific Languages is a quite a trending topic in recent months (maybe years). What was your main objective for writing the book?
Debasish Ghosh: The multitude of languages that we see today on the JVM has created a niche ecosystem for expressive programming. By expressive programming, I mean architecting the solution domain of your problem in such a way that even business users can understand and verify many of the domain logic that programmers write. This paradigm of development can have a huge impact on the way we write and deliver software today. I have been working on this model of development for some time now. I wanted to share this thought and some of my experience in building real world DSL based solutions with all developers out there.
Aslam: In your book, you work with several general purpose languages for creating DSLs. What should we look for in a language that makes that language a good candidate for creating internal DSLs?
Debasish: Internal DSL is almost not differentiable from expressive APIs. In my book I have emphasized on this idea that a well designed API that models the ubiquitous language of the domain is an internal DSL. You can have internal DSLs designed in any language – the amount of expressivity will vary with the features that it offers. Languages that offer functional programming idioms are more natural for designing expressive internal DSLs because functions compose more naturally than objects. Additionally some languages like Scala offer higher order abstractions like parser combinators as a library that make a great tool for developing external DSLs. DSLs in Action discusses all these in much gory details along with real world examples from a particular domain.
Aslam: So we build a DSL, and write lots of small pieces of code with the DSL. Then we discover that we need to enhance the DSL. Can you share any tips, techniques or thoughts on how to deal with changes in DSL so we don't break (a lot) of the old DSL code base.
Debasish: DSL is like any piece of software. It will evolve over time and you need to architect your system so that it can adapt to changes in the DSL. Just like you model associations between objects non-intrusively that continue to work even when you change one of them, you do the same with DSLs. Whenever you compose multiple DSLs or compose a piece of DSL with your core application, ensure that you follow the principles that lead to well designed abstractions. The general principles are the same –
• Never expose implementations between composed DSLs
• Allow the composition to be based on published protocols and interfaces,
• Use combinators and higher order abstractions instead of lower level structures like recursion etc.
Aslam: How important is syntax sugaring in a DSL and how more complexity (if at all) is introduced as a result of building in sugaring?
Debasish: My philosophy is NOT to go overboard with syntax sugaring. It complicates implementation unnecessarily, makes error handling difficult and may also confuse users. Introduce only sufficient syntax to your users which make the domain vocabulary explicit. One common meme that goes around with DSLs and syntax sugaring is that with enough syntax sugars, non-programming business users can do programming. I don't subscribe to this school of thought. A DSL is designed so that it's sufficiently expressive to the business users. The primary purpose is to make the business rules explicit, so that a business user can read and them, thereby improving the communication between the development team and the user base. This has been an underlying thought throughout the book.
Aslam: Tooling - should DSL creators be building tooling to help others that will be programming with their DSL? Does it make a difference if it is an internal or external DSL?
Debasish: Eclispe Xtext is an excellent tooling platform for developing external DSLs. I have a detailed example in the book on how you can control the complete lifecycle of your DSL using Xtext. But tooling is an area in DSL development that's going to evolve a lot in the years to come. For internal DSLs, we are going to see languages that offer higher order abstractions that help you express your intent more succinctly. For external DSLs, we have already started looking at various workbench tools that promises to take development of external DSLs at the very next level.
Aslam: What are some of the holes that developers can fall into when building their very first DSL?
Debasish: I always suggest an iterative approach to DSL design. It's no different from API design. As with any API design, work collaboratively with your users to iron out the syntax of your DSL. I have also seen instances where developers have fallen into the trap of trying to provide too much sugar to the syntax of a DSL. This can only result in brittle implementations and non intuitive error reporting.
A DSL has two parts – a thin veneer of linguistic abstraction (the surface syntax) for the users of the DSL, which sits on top of a semantic model of your domain. The semantic model is for the DSL implementers and needs to be treated with enough care to ensure that it is well designed as well. The syntax cannot stand on its own – make the semantic model of the DSL expressive as well so that all business rule implementations are equally well published to developers.
Aslam: In your opinion, are we seeing DSL and polyglot approach to development becoming mainstream? If not, what will it take for it to become mainstream?
Debasish: Every new paradigm starts as a nascent movement. Even today we see lots of projects using Groovy as a DSL with mainstream Java applications. I have used Scala based DSLs to pimp my Java classes to give the users domain friendly APIs. All of us use BDD frameworks like specs for Scala, RSpec and Cucumber for Ruby, easyb for Groovy. And most of these testing frameworks can be deployed irrespective of the JVM language that you use for developing the core application. So, it's not the question of becoming mainstream – we are already using lots of DSLs in our everyday programming. Remember the big bang infamous XML for defining Spring beans? Today we are using the nice syntax of Groovy for the bean builder definitions in Grails. Clojure is a language that itself is a DSL. You don’t have to do anything extra to make a DSL out of Clojure. Just write idiomatic Clojure and you have a DSL.
Aslam: Debasish, thank you for taking the time to share your thoughts with with us, and thanks for the huge effort in putting down all your knowledge and experience into your book.