Why Java forces us to write rubbish code?

Anybody I don’t like, read this! :

com.superframework.core.base.Object object = new com.superframework.core.base.Object()

Sometimes one cannot avoid this rubbish in Java, even today. I do not wish my enemies to read such code, not in my code I want to be proud of!

I wonder how many times I have asked myself why Java is so complicated to read and write? Why I have to keep writing so many characters and lines of code to express a simple repetitive task? It’s appeared to me like Java language designers keep torturing developers by forcing us to use constructs invented 15+ years ago without an alternative.

But this one is simply an outrage. Considering that e.g. Python is even older language than Java and you never have to write complicated namespaces in the code more than once in the import. On the contrary, Java does not provide a simple and readable solution when two classes of the same name have to be used in one source file.

Consider following code comparison.

In Java:

import com.myapp.base.Object;
...

  com.superframework.core.base.Object frameworkObject = new com.superframework.core.base.Object();

  Object javaObject = new Object();
<br>

In Python:

import com.superframework.core.base.Object as FrameworkObject
import com.myapp.base.Object as MyObject
...

def frameworkObject = new FrameworkObject()

def object = new MyObject();

No commentary needed, everybody can surely see the difference. Although, you can see the horrible Java code above mostly in generated code, sometimes it is not easy to avoid name clashes, which lead to Java code one would puke from. In this particular example, imported Object can be even easily confused with standard java.lang.Object. In Python, you may name it as you wish to avoid name clashes.

After many helpful improvements in this direction in Java 7 and Java 8, I strongly recommend also to revise a recommendation raised almost 15 years ago for Java version 1.3:

I understand that 15 years ago adding syntactic sugar to the language was not a top priority. But nowadays, if Java aims to stay the language of choice for millions of developers, it is necessary to consider small improvements like this, which have drastic impact on quality of the code and developers’ work.

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha loading...