Archive for the Development Category

Every now and then, I start thinking about domain-specific languages and about how it would be really nice if we could have a relevant domain-specific language for each piece of code that we needed to write. Recently, I realised that if we ensure that we “code at the right level” then we can go a long way to achieving it.

An example of coding at the right level: If you were performing a database query, you would either specify the database query as SQL (or another query language possibly) and issue the query, or you would ask your object-relational mapping layer to do it for you. Either way, you are only dealing with querying the database rather than worrying about opening a network socket to the database server, sending the query across the wire in the correct format, marshalling the results, etc, because these operations are at a much lower level and you’ll have a library or database driver to do them for you. Likewise, you wouldn’t decide in the same method which web page to direct the user to next, because that is a much higher level. This is an example of an operation that would happen in a method of a typical DAO object and any non-DAO operations are clearly out of scope.

The same is relevant in other situations. Let’s say we have a web application and have split it up into these layers: web layer (e.g. a Spring MVC Controller or a Struts Action), business delegate layer and DAO layer. In the web layer, all we are concerned with is getting the relevant data from the request, passing it to the relevant business delegate and then forwarding to the correct page depending on the result of the delegate call. At no point should we be worried about performing any business operations ourselves, and certainly at no point should we be concerned about interacting directly with the database. As such, if we had a simple domain-specific web controller language, we could code displaying a product’s information as something like this:

get productId from request
if productId missing then display error 500 page
get productData for productId
if successful then display product page with productData
if permission denied then display error 403 page
if not found then display error 404 page

Which is a fairly typical web request pattern. To convert this into Java, we would get something like this (I had to fight with Wordpress a bit to get it to format):

String productId = request.getParameter("productId");
if (productId == null)
{
..redirect(ERROR_500);
}
else
{
..try
..{
….Product product = productLoadDelegate.loadForProductId(productId);
….request.setAttribute(”product”, product);
….redirect(PRODUCT_PAGE);
..}
..catch (NotFoundException e) { redirect(ERROR_404); }
..catch (PermissionDeniedException e) { redirect(ERROR_403); }
}

We’ve got a bit more syntax here than in our domain-specific language above, but all the concepts are the same. Nowhere are we dealing with database connections. Nowhere are we worrying about network sockets. Nowhere are we worrying about where our product data is stored.

If we can follow the principle of treating each class or method as a domain-specific area and program in Java or our language of choice as if it was a domain-specific language, then our code is much neater and more maintainable. It’s a game to play to enforce separation of concerns.

I’ll probably come back to this at some point when I’ve thought about it a bit more.

I commented on a blog entry here where people were Java-bashing and saying that it was inefficient and restrictive. I’ve expanded that comment into an entry here and put a bit more thought into it.  It boils down to a trade-off between speed of implementation and cost of maintenance, which is a topic that is well understood in some contexts and I feel that it applies here too but is lost in the noise of language wars.

Some people rant about Java being bloated architecturally. If you architect correctly then it is not bloated. To some people, “Java” isn’t Java. To some people, “Java” is layers of pointless, bloated EJB and middleware and endless acronyms and buzzwords. To me, Java is a language and you can use it to program at whatever level you choose, given the right frameworks and support. 

However, there are still restrictions that some people do not like. Some people like manipulating hash tables and lists with operators rather than with method calls and don’t like that Java won’t let you do this. My view is that the more obvious that syntax is (i.e. in correctly named method names) the more self-documenting the code is.  Of course, certain symbols such as “[]” for indexing are (almost) universally understood.  But if these can be overloaded in strange ways as they can in some languages, then this universal understanding is watered down.  And how much time is spent actually typing in code when considering the whole process of delivering a project?  Not much.  The few characters that would be saved by typing “var[i]” rather than “var.charAt(i)” are negligible when compared to the thought processes that went into deciding how your method / class / package / application is actually going to function and hold together.

I like Java. It makes me feel comfortable. It makes me feel safe in a particular way: That if I write my Java code clearly and with a minimum of complexity then there is (hopefully) only one way in which a random, possibly underqualified support programmer can interpret it when (s)he attempts to change it in three years time.  This is, unfortunately, a large proportion of many applications’ life cycles.  Jumping on the bandwagon of shiny new technologies when they appear on the horizon can end up costing your organisation or customers a lot of money when they need to find people to maintain them.  To ignore this reality is tantamount to intentionally producing an unmaintainable product.  Maybe in a few years there will be a critical mass of capable, trained and relatively inexpensive Ruby programmers available and it is at that point that producing a significant government application in Ruby becomes a workable proposition.

While I am sure that I and a number of my colleagues would be more productive writing in a higher-level language than Java, that is not the only consideration in software.  Also, we get good at bending Java to our will. All those frameworks and tools that are out there - if you find a small set of good ones that you know how to use then they can help a great deal.   

We can go on about how brilliant we are and how we can produce brilliant code and how we can maintain it without too much trouble. But it is not the brilliant “we” who are going to be maintaining that code and that is what we need to be wary of.