Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, May 27, 2008

Java should have value types

One of the features Java lacks is the notion of immutability (similar to some variants of constness in C++). Immutability is a very important design tool in my opinion and I would put it the highest on the list of features I would like to see in a new Java. This one is actually quite suitable for a point release and I really hope that JDK 1.7 will at least get an annotation for immutable types (such as the one proposed with the JCIP book). It deserves better than an annotation (or for that matter a tagging interface), but I don't have any hopes of Sun adding keywords to the language anytime soon. You can follow the RFE in Sun's bug parade if you are interested.

Others have made good summaries of why you want to use immutable objects, so I won't list the reasons why they are good again. In short it all comes down to easier and safer code. It also allows for performance optimizations that are not possible without (hashcode caching and String.intern() to start with, but even more in distributed computing).

What I would like to see is to go a step further and introduce value types. With this I mean types of objects that are not only immutable but are treated as values in all regards, which most importantly adds the notion that they should use value equality, i.e. two instances of a type are considered the same whenever all members are the same. The members of value objects are restricted to other value types.

Some existing languages use this notion, most noticably C# (in form of structs and enums) or Lava (a language unrelated to Java). Java itself has a number of value types such as all primitives and some object classes such as String. But the notion of a value type is not well established and in fact primitives are treated quite differently to String, which in turn has its own specific hacks in the Java compiler.

Instead of having primitives and targetted optimizations for specific classes such as String Java should have a proper notion of value types that are:
  • immutable
  • reference only other value types
  • use value identity
All value types would be first class objects (no more special primitives) and all the optimizations done for primitives and String could be extended to include not only further JDK classes (there are plenty of candidates) but also classes defined by a normal Java programmer. Imagine having the equivalent of String.intern() for all your data.

So my fictional version of Java that I call Java3k would have value types so we:
  • can write safer code
  • don't have to worry about writing hashCode() and equals(..)
  • can rely on a lot of performance optimization by the compiler
  • can forget about primitives (and all the boxing/unboxing annoyances)
  • have an easier time writing multi-threaded and distributed applications
I would even consider disallowing anything but reference identity for types that are not value types, but that would most likely cause too many portability issues when migrating from existing Java code to Java3k.

I think a separation of your types into value types and the rest (which can then further be separated) is a very valuable approach and personally I prefer taking it as far as I can in the sense that I try to make as many types value types as I can and disallow value identity outside that group. It makes for much cleaner code even if you don't have language support.

The other effect it has is that it leads you towards writing more and more code as pure functions. Instead of mutating state on objects you let them create new objects without changing themselves. The substring method on Java's String class is such an example: it doesn't change the String instance it is called upon but creates a new object. That might make it confusing for beginners, but it means that it will never cause unwanted side effects (and the part of being confusing is only since Java let's you ignore return values, which is a feature that falls under the category of Pretty Bad Ideas). And not having side effects is extremely valuable -- similar to many other good ideas you have to get used to it (unless you have been raised on a functional programming language), but once you get the idea you don't want to miss it.

So that is my first feature for my fictious Java3k: explicit value types instead of primitives and mystery hacks for String and the like. It would not only add a lot of value in terms of safer and faster code but also get rid of enough cruft to effectivly reduce the language complexity significantly.

PS: once you have value types you could also add derivation by restriction such as used in XML Schema, but maybe that's a Java4k feature.

Java: getting old and crusty

One of the main reasons I finally decided to start blogging is that I find myself whinging about the state of Java (the programming language) again and again. That type of repitition surely violates the DRY principle, so I figured I should have one place to collect my thoughts in a reasonably structured fashion and then point whoever might be interested there.

So here we go...

I've been using Java for about 7 years now, which makes it my personal record. I wasn't there from the beginning (I never even had to use pure AWT), but I believe over the years I have seen enough to have a well-founded opinion about Java the language and many things in the Java eco-system.

Fact is that I think Java the language is a very poorly designed programming language that collected a lot of cruft over the years. Sun's inability to stop chasing the holy grail of backwards compatibility (but inavoidably failing) has stopped the language from being cleaned up from problems, while at the same time the need for new features added more complexity into the already brittle system. The result is something that is hard to grasp for an experienced programmer and in my opinion causes a lot of conceptual overhead in every single programmer that would be avoidable. And of course it raises the entry barrier, adding cost to learning Java and most likely stopping quite a few people from trying it in the first place.

I even go further and claim that Java as it is causes a lot of cultural damage. While it certainly was an advancement over previous languages in some regard, it left many a problem unsolved and added some new ones. The workarounds to these problems are now what most programmers consider state-of-the-art and while I never counted I would guess that about half the patterns in the GoF book wouldn't be needed if our programming languages weren't as bad. Instead of looking for proper solutions people take the problems as unavoidable since they do not know any better. Java Puzzlers are not fun, they are a warning sign.

You might wonder why I rant like a grumpy old man instead of just going somewhere else. The problem is that the grass doesn't seem greener on the other side of the fences.

Certainly C# has evolved a lot more over the years, but somehow .NET as a platform doesn't seem to offer all the luxury of hundreds of OpenSource libraries and great development tools that the Java ecosystem has to offer. I must admit that I haven't really tried .NET yet, so maybe I will stay corrected on that one one day.

On the other hand the languages that are based on the JVM and aim to interoperate with the Java world are not really what I like. Groovy, JRuby, Jython etc. lack manifest typing -- what I am looking for is a language to replace Java in long-term projects and I am a strong believer in having expressed your intentions well in code (something I will probably talk about in later posts). And while I consider Scala a very interesting language it seems a bit too much to change at once and a bit too experimental in places. I can see myself coding in Scala in the future, but I find it hard to imagine the type of teams I have worked with over the years to switch over anytime soon.

What I would like to see is a new version of Java -- something the JavaPosse discussion group calls "Java3k", alternative labels I have seen are "Java 2" or "Java++". I quite like the first one since it seems to imply that it is not really about time-to-market. So from now on I will refer to a new and extra fabulous version of Java that has all the problems fixed as "Java3k". The even more fictious version after that is "Java4k", which I might refer to if I discuss features that I don't think are ready for mainstream yet.

If I stick to my plan the following posts will discuss in detail certain aspects of what I would like to see in a Java3k. The pattern I am intending to use is first to explore a problem in the existing Java language, then discuss what could be done about it in general and how I would like Java to change. Whenever possible I will try to link the ideas to their origin in other programming languages, but unfortunately I will probably not be able to provide strong references for everything. That is usually not because things are my own inventions, it is more that I am not good in keeping track. Even if I remember that I got an inspiration somewhere I do not always recall where I got it from. Feel invited to comments on missing links.