More Java rants

Feb 04, 2005 15:40

In today's edition of the continuing "I hate Java" series, Donna talks about the lack of higher-order functions:

I hate writing the SAME code over and over again.  In the program analysis framework I'm using (Fluid), you can attach an arbitrary (untyped) piece of data to an AST node.  This is convienient.  These data are keyed with a "slot info", and when you create a slot info object, you specify a default value for the slot.  But unless you have immutable values, the only value that makes any sense is "null".  In my case, I'm storing a mutable list, and a specifiying an empty list as the default would result in the same list being returned for all nodes. (Fortunately I realized this before I wrote code to do this.)  What's annoying is that obviously what we want is for the slot info's to take a *function* that will provide the default value. But this is such a pain in the ass to do in Java that of course no one does this sort of thing.  What I'm sick of is time and time again, writing this code:

result = hashtable.get(key); (or "exists" or whatever)
if (result == value doesn't exist)
  add a new item to the hash table
  return this item
else
  return hashtable.get(key)

I'm curious if there's a common idiom for this case in functional programming, or if it's even built-in to the standard libraries?

(Incidentally, I got so sick of how ugly the above code is that in one case I wrote an inner class inside of a function to do get a quick-and-dirty closure).

Note: Please excuse my rants.  But the last time I programmed this much in Java, I didn't know any better and didn't truly understand its mediocrity.  I often had a general sense of "this is kludgy, there has to be a better way!" and I'm very glad I can now articulate the awfulness.  (In fact, sometimes I mistakenly thought it was the nature of the problem that caused the badness!)

My next programming project is going to be in Scala, without a doubt.

rant, java

Previous post Next post
Up