Sunday, February 27, 2011

Use Java as a dynamically typed programming language

Java is a statically typed programming language. This means that at the time of compilation the types of variables used in the program are known. This feature is one of the most powerful (if not the most powerful) thing, which gives a programmer warnings about bad code written before he runs it. This allows also to build powerful development environments like Eclipse with content assists and refactoring support. Unfortunately there is no rose without a thorn. Because of static typing a program code is a much bigger (types need to be specified in declarations, casting is necessarily) and sometimes simple problem becomes too complex. If you don't believe in this statement - check the core of JavaServer Pages technology (I mean scriptlets, without JSP tags) and see how much unnecessarily code you need to write just to print some attribute's field on the screen.

There is a lot of frameworks written for Java. Most of these frameworks use interfaces or abstract classes as a building block. Let me give an example: in order to create a HTTP servlet you need to create a new class extending the abstract HttpServlet class. This is simple, but... hmm... Do you really need to write a dedicated class for every action? It will not be better if you have multiple actions grouped together? Because of static typing you can't just do this easily, which of course does not mean that this is not possible. The solution is the reflection API bundled into JRE. You can execute methods, access or update fields etc. of objects of any type.

As a simple example what you can do with reflections check the following code of controller written in Egg Framework (full stack web framework being developed by me):

package controllers;

import static framework.GlobalHelpers.*;
import framework.Response;

public class SampleController {
 
 public void newProduct() {
  Product product = paramsAsBean(Product.class);
  // TODO save product in db
 }
 
 public Response removeProduct() {
  if (session("logged") == null) {
   // not allowed
   return renderText("Access denied");
  }
  // TODO remove product from db
  return null;
 }
 
}

The Egg Framework uses reflections extensively. There is no configuration (Convention over Configuration), no interfaces. Every class can be a controller, every class can be a form bean etc. (just plain old java objects - POJO). In this example Egg Framework uses request parameters sent to the server to decide which method (action) to invoke. Note that both methods have different signatures - not only the name, but the return type. But for Egg Framework it does not matter. If the action returns something not null then Egg Framework handles this, otherwise it runs normal processing. In this example I have used also static methods (check the import static statement on the top). Static imports are really great and can simplify your program dramatically.

So what the conclusion for this post? I think that there is still a lot to do in Java libraries, frameworks and APIs design. Egg Framework is one of many examples how to make Java programs looks prettier.