Empire-db » The principle of string-free coding Download

The principle of string-free coding

We believe that for the issue of code quality, string literals are the root of all evil. In data persistence string literals are often used to specify property names or even whole SQL fragments. This makes code harder to develop and even harder to maintain and can only be balanced through extensive and expensive testing.

We further believe that the most powerful tool for detecting and revealing errors is your compiler. Thus we think that no XML files should be used and code should be written in a way that gives the compiler the opportunity of performing this task as good as possible. In practice this requires working with object references rather than string literals wherever possible.

For example instead of writing:

StringBuilder cmd = new StringBuilder();
...
cmd.append("employee.lastname like '" + name + "%' ");

it should rather be somewhat like:

DBCommand cmd = db.createCommand();
...
cmd.where(EMPLOYEES.LASTNAME.like( name+"%" ));

Going beyond the persistence layer

But our objective is not only to stop using string literals in the persistence layer but all the way up to the presentation layer. This is especially hard for Web application development as it means avoiding string literals in JavaServer Pages as well, which in turn requires avoiding expression languages (EL). However it is possible. As an example consider a typical data driven Web application based on JavaServer Pages. With traditional Struts2 tags a particular property must be specified in the JSP as follows:

  1. For the list view when displaying a list of query results:
    <s:property value="employee.lastname"/>
  2. In the form view when displaying an input control for the field:
    <s:textfield key="employee.lastname" size="40" required="true"/>

Using object references this code can easily be written string-free as follows:

  1. For the list view when displaying a list of query results:
    <td><s:property value="<%= EMPLOYEES.LASTNAME.getName() %>"/></td>
    or even simpler using Empire-Struts2-Extensions:
    <td><e:value column="<%= EMPLOYEES.LASTNAME %>" /></td>
  2. In the form view when displaying an input control for the field:
    <s:textfield key="<%= EMPLOYEES.LASTNAME.getName() %>" size="40" 
                 required="true"/>
    or again simpler using Empire-Struts2-Extensions:
    <e:control column="<%= EMPLOYEES.LASTNAME %>" />

With our unique Empire-Struts2-Extensions we also address the issue of form data validation in order to abolish the need for redundant and unsafe XML validation files. The same can be done for other applications frameworks – we’re waiting for your contribution.

Summary

With Empire-db we proclaim the principle of string-free coding by using object references rather than string literals for both query definition and metadata access and from the persistence layer up to the presentation layer. In order to make this possible a consistent way to address properties and their meta information is required that goes beyond the requirements of data persistence. Empire-db’s object model schema definition in conjunction with its data and metadata interfaces provides such a mechanism. The result is unprecedented compile-time safety leading to reduced cost of testing and maintenance.