Java Code Geeks

Monday, December 29, 2014

Why Scala is hard to grasp for Java developers

Scala is a multi-paradigm language. It has characteristics of object oriented programming, functional programming, meta programming (language oriented programming or DSL). So a java programmer might write scala code which looks completely different than a Clojure or Haskell programmer writing the same scala code. This is because java developer might use more of Java style (mutable variables, for loops) whereas the developers from functional programming style might use more of functional aspect of scala.
Here is a sample example, different ways to calculate sum  of a List of Double.

 class Test {  
  def sum(data : List[Double]) : Double = {  
   //uses a mutable variable to produce the sum, this is most of the java developer might use  
   //but it is not the best way to do so. Because most of the functional language does not like  
   //to have mutable variable  
   var sum : Double = 0  
   data.foreach( v => sum = sum+v)  
   //Fold and Reduce are two techniques calculate the value at one shot (without mutable variable)  
   val totalSum = data.foldLeft(0.0D){ (total,n) =>  
    total + n  
   val anotherSum = data.reduceLeft[Double]{(acc,n) =>  
    acc + n  
   //now short hand of fold and reduce. Underscore is positionally matched arg  
   val yetAnotherSum = data.foldLeft(0.0D)(_+_)  
   println("Sum = " + sum + " total sum = " + totalSum + " and another sum = " + anotherSum + " yet another sum = " + yetAnotherSum)  
   return sum  

As a result, a new developer gets confused which is the right way of doing thing. Think that you write a piece of code and gave it for review and even reviewers fight over the right way of doing it.

Overall as a rule of thumb, I think it is better if we consider Scala as a "functional language" and try to use the best practices from functional world. This is why I have started advising Java developers to learn one functional language like Haskell or Clojure before picking up the "Scala" book. This tunes java developers mind towards functional world and come out of habits of using mutable variables or explicit for loops.

This is however a delicate topic and would love to hear more from users who transformed from Java to scala developer.

Thursday, July 24, 2014

Spring Annotations vs xml configurations

I faced this question myself while working on projects. There are many school of thoughts on this topic, however after practicing both the styles, I have come to a point where I can compile a list of guidelines when to use one vs the other. Here is my guideline

  1. If you follow clean architecture principles, you must have heard uncle Bob repeatedly saying "A good architecture allows us to defer critical decisions like UI,framework,database etc". If you want to use spring as a framework to manage dependencies among your beans, do you want to minimize spring dependencies or want to move out of spring framework in the future? If yes, then go for XML which externalizes the configurations. But then in real world, I have not seen any big project that started as a spring project and then later got converted to a JEE project, so if you are spring shop, no problem to use annotations.
  2. Are you writing a library/API which will be used by many teams? In that case, it is easier for everyone to read and understand the XML config. All the bean dependencies are in one place so it is easier to understand rather than downloading the source code of your library from maven repo and go through the code/annotations.
  3. If you need to use spring transactions, I recommend to use annotation. It is hard for a developer to understand that your methods need transaction if that is externalized to a XML config, rather if it is annotated in your class/methods that is easier to follow and understand.
  4. If it is a considerable size of a project and you care for application startup time, then pay attention to context component scan. It may take long time for spring to scan your classpath, read annotations to load all the annotated beans and their autowired dependencies. 
So both the methods are useful and that is why both of them are supported. Choose one over other or choose both according to your need. You can of course mix both the approaches in one project.

Wednesday, July 23, 2014

Discussion around TDD

Recently there have been quite a few discussions on TDD, its usefulness and whether it helps in design or not. It first started from a blog post from David Heinemeier Hansson(creator of Ruby on Rails) - TDD is dead. Later it became more interesting when David, Martin Fowler and Kent Beck (writer of Junit) started a discussion on google plus. The videos for this series can be found here .
Whether you are practicing TDD or not, I think this is a very good discussion to listen to.  

Sunday, August 19, 2012

When to use java refection

Recently I faced with this question when to use java reflection? Many frameworks use it but we still keep saying do not use it in day to day work.  What's the real deal with it?
There are some real problems with using reflection,  java reflection is a hack from java compiler perspective. Reflection takes away all the compile time type checking of java and makes it more like a dynamic programming language. So compiler does not able to catch errors at the compile time but errors are thrown at the runtime. No IDE can refactor code written with reflection. If you refactor and change the signature of  the method, build will be fine but at runtime the code can fail because some code was using reflection to invoke the method with its old signature. These are some real reasons why we try to stay away from reflection. Above all of these there are performance issues also. Direct method call may be two to fifty times faster than the reflection counterpart, this is however very use case specific but in general, code with reflection runs slower on JVM.
But reflection is a powerful technique and it has a very clear use case. At the time of compilation if component A has no knowledge of component B but at the runtime, A wants to use classes of B, then A needs to use reflection. Example, Junit framework has no idea what test classes we are going to develop, so it needs to use reflection to understand the test class and methods. Hope this will clear the doubts in your mind as when to use reflection and not.

Tuesday, July 10, 2012

Client Side rendering - the new MVC

Client side rendering is getting popular now a days. Let me give a little bit of background and share my view that client side rendering is a good thing.
In pre historic days when MVC was not much popular, people used to write simple javascript to render a HTML page. However, managing this form of page is hard, because if you want to do a simple test like give 10%age of your users a new look and feel of the page to test whether people will like that new UI, will make all if - then - else statements inside of the javascript. As the requirements get complex, the UI get complex and javascript becomes unmanageable.
Then comes MVC world, where view layer is clearly a seperate layer managed in the server. Like some uses JSP as the view layer or template engines like Velocity as the view layer. For a velocity type of engine, UI developer writes the skeleton of the HTML page, server sends the model (data) and the template to the template engine and template engine renders the final HTML by merging the template with the data. Spring provides integrations with such template engines to resolve the views. This was a good strategy untill Ajax came into picture. In case of Ajax, the basic webpage loads fast and then lots of small fragmented request goes to the server and server sends back smaller data set back to the client. Now usually serer sends back small JSON objects and there needs to be some unit at the client side which will read this JSON and render the HTML. This pushed the concept of template engines to the client side.
Client side template engine like dust.js or mustach has similar concept like velocity template, they take the template and the data as the input and renders the HTML. Only difference is that it happens at the front end and not at the back end. One can push this concept little further and instead of the using Ajax, from the first request itself, they can use client side rendering. The flow goes something like this -

  1. User request the webpage on the browser
  2. webpage sends the request to the server
  3. server send back a basic html which has reference to the client side template js and the precompiled js files
  4. Server does not close the connection, but keep flushing JSON data as and when ready to the same client connection
  5. On the client side, template engines take the template name and the JSON data and renders them
Some useful things are happening here -
  1.  Load on the server reduces as the server is not merging the template with the data, its the client side browsers that are doing it. So this solution is scalable as the server load goes down
  2. Since javascripts can be cached by the browsers, so once the page is loaded, sub sequent operations on the page becomes faster as the template engine and the pre compiled javascript templates are already loaded in the browser memory.

Monday, July 9, 2012

Why business logic should not be in database stored procedures

Couple of years back I worked on a project which is database intensive. It was so much database intensive that most of the business logic were written in database stored procedures. Java code was a thin wrapper on this which used to call these procedures. There are lot of things that go wrong with this model of application development. While in all these years I had this in mind that it is bad to develop software whose all the knowledge is in the procedures but I could not itemize my thoughts exactly whats wrong with this approach. Fortunately Pramod Sadalage from ThoughtWorks have given it the details ( here are the below reasons why
  • Writing stored procedure code is fraught with danger as there are no modern IDE's that support refactoring, provide code smells like "variable not used", "variable out of scope".
  • Finding usages of a given stored procedure or function usually means doing a text search of the whole code base for the name of the function or stored procedure, so refactoring to change name is painful, which means names that do not make any sense are propagated, causing pain and loss of developer productivity
  • When coding of stored procedures is done, you need a database to compile the code, this usually means a large database install on your desktop or laptop the other option being to connect to the central database server, again this leads to developers having to carry a lot of dependent systems just to compile their code, this can to solved by database vendors providing a way to compile the code outside of the database.
  • Code complexity tools, PMD metrics, Checkstyle etc type of tools are very rare to find for stored procedures, thus making the visualization of metrics around the stored procedure code almost impossible or very hard
  • Unit testing stored procedures using *Unit testing frameworks out there like pl/sql unit, ounit, tsql unit is hard, since these frameworks need to be run inside the database and integrating them with Continuous Integration further exasperates the problems
  • Order or creation of stored procedures becomes important as you start creating lots of stored procedures and they become interdependent. While creating them in a brand new database, there are false notifications thrown around about missing stored procedures, usually to get around this problem, I have seen a master list of ordered stored procedures for creation maintained by the team or just recompile all stored procedures once they are created "ALTER RECOMPILE" was built for this. Both of these solutions have their own overhead.
  • While running CPU intensive stored procedures, the database engine is the only machine (like JVM) available for the code to run, so if you want to start more processes so that we can handle more requests, its not possible without a database engine. So the only solution left is to get a bigger box (Vertical Scaling)

Sunday, March 6, 2011

Spring Session Scoped Bean

Somehow my experience with spring was limited so far in my career. Its just that for last one year, I have been using Spring extensively. I recently used Spring's Session Scope beans. We most of the time use Singleton bean or the Prototype beans but session scoped beans are very useful in those use cases where you need to create an object per httpsession. For example, the shopping cart object which holds the list of shopping goods that a customer is buying. We can of course do this manually by creating a new Cart object per user session, however what if the lifecycle of such objects can be controlled by outside IOC containers like Spring? Yes, Spring Session and Request scoped beans are just for that, to create an object and bind that to each unique session/request.
Now the question really comes how to test such a bean in JUnit test cases? One way to test is - test just the functionality of the bean, but if I was more interested in to find out that Spring really does create object per sessions and can maintain the lifecycle of such beans. I created test cases to create a mock httpseesion and httprequest objects and fake one http user session.

Here are the steps to get session scoped beans in Junit test cases
1) Create one Session aware Application context which is XmlWebApplicationContext
2) Create mock session and mock httprequest
3) Write test cases to get the session scoped beans and write the necessary test cases
4) tear down the httpsession and request.

public class AbstractSessionTest extends TestCase {
protected XmlWebApplicationContext ctx;
protected MockHttpSession session;
protected MockHttpServletRequest request;

protected String[] getConfigLocations() {
return new String[] { "file:src/test/resources/test-context.xml" };

protected void setUp() throws Exception {
ctx = new XmlWebApplicationContext();
ctx.setServletContext(new MockServletContext(""));
// set the session attributes that the bean might look for?
protected void createSession() {
session = new MockHttpSession(ctx.getServletContext());

protected void endSession() {
session = null;

protected void createRequest() {

request = new MockHttpServletRequest();
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(

protected void endRequest() {
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
request = null;


public void tearDown() {

One more useful tips here is - how to access the session variables from the Session Scoped beans? Well the friend here is RequestContextHolder. Using this - we can get hold of the httpsession inside of the bean and we can access any variables that was bound to this session in some http filter or any upper layer which might want to put session scoped variables to indicate one user specific data.

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession session = attr.getRequest().getSession(false); // true == allow create
String value= (String)session.getAttribute("varname");