14 Dec
Some things I’d like to see fixed in Java
It looks like closures are making their way into Java 7. Huzzah! Since the Java guys are lavishing goodness upon us, I thought I’d put in a request to fix some of the petty grievances I have. I’m going to ignore the core libraries for the purposes of this, as that’s a huge subject and libraries can be replaced (see the date/time API and Joda time).
Replace the ‘final’ keyword with the inverse, ‘mutable’
Mutable state is a necessary evil. Immutable objects are simple to create and use, are automatically thread-safe and make testing much less painful, amongst other things. Moreover, the fact that classes are overridable by default encourages people to use inheritance rather than lovely testable composition for code reuse. Unfortunately though, nothing in Java is immutable by default, so you have to make a concious choice to do the simpler, better thing. It’d be much better to have to declare a member or class mutable so that developers had to explicitly do it wrong.
Allow labelled parameters
public void drawSquare(int ~width x, int ~height y) {}
drawSquare(~width: 2, ~height: 3);
drawSquare(~height: 3; ~width: 2);
That way I don’t have to go and look up the API to see which way round they have to go. Sure, leave the defaults the way they are – this clearly would have to be optional (maybe throwing a compile-time error if they’re wrong rather than swapping them?) – but it could help nip nasty logic bugs in the bud.
Allow default parameter values
public void foo(String bar = "douglas") {}
beats
public void foo(String bar) {}
public void foo() {
foo("douglas");
}
in my book. Syntactic sugar, but brevity and readability win out.
Implement arrays properly
byte[] a = {1, 2, 3};
byte[] b = {1, 2, 3};
a.equals(b); // will return false
Why? Oh, I can use Arrays.equal()? Fantastic, static calls, my favourite. Oh, and there’s the whole nested array thing too, just for extra fun.
Allow switch statements on Strings
switch(myString) {
case "foo":
// some stuff
break;
case "bar":
// other stuff
break;
default:
// even more stuff
}
is more readable than
if ("foo".equals(myString)) {
// some stuff
} else if ("bar".equals(myString)) {
// other stuff
} else {
// even more stuff
}
Syntactic sugar again – and also a possible source of bugs given casing and the Turkey problem – but I’d still like it.
Fix generics
Enough is written about this elsewhere. Suffice to say, losing generic information at compile-time is a whole world of suck (it’s why you can’t have primitives as generics without boxing, for instance).
Swing should be banned.
Ok, I lied about ignoring libraries, but it cannot be said often enough that Swing is pure distilled evil.


Posted by Jon Skeet on 14.12.09 at 9:23 pm
You know what? There’s a language/platform which allows almost all of that. (Not array equality, admittedly, and mutability is still the default – but the rest…) And the lambda syntax is nicer.
Let me know if you want me to recommend a book to help you learn it.
Posted by Adam Millerchip on 14.12.09 at 9:23 pm
I miss proper coding and being able to write posts like this.
Best I could tell you lately is the least bad way to hack something in Perl. I foresee a New Years Resolution…
Posted by Douglas on 14.12.09 at 9:23 pm
Anything to flog another book, eh Jon?