Thursday, 7 February 2013

Why I Will Not Use Java


This post is quite old. I wrote it back in 2010, when I realized Java's main drawback is its verbosity. I didn't expand my thoughts, 'cause I was too engaged at work with C, Objective-C and Python. But in these days I read some articles about Java 8 and I felt very disappointed, 'cause it lacks some features, some snippets and doesn't resolve Java's main drawbacks.

Java Language is Verbose

Java Language is verbose, in the worst way you can say it, mainly for two reasons.

First: in 2013 you have still to write all getters and setters, when even Objective-C (whom is oldest) has properties since 2006 (Objective-C 2.0). Usually, you resolve this problem in two ways:
  1. Using IDE's automatic getters/setters generator
  2. Using some libraries that simulate properties throught annotations
Yeah, they works. But they also looks like an hack more than a solution.

Second: Java doesn't support default parameters on methods, such as
public  TestClass( int id, String name,
    int value=0,
    double latitude=12.0,
    double longitude=42.0)
A solution is to write TestClass factory methods, but it's boring as writing all getters and setters. Furthermore, with factory methods, class constructor is private, so extending your class is not possible. On wikipedia there are a list of limitations connected to this pattern.

Final masterpiece: we still haven't multi-line strings: we have to add a '+' sign to concatenate every line.

Classpath is Verbose

Java's Classpath is verbose 'cause it's over-engineered. Basically, a well engineered library is a good idea, but Java's classpath exasperate this concept, moving to disastrous consequences: you must open a BufferedReader for every stream you open; every BufferedReader requires a InputStreamReader and so on, creating a chain of objects even to read a text file. By example
public void PrintFile(String filename) throws IOException{
    BufferedReader in;
    String line;

    in=new BufferedReader(
        new FileInputStreamReader(
            new File(filaname)));
    line=in.readLine();
    while(line)
    {
        System.out.println(line);
        line=in.readLine();
    }
}


Now, how it looks like in another language? Such as Python?
# Reading all lines as a list of strings
def PrintFile(filename):
    f=file(filename)
    lines=f.readlines()
    for l in lines:
        print l

# Reading a line at time
def PrintFile(filename):
    f=file(filename)
    line=f.readline()
    while(line):
        print l
        line=f.readline()

Want exaggerate? Let's see it with ANSI C

#include <stdio.h>

void PrintFile(char*  filename){
    FILE* f;
    char* line;
    line=( char*)malloc(256);
    f=fopen(filename, 'r');
    line=(line,256,f);
    printf("%s",line);
     while(line!=NULL)
    {
        line=(line,256,f);
        printf("%s",line);
    }
    fclose(f);
}
Even ANSI C's stdio.h is simpler. And I haven't show you how to read a string from standard input. Another BufferedReader that reads from System.in.
I repeat: Java's classpath abstraction is good, shows many utilities. But it's incredible there aren't "commodity classes" such as Console to provide a simple
String a=Console.in();
Console.out(a);
Something similar exists for .NET Another example? Wanna read a XML file? Wow, Java+XML sounds good, eh? No! Default SAX reader is something crazy, full of nested constructors, classes to extends. Even Cocoa's default SAX reader is simpler to use! Now I remember when I was scared to use XML with iOS for the first time: I had really bad experiences with Java.

Java's advocates says "Verbose is Beauty"

Bozho says in this post he likes Java's verbosity because it forces to understand what's happening under the hoods. It's not a good reason to make this a standard way to do simple tasks.

Conclusions

Java isn't slow as you think. HotSpot virtual machine gives you fastest execution among all interpreted languages. Faster than HotSpot there are only compiled languages such as C or C++. Furthermore, Java provvides things such as
  1. Huge standard library
  2. Portability
  3. Garbage collection
  4. Many tools for your programs
If you are not bored by Java's verbosity, probably you will work with it with success. There are some interesting works, such as CassandraDB, Minecraft and Spiral Knights. Probably, if you're going on to write a new videogame for PC or a high-performance server, probably Java is the right choice. I am bored by Java classpath's verbosity. I prefer to write my own jobs in Python or in C

UPDATE

Latest Java's exploit aren't making me happy: security holes and broken promises about Java Language new features make Java developers angry. Please Oracle: you have Sun Microsystems's legacy. Do a good job.
Post a Comment