Thursday, October 30, 2008

Scarlet – A Scala DSL for web applications

Work in Progress
The Scarlet project is currently under development. Please keep an eye on the sourceforge project site ( or contact personally for updates.

Introduction to Domain Specific Language (DSL)

Domain-specific languages have lately become a hot topic and a lot is being spoken and written about them all over the internet; much of the buzz around functional languages is their applicability to (once again) put the power and control of an application in exactly the right place where it belongs, i.e. in the hands of its users.

By defining a simple language that the application users can understand, feel comfortable to communicate and use, programmers can effectively reduce their involvement in the never-ending cycle of UI requests and enhancements and can let users create scripts and other tools that allow them to create newer behavior in the applications they use. A perfect example of a wildly successful DSL is the Microsoft® Office Excel "language" used to express the various calculations and contents of the cells in the spreadsheets.

Well, some might even go far and suggest that SQL itself is a DSL, this time a language aimed specifically at interacting with the relational database (Imagine if programmers had to fetch data out of Oracle via traditional API read()/write() calls — Eshhhh! Now that’s a nightmare).

What is Scarlet…?

To be quick to the point: Scarlets is an attempt to create “platform-independent”, “compilable” and “executable” pseudo-codes.

Pseudo-codes are a kind of structured english for describing algorithms; which enables the designer to focus on the logic of the algorithm without being distracted by details of language syntax. The intent is to describe the entire logic of the algorithm with finer details so that implementation becomes a rote mechanical task of translating line by line into source code.

We have known pseudo-codes probably since the beginning of times at software engineering. And although we understand that pseudo-codes are capable of describing the entire logic of an algorithm to such an extent that implementation remains to be a simple mechanical task of translating lines of plain english into source code; yet, this so-called simple mechanical task is till date performed with human efforts.

The Scarlets project intends minimize this human intervention by defining a Scala based domain-specific language which is very much like structured english and is a narrative for someone who knows the requirements (problem domain); so that he/she can write a pseudo code and see it run with zero or minimal involvement of a programmer who knows the programming language (implementation domain).

A Sample Scarlet

The following is a sample implementation of Scarlet –

AccountApp.scala :

val userName = theParameter named "user" in request
if (userName equals "nothing") {
~~~this renders "AccountUserForm.jsp"
} else {
~~~theAttribute named "LoggedInUser" in session is userName

~~~val accountHolder = new AccountHolder("B123456", "Rahul", "Roy")
~~~val account = new Account("SA-001", accountHolder, 100.00)

~~~theAttribute named "AccountObject" in session is account

~~~this renders "AccountDisplay.jsp"

Do we need a description for the above implementation…? Well, I’d rather leave it for you to understand. Meanwhile here is how the AccountUserForm and AccountDisplay Java server-pages (JSP) look like.

AccountUserForm.jsp :

~~~~~~<title>Scarlet Sample</title>
~~~~~~<form action="AccountApp" method="GET">
~~~~~~~~~Username : <input type="text" name="user" />
~~~~~~~~~<input type=submit value="Login" />

AccountDisplay.jsp :

~~~~~~account = session.getAttribute("AccountObject");
~~~~~~LoggedInUser = session.getAttribute("LoggedInUser").toString();
~~~~~~<title>Scarlet Sample</title>
~~~~~~<%=LoggedInUser%>'s Account : <%=account%>

I hope you found Scarlets to be interesting; anyways please do feel free to share your thoughts and comments here so that I can better it with your cooperation.

No comments: