What we have here is a single identifier that, at 83 characters in length, is too wide to fit in standard 80-character-wide terminal.

File under “Why Java Is Not My Favourite Programming Language”.

(No, there is no reason why a similar identifier could not in principle be generated in some other programming language. But no other language has the programming culture that make such things possible.)

Let’s start by thinking about a very simple example. I’ve recently switched to using Ruby as my language of choice, after a decade as a Perl hacker. Ruby does a lot of things more nicely than Perl, including having proper object syntax, simple everything-is-an-object semantics, a sweet module/mixin scheme and very easy-to-use closures, so I’ve mostly been very happy with the switch. In so far as Ruby is a better Perl, I don’t see myself ever writing a new program in Perl again, except where commercial considerations demand it.

Today, I want to talk about a problem that I don’t have a good solution for, and throw the floor open in the hope that someone else does. Teach me, O commenters.

Suppose your program has to build a tree structure — for example, as the result of parsing a query in some kind of structured query language. And suppose that, having built the tree, you want to do several different operations that involve walking that tree. How do you design that program?

I’ve made no secret on this blog that I am fond of Ruby, and that I find Java rather verbose and awkward. But at the same time, I realise you can’t deny the commercial importance and ubiquity of Java. I’ve been hoping to find a way to combine the stronger points of both languages, and of course JRuby gives me just what I need.

So I’ve been experimenting with it for the last few days, and I like what I see.

For example, whereas in vanilla Ruby you might write:

def qsort(a)

return a if a.length <= 1

pivot = a.delete_at rand(a.length)

return (qsort(a.select { |x| x <= pivot }) +

[ pivot ] +

qsort(a.select { |x| x > pivot }))

end

In JRuby, you simply write:

jrequire ‘java.lang.Jruby.*’
jrequire ‘java.lang.util.ArrayList’

def public ArrayList<T> qsort<T> (theArray is ArrayList<T>)

if Arrays.lengthOfArray(theArray) <= 1

return theArray

end

theRandomInteger = RandomIntegers.rand(theArray.length)

pivot = Arrays.elementAtIndex(theArray, theRandomInteger)

Array.deleteElementAtIndex(theArray, theRandomInteger)

lowerTemporaryArray = ArrayList<T>.new

Iterator<T> iter = theArray.iterator()

while iter.hasNext()

theValue = iter.next

if theValue <= pivot

lowerTemporaryArray.append(theValue)

end

end

lowerTemporaryArray = qsort(lowerTemporaryArray)

upperTemporaryArray = ArrayList<T>.new

iter = theArray.iterator()

while iter.hasNext()

theValue = iter.next

if theValue > pivot

upperTemporaryArray.append(theValue)

end

end

upperTemporaryArray = qsort(upperTemporaryArray)

resultArray = ArrayList<T>

Arrays.appendElementsOfArray(resultArray, lowerTemporaryArray)

Arrays.appendSingleElement(resultArray, pivot)

Arrays.appendElementsOfArray(resultArray, upperTemporaryArray)

return resultArray

end

It’s an important step forward. I think I’m going to be using it a lot.

The recent series of Why XYZ Is Not My Favourite Programming Language articles has been fun to do, and it’s been great to see the discussion in the comments (even if it’s mostly people people saying that I am talking a load of fetid dingo’s kidneys). But I don’t want to extend that series beyond the point of diminishing returns, and it’s time to think about what it all means. As duwanis commented on the Ruby article, “I’m a bit lost as to the point of these posts”; now I want to try to figure out just what, if anything, I was getting at.

Java has four different “kinds” of types. Up until Tiger, it had these three:

Primitive types: longs, shorts, booleans, ints, outs, whatever. They map more or less to machine types.

Classes, which are the primary mechanism for extending the language.

Arrays, which are this weird hybrid type that was introduced to make it easy to port C and C++ code to Java. But you can’t make arrays immutable, and you can’t subclass them, and there’s only modest syntactic support for them, and reflecting on them is painful.

The problem, of course, with having different types of types is that all of the code you write, now and forever, has to be prepared to deal with each type-type differently. So you get interfaces like this lovely one. Scroll down to the Method Summary, and looky looky, there are 76 actual methods in the class, but only 10 distinct pieces of functionality.

Just to make things worse, Java 5 added a new kind of type, called an enum. They’re sort of like C enumerations, but they can also have methods. Which makes them sort of like Java classes, but you can’t instantiate them, and you can’t have one enum type inherit from another enum type, so you can’t have polymorphic methods, nor can you declare an enumeration heirarchy. So they’re not really like classes at all.