воскресенье, 3 апреля 2011 г.

My 6 tips for designing Java-based solutions

Database choice

The common mistake is to use MySQL in areas where PostgreSQL is a better choice. I mean enterprise applications with hundreds of tables and complicated normalized database structure used by intra-net users. I think it happens because MySQL is easier to set up and administrate, and more people familiar with it. While using MySQL is the right choice for high-load apps with denormalized database structure, and for projects with simple database structure, MySQL's query planner is not as good as PostgreSQL at the moment of writing this article. In addition, PostgreSQL has much stronger support for stored procedures, it implements wider set of ANSI SQL standard than MySQL. Nowadays almost every enterprise-application needs GIS functionality, and Postgresql has GIS support. Another advantage of PostgreSQL is that its date/time types include timezone information, and that will save you a lot of time by avoiding conversions from one timezone to another if you have users who operate in different timezones, for example, schedule service. I am not saying that it is impossible to use MySQL in this area, it is certainly possible, but the cost of development would be higher. If you need to import large bulks of data,it is likely that you will have to use stored procedures to import data in a reasonable time frame, and stored procedures in PostgreSQL exist for decades while MySQL adopted them 1 year ago.

Of course, it is possible to write a query which will be executing very slow on PostgreSQL, but in general, the chances to produce a good query plan are higher when using PostgreSQL, particularly for sub-queries.

Web framework choice

Ironically, in many cases decision to use Java for web development is a mistake. There are two cases here. Case 1, we can avoid using Java language, but still use Java platform, for example, with Grails. Case 2, we can avoid using Java platform at all, and use Apache plus some scripting language like PHP, Python or Ruby. My favorite is Python.

The problem with Java language for web is long development cycle. For example, when you add a method or variable to a Spring MVC controller, you have to re-compile the re-start web server. People who use scripting languages like PHP, Python or Groovy just save file on the disk and refresh the page, and that is much faster. So I suggest considering implementing web layer in Django, Ruby on Rails or Grails, and that can speed up web development from 2 to 5 times.


Let's assume that you decided to use Java for web developement. My advice is : avoid using JSP in any form, including JSTL! I suggest using FreeMarker which is a fantastic templating engine with clear and elegant syntax. Velocity is also a good choice. If your system sends emails, then use FreeMarker or Velocity for message body.


Often people write classes which provide access to applications configuration. It is better to spend that time on something useful because commons configuration is an excellent library for configuring your application.


If you have functional tests, then TestNG is a better choice than JUnit. Functional tests are scenarios which are executed in an environment which is close to production environment, I mean real database, JMS server, probably mail server. For functional tests you will want to control the order of execution, for example, first you call your public API to enter some information into the system, then call API to update it, then call API to remove it. The order of operations is clear: first create, then update, then remove. In TestNG you can define the order with annotations. Of course, you can use JUnit, but TestNG is a more elegant solution for functional tests.

Temporal types

When designing API avoid using java.util.Calendar, use Joda Time instead. It will save you the time you would spend on writing unnecessary code for timezones, periods, intervals, time calculations.

Комментариев нет: