12/08/2018, 17:07

Everything you should know when design a domain-specific language (Pt. 2): General-purpose vs. Domain-specific

Previously on Everything you should know when design a domain-specific language : Part 1 : (Full article is provided on this url: https://viblo.asia/p/everything-you-should-know-when-design-a-domain-specific-language-pt-1-general-purpose-vs-domain-specific-jvElaOWdKkw) The relation between ...

Previously on Everything you should know when design a domain-specific language :

  • Part 1 : (Full article is provided on this url: https://viblo.asia/p/everything-you-should-know-when-design-a-domain-specific-language-pt-1-general-purpose-vs-domain-specific-jvElaOWdKkw)

The relation between General-purpose programming language & Domain-specific programming language

There are many attempts at making general-purpose languages more specialized, usually through the means of libraries and frameworks. Rather than letting programmers build complex algorithms to solve trivial math problems, GPLs often have standard math libraries that provide interfaces for computations like calculating the root of a quadratic formula. This has positioned third-generation languages, in which general-purpose programming language holds the majority, to be aligned in terms of functionality with fourth-generation languages. On the other hand, domain-specific languages often lack generalized features that are needed to make the language programmable. To resolve this issue, many domain-specific languages are designed not to be isolated and independent, but to be represented as services, embeddable parts, utilized and invoked by GPLs. LinQ – a Microsoft-developed query language – is designed to be seamlessly integrated into existing .NET codebase like C# or VB.NET. This allows LinQ to be a part of general-purpose source code, complementing the existing code while being its own language.

// LinQ code that integrates into C#
public void Linq()
{
    string[] words = { "cherry", "apple", "blueberry" };
    var sortedWords = from word in words orderby word.Length select word;
    Console.WriteLine("The sorted list of words (by length):");
    foreach (var w in sortedWords) {
        Console.WriteLine(w);
    }
}

Like LinQ, there are many other languages that are built upon GPLs. These languages often employ some parts of the existing languages, such as parser, compiler, IDE, or they integrate into the existing languages. This allows language implementers to have a quicker language development process, since a full set of components is not necessary. A prime example of this use case is Xtend. Xtend is a domain-specific programming language that is designed and implemented to be an extension of Java. The purpose of Xtend is to have full interoperability with Java, while being more concise, readable, and expressive. While having as many syntactic features as an advanced GPL like Java or C#, Xtend is still a DSL. This is because Xtend exists solely as an extension of Java. Its purpose is to help programmers write Java code faster, produce more readable code using syntactic sugars, which is a term to describe bonus features in the grammar that make writing code faster and easier. Many other extension languages also employ the same technique, to have syntax as close to the host language as possible, while adding new syntactic sugars to improve the host language. Extension languages are considered a quick way to greatly improve an existing language.

On the TIOBE Index, which ranks programming languages in terms of their popularity, the top 10 languages are general-purpose, with JavaScript, Assembly and PHP being pragmatic languages. The first domain-specific language to appear in the list is MATLAB, at position 17. This signifies the widespread adoption of many general-purpose languages, but doesn‟t truly indicate the popularity nor usefulness of DSLs. Because the index is based on search engines‟ rankings, and GPLs are applicable to more problem spaces, programmers across different domains search for GPLs a lot more than DSLs. In terms of practical applications, developing software in domain-specific programming languages has many advantages. One of the first reasons for the existence of domain-specific languages is to improve programmer‟s productivity by making programming easier on certain tasks. To further discuss this point there is one distinction we have to make. Because of how a DSL can be implemented, there are two types of DSL: internal DSL and external DSL. An internal DSL, sometimes called embedded DSL, is a domain-specific language that is written inside of a host language. Internal DSLs only serve as an extension of the host language, because they cannot go over the boundaries of the host language in terms of syntax. Because of this detail, an internal DSL often augments the capability of the host language, or introduces new capability, but under the control of the host language. This is especially true for the use case of LinQ. Since LinQ is embedded into C# with limited expressiveness, it has to rely on C# for many things, including the type system, variable creation, etc. Instead, it provides features that augment the existing language, making it easier for programmers to write code for data querying tasks. On the other hand, an external DSL is a domain-specific language that is developed isolated of any host language. An external DSL has its own syntax so it does not rely on the grammar foundation of any other language, but a full parser is often required as the grammar is written from scratch. Examples of external DSL are SQL, Xtend, MATLAB. To improve programmers‟ productivity, these languages introduce really specialized language constructs at very high level that stay close to application logic. Back to the previous example with MATLAB, the three lines of code concisely describe what is does, to “solve” an “equation” with symbolic variables x as the root, as opposed to the C# code, which goes more into details of how the underlying logic works. Similarly, to query data from a database, a C# programmer would need to specify a connection string, initialize a connection, specify a data reader and fill the queried data into an initiated data table. SQL can do the same task elegantly in one line of code.

-- SQL command to query from a table
SELECT * FROM DATABASE.dbo.TABLE;

Due to this expressiveness and other factors, SQL is almost the exclusive technology for querying data nowadays and all other programs have to go through a layer of SQL to access the data.

The second reason for why DSLs are still useful is due to the fact that DSLs are small languages. The syntax of a domain-specific language is often concise, contains only features that are meaningful to the application domain. They allow nonprogrammers to directly see and understand the code that runs their systems. This leads to domain experts acknowledge better how the technical sides of their business works, making it easier for developers and business analysts to converse with each other in a language both can comfortably use. The ability to communicate between different parts of a business is crucial for the success of a project. In fact, a study done in 2011-2012 reveals that minimally-effective communications resulted in 58% of project failure. This has scared away business owners of producing their own business management systems. And have to resort to buying other enterprise software. These enterprise applications, initially known as BPM suites, are enterprise application suites capable of modelling and visualizing the business process graphically. With these tools, business owners were able to design their business process effectively and share their visions with everyone else. But soon designing was not enough, new ever-changing demands call for flexible and adaptable business processes. Here the M in BPM does not stand for Modeling anymore, but the whole abbreviation stands for Business Process Management. Now, businesses are not only modeled, but can also be managed dynamically. These tool suites, being developed to sell customers of any business domains, are only really useful for large enterprises, involving in many domains. However, due to their very complex nature, smaller and more specialized systems only got a benefit from a small part of these universal tools. Moreover, these universal tools, with the ability to fully and confidently manage business systems at the largest scales, are basically very expensive. With the flaws mentioned, small businesses are better off not using such facilities than having to pay a large sum of money and effort in order to gain suboptimal results later. This is where domain-specific languages come into play. Using notations and design languages already familiar in those enterprise suites, businesses can have more specialized facilities, and making adjustments to the business processes will be more natural. Knowing this, many companies making enterprise suites are heading to a new direction. An example for this new approach is ABAP. As previously mentioned, ABAP is a language for programmable business applications. What sets it apart is its ability to enable large corporations to build frameworks for material, financial, and accounting management systems. ABAP is compiled and executed in an extremely specialized environment, built specifically for it. This indicates that no other languages can co-exist with ABAP in the same environment, and at the same time, ABAP cannot be used outside of its environment. In a sense, while ABAP has many features of generalization and is capable of creating massively large applications with complex logic for businesses, it is still a DSL and can only be used to create business applications. ABAP is also positioned alongside Java as the languages for programming the SAP Application Server – German multinational software corporation SAP SE‟s proprietary business suite. Using such languages as ABAP, they allow a more automatic management process, resulting in less manual management by human, which cuts down on labor cost. These automated processes also allow a much more rapid business exercise and can help boost businesses significantly.

Not only in private sectors, but domain-specific languages are being adopted in public domains as well. On the Web front, new languages are being published and getting widespread support, such as TypeScript, CoffeeScript, WebAssembly. These languages are aligned to be new alternatives to JavaScript for logic execution on the Web. On system development, there are languages like MicroPython, Swift, Kotlin for embedded computing and mobile computing applications. The need for more programmer‟s productivity is always a head-aching problem, as languages are merely the tool and programmers are the ones who make software. Having specialized languages that help programmers write better software quicker is becoming a trend in the software development field.

0