Monday, August 01, 2005

Java Generics

Well, I got all of us in the office moved up to Eclipse 3.1, and have been fairly happy with it so far: a bit faster to start up, filters on the properties pages, etc. And support for JDK 5.0.

I've been interested in playing with generics since I heard Java was getting them. I had messed around with C++ generics, and liked them there, so once I got Eclipse running, I went to work correcting the type warnings that popped up in all of the Iterators and HashMaps we're using.

So I fixed everything in three of our smaller projects, and noticed that the remaining two had 1350+ warnings remaining. Hoo boy, what to do. I mentioned it to Boss, who leaned toward letting our junior guy, Junior, fix them at his leisure. So far, so good.

Then I looked at the code again. It was ugly. < and > everywhere, hideously long names, and after wandering through a few dozen source files, I had found nothing that made me think "hey, glad I put that there, or I wouldn't have found *THAT* bug". So I started thinking about ways to make it cleaner.

I Googled, and found a mention of subclassing, i.e. HashMap, which worked fine (we use HashMap<String,String> a *LOT*), and the code became a lot less ugly, and made further changes a lot easier to make (I called the new class HashMapSS; pretty easy change to that from HashMap). I played with that a bit, then took another look.

I got down to about 1300 warnings, and asked myself: What does it buy us to fix all of these? Initially, I was telling Junior and two other coworkers about generics, and just got blank stares. "Increase coding time and make the code ugly? For what?" With these new classes, we're just down to a minor increase in coding time, but again, for what?

In our case, we're not running into problems that adding these checks would have caught earlier. ClassCastExceptions and the like. As I'm the office code Nazi, I'm the one who would be most eager to code this way, and I'm sitting on the fence. Everyone else has turned off the warnings and continued merrily along.

I'll probably continue to code with the warnings on, and see how it goes, just because, but I'm not as excited about generics as I had expected to be. Perhaps the Python I'm doing in my spare time is rubbing off. Throw a few test cases at it, and all this static typing is more of a burden than an assist.

0 Comments:

Post a Comment

<< Home