The Joys of Development Frameworks

Posted by & filed under , , .

ScriptingI haven’t had much time lately to use my beloved mobile WordPress application – due to a mixture of owning a Kindle nowadays and being swamped with work. Ironically it was some of the recent work that forced me in the end to write this article so today I gave up my Kindle on the way to work in favour of this.

Many of you programming in Java would have employed some frameworks at least from time to time. They are great allegedly as it makes code so much easier to write, read and maintain. With some of them being so popular that it’s difficult to find developers not using them it means sometimes you pretty much have to adopt them. Up to the point that these frameworks are being used even when not needed – and in fact when using them makes writing code twice as hard.

I’m going to refer to one particular project that had very simple requirements: what was needed basically was a web app that offers 3 url’s: 2 of which which show an image based on a simple set of predefined rules (eg if user IP in this range show a.gif, if cookies enabled show b.gif etc) and another component which sets a cookie. The set of rules are fixed and unlikely to change in the next 5 years – so there’s no need to complicate the code with adding any business rules components etc but rather just implement them in the code in an if/then/else manner. In Java terms that translates into providing 3 servlets or jsp pages, one for each of the above.

In implementing the above I had to review some code previously written for the same purpose by another developer- this code was abandoned in the end not because the developer failed to achieve these goals but for other reasons outside the scope of this post. So bearing in mind that we had some code that “did stuff” it was obviously worth investigating this code and see if I can pull out any of the existing code and re-use it. Unfortunately I decided to scrap this code in the end and start fresh!

Since the requirements were very short and as such the web app was going to be very lightweight I decided not to employ any other libraries and rely entirely on the packages in java 6 (obviously the exception being the servlet API which was indeed needed). So I ruled out log4j and all of its dependencies in favour of the java.util.logging. I ruled out Spring MVC as there was no model and view and as such this wasn’t required. My app had 3 servlets, an utility class and 2 other classes used for configuration and monitoring (for my own benefit); configuration was to be done via web.xml so altogether you could count about 7 files in the project (add a couple more for unit tests granted!). It compiled in a second and the size of the generated war file was around 40kb (plus the servlet API). You can virtually run this on a mobile phone!

Now I had to look back at the previous code (out of sheer curiousity if nothing else!). So, we have:

  • a servlet + a jsp for each of the 3 URL’s/actions I’ve mentioned – so 6 files already just for this
  • a Spring context file which beans used for configuration etc
  • a http-views.xml which defines servlets + their mappings and also defines a jsp “resolver” servlets to handle jsp pages which are now stored under WEB-INF/… etc
  • a web.xml as before
  • a couple of other Java sources for the same purposes as in my case (configuration and monitoring)
  • Unit test classes

So I’m counting about 11-12 files not including the test files versus 7 in my case! The compilation takes slightly more now (mostly due to packaging all the libraries needed by Spring I’m guessing) and the result war file size is the order of Mb!

The point is though not how great I am with my programming (we all know that :D) and it’s not even how rubbish the other developer was (in fact he is very good!) — it is more about the fact that people “buy” so much into a technology that simple things no longer can be done in a simple manner but have to employ this technology. Even though it makes solving the problem twice as complicated by employing this framework / technology!

Spring is great, don’t get me wrong — but just like any other technology it’s not always the right tool for the job — why use an excavator when a simple spade would do?