You are currently browsing the monthly archive for April 2007.

I bash Java. A lot. So now, I’m going to take a bit of time to mention something positive:

Java generics combined with the enhanced for loop. It makes me feel like I’m working with the C++ Standard Template Library, and that’s a happy feeling.

Some people don’t like generics. Ken Arnold considers them “harmful” (seriously, that phrase should not be used anymore). I suppose Java was originally designed for blubs like Ken. The C++ community has been working with the same notion for significantly longer (where do you think the syntax came from?), and I don’t quite see anyone complaining about the STL‘s complexity. Rather, it’s a very simple and elegant way of abstracting basic algorithms.

Some concrete reasons in favor of using generics:

  • Type checking is pushed to compile time. The more checks that can be done at compile-time, the less risky a piece of software is. Generics allow a function to be provably type-safe by removing all casts.
  • They provide executable documentation, which is the best kind. I just converted some of my own code to use generics, and I found that I was already describing the data structures in comments anyways: Map matrix; // map(int -> map(int -> int)). Converting to Map<Integer, Map<Integer, Integer>> matrix means that the type documentation will never go out of date, as it is required for compilation. If I changed to a matrix of floats or longs, my comment would have probably fallen by the wayside, and wrong documentatio is arguably worse than no documentation.
  • They reduce visual complexity. There is a bit of duplication when declaring a new object: Map<Integer, Map<Integer, Integer>> matrix = new HashMap<Integer, Map<Integer, Integer>>(); That occurs much less frequently than data structure traversal, which (with generics) now has the advantage of avoiding typecasting. It’s not bad to see a bit of verbosity at the top of a function where my mind’s not invested in something, but I really hate trying to wade through a long algorithm and seeing language boilerplate clutter up my screen. Generics also go hand-in-hand with easy-reading iterables:
    for (Integer row: matrix.keySet()) {
    for (Integer col: matrix.get(row).keySet()) {

    This is on par with iterators from other imperative languages (C++, Python, and PHP, though C++ is the only one to standardize on ranges for iteration).

…is that it takes so long to type things! I’ve always thought Hungarian Notation was a foolish thing for classes and interfaces (e.g. CRotationMatrix and IAffineTransformation), but now I’ve decided why.

  • Anyone who uses a code-completing IDE should be able to type in any class or interface name and get at least a hint about it’s type. This is particularly relevant to Java, which appears to be a “tools-required language” by design with its verbose but easy-to-parse style.
  • Anyone who uses a “man’s man” editor like vim/emacs/editplus should at least have access to some basic text-searching utilities. Something like `grep 'class TheClassName' path/to/repo -R` or (in PHP-land) surfing to is typically sufficient.
  • Most code I’ve come across in dynamically-typed languages like PHP or Python doesn’t require a distinction between “implementing an interface” and “extending an implementation”. I know PHP allows the abstract class keyword, and I can’t really say that I like it.

So who really wants to see the C or I in front of their class/interface name? I dunno, but it’s not me. I don’t buy the argument of “it makes code more self-evident/documenting”, because jumping to a definition and seeing

interface AffineTransformation ...
class RotationMatrix ...

is just as readable (moreso I think because of the conciseness) as

interface IAffineTransformation ...
class CRotationMatrix ...

I think I would shoot myself in the face if I had to program with CRotationMatrixAffineTransformation.

Module Names

Module names are another gotcha that I don’t like. Now, I’m not a regular Java programmer, so tell me if there’s some awesome reason that I’m overlooking.

I’m currently working on some Lucene and Nutch code. They are (respectively) defined in modules org.apache.lucene and org.apache.nutch, and when I play in an 80-character-wide ssh account, the pathnames tend to wrap around my screen unless I start symlinking everything.

It’s all nice and great to know that my particular version of Lucene came from org.apache and not, but I think I’d much rather just use the module lucene without organizational domains getting involved.

  • If a codebase is transferred between organizations, they eather have to deal with the headache of renaming everything and breaking backwards-compatibility, or keeping names as-is and just knowing that the “current controlling organization” is wrong. What if I downloaded something from Apache’s website that still referenced modules (maybe originally developed by IBM and then donated). The license is in a big JavaDoc at the top of every page, I don’t need to see it referenced everytime I need to import a module.
  • it adds unnecessary levels. Am I really worried about multiple groups creating lucene modules and having to deal with both of them? Not really, because of the concept of trademarks. If I create a software product called Vista, you can bet Microsoft will have something to say about it. It doesn’t matter if I’m (which is bad anyways, because I could change hosts anytime) or just vista. If I use the same name as some other product, people will be confused, and organizational namespaces won’t do a hoot about that.

Ok, I’ve bashed Java enough tonight. I really should find more languages to dislike, or more good things about the Java language. I don’t necessarily want Java to change… I just want other things to replace it. Like some ultra-sweet combination of Python and C++.

Edit: I’ve tagged this in Usability because I certainly consider a programming language to be a user interface the same way I consider a software developer to be a digital content creator.