Java The Unfriendly...

I must preface this with saying that I don’t do much Java work. Perhaps it’s more friendly for a different domain of work, but at least for me I find that it’s overly complicated to do simple things. For instance, checking the existence of a file looks like this:

if ((new File("filename")).exists()) {
  // Do something
} else {
  // Do something else
}

Why do I need to spend the time creating an object just to throw it away? And that’s not even my real problem. The real problem is that it’s hard to read. To make it a little easier on the eyes, you can do this:

File f = new File("filename");
if (f.exists()) {
  // Do something
} else {
  // Do something else
}

But now you have a couple other issues. First, I’m going to have a variable for something I’ve used exactly once. Second, if this function is long-running, then f will be around for a long time (unless compilers can optimize that out… that’s not been my experience though).

It all boils down to this: it’s too much work to do seemingly simple things. Moreover, absolutely everything does not need to be an object. There is no reason that you couldn’t offer a class method called exists() that takes a string and does the dance for you. At the very least, it would be easier to read:

if (File.exists("filename")) {  // Warning this won't actually work.
  // Do something
} else {
  // Do something else
}

The other difficulty is something rather new to the language: generics. Now I have a pretty extensive background in C++. I spent years coding in it until I met Python so I have a built-in mental model of how I think Generics should work. I’m using generics in a project at work, and what I find is that you can really tell that they were an after-thought. Joshua Bloch and Bill Pugh did a Google TechTalk that highlighted some of the more problematic issues with generics, especially in regards to autoboxing:

</param></param></embed>

I’ve run into an issue recently with generics that I’m unable to make warnings go away for… which is highly irritating. For instance, I wanted to create a map that was keyed by Strings and stored a List of an interface. For instance:

Map<string, List<MyInterface>> map = 
  Collections.checkedMap(new HashMap<String, List<MyInterface>>(),
                         String, List<MyInterface>.class);

Actually, I tried the above knowing it wouldn’t work. The problem is that you cannot take .class of a generic (List in this case). Generics are a runtime entity, not a compile time one. As a result, it's impossible to do the above. Therefore, you can't get the benefit of having a checkedMap. That seems pretty drastic to me. Drastic enough that they should have really consider building generics differently. Unfortunately, Java has been around for a while, so it probably wasn't feasible to do this any other way. What a shame.

A fellow Subversionite, Blair Zajac pointed me to Scala. It’s unfortunate I can’t really use it in my current project, but it’s definitely more friendly than Java is today.