A DSL is a specialized language that addresses domain-specific issues. By getting closer to the problem, the DSL can provide benefits that may not be found in the common language. The Java world is full of DSLs. Property files, Spring contexts, a usage of annotations, and Ant tasks are examples of DSLs.
When I started to look at other languages like Ruby, I began to understand that the Java language is not a good grasp of DSL at the moment. In this article, you'll see four techniques for using Ruby to integrate clean DSL. You will then see what options are possible in the Java language.
A world of hidden languages
Although you may not know it, you actually have nowhere to meet the DSL, from everyday life to using the application to the program you wrote. In court, a stenographer can be seen to record quickly with a DSL. Music uses several different annotations to describe the volume, tone, and length of each sound, in a format suitable for a particular instrument. (I use the guitar six line, each of which represents a chord on the guitar.) Use DSLs because they are more effective at solving problems than dictating or taking dictation.
DSL is also used when working with everyday applications. The best example is a spreadsheet. Writing spreadsheets is easier than using the simplest accounting procedures. The DSL of spreadsheets fundamentally changes the nature of programming for a particular problem.
DSL in Java programming
Looking back, Java is also using DSL everywhere:
JSP makes it easier to build custom user interfaces.
SQL represents database operations.
The property file represents the configuration of the program.
XML describes the data.
XML describes the program configuration, such as in EJB, Hibernate, or Spring.
XML describes an action, such as an Ant task or a business rule in some kind of engine.
The Java language is not particularly good at domain-specific languages because it is difficult to extend the language in the most appealing way for DSL developers. That's one reason why XML is so rampant. XML is extensible, Java and its integration is good, you can easily build tools to interpret it, and it does not need to be compiled with Java classes. But XML is unfriendly to human reading. So you can see a wide range of complaints about the overuse of XML in the Java language.
DSL using Ruby and active records
In the first article across the boundaries series, you saw the activity record (the persistence engine behind Ruby on Rails). In this article, I went back to the activity record, because it had a wonderful application of the DSL concept in several places:
A domain-specific statement structure and glossary. The activity record builds a glossary for wrapping relational databases with Ruby objects. For example, in a database-supported object, you can use Has_many:p eople to build a one-to-many relationship mapping with objects supported by another database.
The behavior of the extended class. According to the naming convention, the active record class, called people, has properties that correspond to each column in the database.
Modifies an existing type. Rails typically modifies classes such as Fixnum to provide a domain-friendly experience.
Expand the vocabulary dynamically. Activity records provide some surprises, such as adding a custom finder based on the structure of the database.
English modeling. Activity records Modify the plural form of a class based on the context.
As you continue to read this article, you will see the Ruby features that make these techniques possible. You will really appreciate the difference between the Ruby and Java ways of doing things. To write code along with this article, you need to install Ruby and Ruby on Rails, which includes activity records (see Resources).