Domain Specific Language (DSL)
A DSL or Domain Specific Language is written inside an existing host programming language. As the name suggests, it is targeted to a particular domain for providing solutions in an application and cannot be used for any requirements out of that domain. A domain could be web, database or built tools etc. A DSL is used in a general-purpose language (Host Language) such as Java.
In this article, we will be discussing DSLs in Java, how they work and why are they so significant.
DSL in Java
Java DSLs are optimized for a particular problem domain in Java. They are usually developed by or with the involvement of the experts in the domain for which the DSL is being created. They are relatively smaller, less complex and most importantly they are used within the codes written in Java.
Unlike general-purpose programming languages where you can try out working with various new or existing domains such as AI, web and mobile applications development, DSLs were never aimed to do that. Instead, DSLs are created for very limited applicability and use in a specific domain, but they are powerful and capable enough to properly address the problems and solutions in that domain. A good example of DSL could be HTML. It is commonly used for web development tasks but it certainly cannot be used for any tasks outside of the scope of the web.
Examples of Java DSL
Other than HTML, some other examples of DSLs in Java include:
- XML as a markup language. If you have ever built a mobile application in Java, you must have used XML for implementing UI-related data.
- UML is commonly used in modelling.
- SQL and its series of variants are widely used for Database management in Java.
- Drools is used to set up Business rules.
- Maven and Gradle do not need any introduction. They both are also used in Java as build tools.
The basic classification of DSLs divides them into the following two categories:
- Internal DSL
- External DSL
A DSL that is written in one language and it is then processed in a different host language is classified as an external DSL.
The example below shows a small external DSL written in SQL:
SELECT FullName FROM students WHERE score >= 80 AND class = 10 AND stream = ‘Science’
Now, it can be used in any general purpose language other than SQL like in Java.
If the DSL is written and possessed by the same language then that DSL is known as an Internal DSL. These are also called embedded DSLs.
See this example below demonstrating a small Java internal DSL, written and processed in Java:
orderAmount = orderAmount .SummersDiscountApplied() .LoyaltyCardDiscountApplied() .SeniorCitizenDiscountApplied();
DSL vs GPL
The creators of Java or any other GPL (general purpose language) do not know where their programming language will be used. Java has been used for a series of applications since its release. For example, the initial releases of Java were introduced way before the release of android. The creators never knew that Java would become one of the most used programming languages for android applications.
General-purpose programming languages are always created with generic constructs that potentially can be used for any problem, solution or business requirement. Java being a general-purpose programming language is used everywhere from desktops, mobile devices to embedded systems for banking, e-commerce and manufacturing etc. While targeting a generic construct, a GPL cannot provide a robust solution for every domain. This is why DSLs become so important.
A Java DSL aims to document the requirements and behavior of one particular domain in the application. It not only offers all the solutions for a particular domain but it also plays an important role in connecting business with the technicalities behind a domain. Java DSLs are also used by people from different backgrounds, for instance, XML is used by developers, designers and even sometimes business managers in the designing process of an application.
Advantages of Using a Java DSL
In often cases, Java DSLs are intended to be used by developers who must be fluent in the domain the DSL targets and may have very minimal knowledge of Java. This gives a big advantage to Subject Matter Experts (SME) who are experts in their particular domain such as Web development or database management as they can easily work on the tasks related to their domains regardless of whether they have a command or Java or not.
Using Java DSLs can also improve the code quality as it provides better architectural conformance, and easy-to-maintain code resulting in having fewer bugs in the code. This is achieved because developers can work only on certain components of the code over which they have significantly more command.
In contrast to Java libraries and frameworks, some Java DSLs also come with tooling support. This can be a great help for developers as the tools for code completion, visualizations, debugging and simulations are provided with some Java DSLs that can improve the user experience and make things much easier.
When to Create a Java DSL?
Other than the previously mentioned Java DSLs, you can also develop your own Java DSL to perform a customized series of tasks for a specific domain. The best approach for creating a Java DSL is by first prototyping the desired API and then working on implementing it in Java. Implementation of a Java DSL requires continuous testing to make certain that you are on the right track. There are more approaches but this prototyping and testing approach is the most recommended method.
Creating Java DSLs in Java certainly requires relative more work than other tasks. There is more code to be written, and a lot more documentation is needed to be created and maintained. This all can be worth it if the DSL can provide the solutions and improvements in your code. If done right, your code can become more compact and easier to maintain, which will eventually make your work easy in the long term. There are other methods to create Java DSLs, it totally depends on what you are trying to achieve.
Despite there are various tools easily available for developing Java DSLs, it is to be noted that should not define your own Java DSL right away as it is a very tedious and time-consuming process, instead you should first search for an existing DSL that could suit your requirements and solve your problems. There are numerous websites available offering existing DSLs or DSLs created by other developers for specific domains like DSLFIN lists a huge number of DSLs targeting the finance domain.
You should only go with the route of defining your own DSL when either you are unable to find a suitable DSL that can fulfil all your requirements or you have enough resources to develop a fully customized Java DSL for your application.
Tools to create a Java DSL
Following are some notable software and tools if you have to develop your own Java DSL:
– JetBrains MPS
Primarily, JetBrains MPS is an open-source integrated development environment (IDE) used for Java development but it also offers features to create Java DSLs. It features a projectional editor that comes with a document containing the DSL as its underlying abstract tree structure. JetBrains MPS also supports code generation to Java so you can also generate the code of your DSL although it is not mandatory, it offers various advantages if it is done.
Xtext is an open-source software framework for developing Java Internal DSLs. It is written in Java and is integrated with Eclipse IDE. Unlike most of the standard parser generators, Xtext not only generates a parser but also generates a class model for the abstract syntax tree.
– Word Processors
It might come to you as a surprise but a Java DSL can also be written using a word processor like MS Word. You can write a DSL in English using a defined vocabulary that can be then processed in any host language including Java using a parser generator such as ANTLR. Despite that, it is highly recommended especially to inexperienced developers to use a proper DSL editor instead of a word processor as it will make syntax and semantics checking much easier.
Wrapping it up
Java offers some great options for internal DSLs. Java DSLs are often much more featureful than the ones developed using other GPLs. Along with that, powerful Java IDEs such as Eclipse also provide full support for the creation, usage and maintenance of Java DSLs. Ultimately, there are various pros and cons of creating or using Java DSLs. It is up to you, to decide what is best for your projects.
Also Read: An Introduction To Inline Classes In Java