Java is Getting Old

March 13, 2010 § 1 Comment


For the last 6 months, I have been programming with both Java & Python. Python is new for me. Java is my whole time programming language, its almost 8 years that I do Java programing (since the first years of University). I love Java, I love its huge community which is very important for a programming language success, and finally its greatest IDE ever Eclipse.

However, there is a problem in Java, the language syntax is not evolving, I remember one of the last improvements of Java 1.6 was a new look & feel (Nimbus)! do we need another look & feel? do we need Java for desktop programming? come on Java is dead for this field, focus on your power, the web, backend applications, messaging systems, and what about an addition of some of the things I am proposing here?

I still remember the day Java introduced the for each loop in the Java 1.5 release (which by the way was available for years in other programming languages):

String[] myArray = new String[10];

for ( String str: myArray ) {

System.out.println(str);

}

This was a very welcomed change, it increased productivity when using collection classes (do you still use an Iterator)? productivity comes from writing less annoying/repetitive code, like getting the Iterator object, doing a while loop, iterating over the collection… bla bla bla.

These are the moments when your brain as programmer turns off, and your fingers does the work (there is no thinking, just annoying code). This was not a very big problem when everybody had to do it this way. but when I got used to the for each loop, I started working on a project developed in Java 1.4. Every time I came to iterate over a Collection frustration increased because I was feeling unproductive (I know that I can use eclipse templates). Steve McConnell in his book Code Complete 2, lists the ratio of high level language statements to equivalent C Code, which shows the weakness of Java productivity compared to Python.

This is exactly what is happening with me after using Python, there are some great syntaxes in Python that I really really really wish Java would introduce in their next release, they are trivial ones, and easy to introduce in the language, but they have a great impact on productivity, I report two of them:

1. Multi line Strings (Block strings)

These are strings that consist of multiple lines, Python supports them using three single ( ‘ ) or double ( ” ) quotations for example, an SQL query string could be written like this in python:

sql = “””

select *

from table1

where table1.col1 = 10

“””

However in Java it is usually done using a StringBuilder/StringBuffer like this (for those who do the same thing with a String don’t do it, they will slow down performance, remember Strings are immutable):

StringBuilder sql = new StringBuilder();

sql.append(“select *”);

sql.append(“from table1”);

sql.append(“where table1.col1 = 10”);

  1. I know you can write it in one line and its still readable, but assume the query is very big and can’t be contained in a line of 120 columns (code readability)
  2. I don’t want to put it in a .properties file, I just want this query string in my code, and I want it to look nice, don’t start with MVC.

2. Array & Collection Operations Using Operators Instead of Methods

A lot of our programming deals with arrays and collections, we get array values using indexes, we get subsets of an array (slicing), we swap items in arrays… etc, again Python beats Java in this too, for example in Java if you want to get the last character of a String you do:

String str = “ABCDEFG”;

char last = str.charAt(str.length() – 1);

Yes it is not rocket science but its frustrating, in python strings are lists of characters so what applies to lists applies to strings too, and getting the last character is like navigating the list backwards, making the code more readable, and faster to write (BTW, the Java code is 65 characters length, while the python one is 30 characters, the HALF):

str = “ABCDEFG”

last = str[-1]

Here are some other operations that can be done in python on any type of lists, which are not supported in Java, I used strings for simplicity:

str = “ABCDEFG”

str1 = str[1:3] #Substring from 1-3 (BC)

str2 = str[ :4] #substring from 0-4 (ABCD)

str3 = str[:-2] #substring up to length -2 (ABCDE)

str4 = str[0::2] #start from 0 and do steps of 2 (ACEG)

str5 = str[::-1] #start from 0 and do -1 steps, in other words invert the string (GFEDCBA)


These are a subset of the most important features that I wish Java will include in their next releases, its really annoying to do these stuff by calling class methods, I don’t care if Object Oriented allows this or not (a String is an object, and slicing should be done by a method) ok let there be a substring method, but I want to extract substrings like python too, its faster, easier, and more readable, and make it up to the programmer to choose between the bulky methods, or the intuitive operators.

One last thing, In this post I mentioned things that Python does in a great way, and that I wish Java will support them, but it doesn’t mean that Python is better than Java, there are many things I hate about python too, the most important one is the decision to use indentation instead of braces ( { ) for code blocks, which drives me crazy, there are many issues in Python too, Nothing is Perfect.

Advertisements

Tagged: , , ,

§ One Response to Java is Getting Old

  • Mohd Ajami says:

    Throughout the whole article I was like “I can’t wait to learn some Python” until the last line where you said Python uses Indentation instead of braces!! I’m not sure I can be patient as you on this, this first thing I will search for is how to use braces, even just virtually.

    Anyway, I like your writings, don’t stop!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Java is Getting Old at Omar Al Kababji.

meta

%d bloggers like this: