Saturday, 26 April 2008

Where Sun Don't Shine

Sun are a big company. Really big. They created some of the most reliable server workstations, some of the fastest processors, some of the most stable OSs in the world. And they created the as of now most-used programming language, Java. Java is sort of nice. It's dumbed-down C++ with some enhancements, it's not feature rich, but nobody in their right minds wants a programming language to be feature rich. I'm a big advocate of "the right tool for the right job" and feature rich languages seem to contradict to that credo. Java is the right tool for many jobs: GUI creation in Swing is easy and straight forward, networking closs platform is painless and portability is one of Java's main selling points - because it's really easy. The only thing you have to keep track of is the JVM and source version and there you go.

I guess if we lived in milk-and-honey-land, that would make Java a perfect programming language. But it isn't, and milk-and-honey-land is far off. Java was supposed to bring interactive web pages to the clients more than ten years ago (note that JavaScript has nothing to do with Java). It failed. And miserably so, I must say. There's nothing that turns me off more than a page using a Java applet. They are ugly, they are slow to load, memory hogs, and they are buggy.

But I don't care about applets. Servlets are a much better technology anyways, and I found Java to be really useful for Web programming. But this rant is really about Java on the Desktop and about Java for the programmer. Over the years, I've come about some rather annoying misfeatures, bugs and Sun's stubborn way of not wanting to address bugs that have been known - literally - for years.

  • Swing sucks, after all. It's ugly. It's slow. It's bloated. It lacks a set of really useful and easy-to-customize stuff in the library. Heck I remember hacking Delphi used to be more fun than Swing. At least you didn't have to call.thatMethod().somewhere().InAnInnerClass(WayDown.theCamelCase.objectHierarchy()); But see below
  • Swing sucks even more. Ever tried to run a Swing app on X using a window manager that is not one of {Compiz,Metacity,Kwin}? Well, good luck. <3 grey boxes. This has been a bug for a long time, Sun claim it's fixed. It ain't. It's fixed for Compiz, everything else still has problems. This is because some mind-bogglingly stupid intern at Sun had to hardcode the goddamn fucking names of Window Managers into .equals() checks of the kluges used for circumventing the original bug. The result? My favorite window manager, XMonad, displays Protege like this.
  • Many people seem to argue against 8-space indentation on the basis of Java code exhausting horizontal screen space even on 1900xX resolutions. That shouldn't be the case. typedefs, operator overloading, sane object names (SomethingBadHappenedButIReallyDontKnowWhatExactlySoHeresAnException, anyone?) and more modern stuff like partial hiding/importing/exporting (like in Haskell) could greatly improve on the situation.
  • Inexplicable syntactic difference between dynamic and static arrays. Yeah, I know, it's just syntactic sugar. Go write your stuff in ASM, then, if you don't like that.
  • No way to generalize a static array. If T is a generic type, T[] stuff = new T[x]; is illegal. But a function header like void some_function(T[] stuff) isn't. This means that you actually can have static arrays of generalized types, but you have to work your way around like T[] stuff = (T[])Object[x](); This is ugly, and, moreover, it's dangerous. It's also a restriction that hits the language quite hard. Don't believe me? Read on...
  • Not only that, but you are not allowed to create arrays of inner classes of parameterized classes. That's right. No, I know it's stupid, but it's the way it is.
    public class General<T extends Something> { InnerClass[] ic = new InnerClass[10]; private class InnerClass { int data; } }
    This will not compile. It fails with the oh-so-descriptive-and-helpful error message "generic array creation". There you go, the inner class doesn't even use that freaking parameter type. It's enough for it to be a subclass of a paramaeterized type. This is especially annoying when you have to create, say a tree class that is to store all sorts of interesting different types. That would be what them Generics are good for in the first place, right? Well, if you want the tree to have, say, 4 children per node, you'd love to do that using a static array, right? And inner classes are usually the best approach for making a specialized Node class... But in Java, you have to suffer compiler warnings and unsafe typecasts to do that.
  • You can't pass functions as arguments. That's because there are no function pointers. Meh. That makes NLP systems and natural language processing (especially computational semantics) unreasonably complex, because you'll have wrap it all up into classes. But this may fall under the right tool for the right job clause. I've long since wanted to switch to Prolog, C++ and, lately, Haskell for natural language processing. They're way batter at that than Java.
  • This isn't even as much as I expected it to be, but there's more that I've missed for now. Jamie Zawinskie has a lot more on this, but I do not share all his views. Maybe it's that I'm just no C programmer and therefore I have a whole different set of expectations. Maybe I just don't understand all of his points, but I couldn't agree more with most of the stuff he describes. When you google for "why java sucks", you will find a whole bunch of pages. About 3000, for the phrase query. That's a lot for a phrase query.

    I don't say Java sucks in general; it's an OK language for quite a number of tasks. And it's probably even the language I know best. But it has deficiencies, and quite some and then some more. I'll be moving on to greener pastures from now. Prolog sounds nice, it's well suited for what I'm currently doing. Haskell sounds even nicer, and I spend every free minute I can find with monads and similar stuff. I'll probably blog about it, too, when I'm good enough at it. ;-)