Static vs. Dynamic Philosophy and JDK 1.5 Generics

10 PM May 14, 2003

I was going to stay out of the latest Static vs. Dynamic debate. Really I was. But then I read Joshua Bloch’s JDK1.5 interview on Sun’s Java site. It’s a good article, but one thing made my blood boil.

Static-typing supporters often predict catastrophic runtime errors should someone ever write a program in a language without static typing. This, despite plenty of evidence to the contrary from Ruby, Python and Smalltalk developers.

Well Bloch tells us that our statically-typed Java code is not safe from runtime errors. The problem is type safety. Apparently we need more type safety, and we need it in the form of Java generics1. He is quoted:

The upside [of generics] is that if you try to insert something [into a collection of strings] that’s not a string, you find out at compile time and fix the problem. Without generics, you discover such a bug when your most important customer calls your VP to tell him that the program on which his business depends just crashed with a ClassCastException.

Well, Mr Bloch, practically speaking, you discover such a bug long before your customer does. This isn’t a theoretical position I’ve arrived at as a result of a dynamic-typing-philosophy navel gaze. I say it in light of the millions of hours of generic-less Java coding that developers have already done.

From my own experience, and fromwhat I read on forums, the kinds of runtime exceptions that cause VPs grief are: NullPointerExceptions, slightly and subtly misconfigured J2EE servers, and mismatched versions of serialized classes. Even FileNotFound when looking for a vital .properties files, unexpected SqlExceptions and OutOfMemoryErrors are more common than ClassCastExceptions.

In short, it seems that ClassCastExceptions are already weeded out early on in the development process. We don’t need more static typing for better type safety—there are bigger problems to solve.

I’m going to stop here because, if I went on, I’d say something inflamatory like, “Bloch’s statement is typical of programmers that place unthinking faith in static typing as some kind of bug removal panacea.” Then I’d probably repeat a whole bunch of stuff that Bruce Eckel already said more articulately than I ever could. And no doubt I’d not resist the temptation to stir up dissension between the Java Generics and CORBA camps by asking if, now we have generics, EJB Entity Bean finders are going to continue returning Collections that require us to cast each and every member using PortableRemoteObject.narrow.

Yes, best to stop now.

1 I should point out that I think generics are a good idea. They work in well with the strong-static-typing flavour of Java, they make good documentation, and they get rid of the constant casting around we have to do whenever using collections.

By alang | # | Comments (0)
(Posted to Software Development)

Comments

Add Comment




(Not displayed)






(Leave blank line between paragraphs. URLs converted to links. HTML stripped. Indented source code will be formatted with <pre> tags.)




© 2003-2006 Alan Green