Online Book Reader

Home Category

Beyond Java - Bruce Tate [37]

By Root 656 0
but Java developers use overloading to enable an API that supports multiple types. You've got a surefire recipe for API bloat.

Need an example? Take the java.util.Array interface. Please. For convenience, you get more than 70 methods. Peel back the onion, and you see they cover only 10 or so pieces of actual, distinct functionality. With a smarter method declaration, you'd be able to specify parameters with keywords, and default unused parameters to an intelligent value, like 0 or null.

Other Costs


When you decide to type everything, it's a slippery slope. When you need to pull back from Java's typing system, you can't always do so. You're starting to see many examples of Java libraries working around the typing in unusual ways. Study the JMX interface for an excellent example. Does it use strong typing? It appears that way, at first. Then you dig in a little and find what only can be conceptually described as an embedded type system—a mini-language, embedded in a String parameter called ObjectID, with a complete language description in the JavaDoc and syntax completely opaque to compilers and interface generators and processors. Java's type system failed here. JMX architects bypassed the type system, building metadata into strings and other objects. If you look around, you'll find other examples of this as well. Most often, Java hides weaker types, or dynamic types, as strings.

The Benefits of Static Typing


After reading about all of the negatives, you're probably wondering why anyone would ever opt for strong, static typing. There are at least two compelling reasons to do so. Static typing reduces certain types of errors (like misspelled variable names), and provides more information for your IDE and other tools. (Most security-related typing arguments refer to weak typing, not dynamic typing.)

Take the following application. Java will catch this error at compile time:

int consumer;

if (conusmer = = 0) return consumer; //spelling error

It's hard to imagine a dynamic language, with rigorous unit testing, letting an error like this through, though. The IDE problem is a little bit more obscure. Many of the features that Java developers have come to depend on, like method completion, rely on information in a variable's type. You can't always get the same contextual information out of a Ruby or Smalltalk IDE.

A Safety Net with Holes


The Java founders most often cite the ability to catch type mismatch errors at compile time rather than runtime. That's interesting to me, because of all the Smalltalk and Ruby developers I interviewed, few have ever had significant problems with type mismatch errors. Of course, most of them lean pretty heavily on automated unit testing, as we all should. You need to unit test code regardless of whether you use dynamic typing. No compiler can guess your intent perfectly. Even if you like the generics implementation, you've got to be concerned with an implementation that's little more than syntactic sugar, with no JVM implementation behind it.

With the heavy use of test-driven development, the argument for reduced bugs is much less compelling. In fact, Java's type safety is not as encompassing as the founders would lead you to believe. At any given time, most of the objects in a typical Java application reside in collections. Any time you remove one of these objects from its collection, you need to cast up from Object. You're effectively retyping an object. If you cast it incorrectly, glass will break in the form of a class cast exception, at runtime. At the same time, improved tools and emphasis on automated unit testing make it much easier to catch type problems in dynamic languages long before they ever reach production. My experience tells me that Java's type safety is not as important and comprehensive as most programmers think it is, and the typing in more dynamic languages, with unit testing, is not as limiting.

The IDE code completion problems presented by dynamic typing will probably get solved by a combination of better browsers and smarter context. Unit

Return Main Page Previous Page Next Page

®Online Book Reader