What Java Owes to Smalltalk
Thursday, August 12th, 2004I have been writing programs in Smalltalk at home for my own edification, while in my regular job I am mostly working in Java nowadays.
When I first started learning Java, I had no idea — no idea — just how much Java owed to Smalltalk! I still didn’t really appreciate it until recently, when I started digging into Smalltalk more seriously. From the notion of everything descending from “Object” to garbage collection to the inheritance model to polymorphism, almost everything Java does is basically aping Smalltalk. Often not very well. I am using Java’s Collections a lot, and even though Collections are a relatively new transplant from Smalltalk, compare this:
for (Iterator iter = myList.iterator(); iter.hasNext(); ) {
myThing thing = (myThing) iter.next();
thing.doSomething();
}
with this:
myList do: [:thing | thing doSomething ]
I’m not just griping about syntax here; which one, conceptually, looks and feels more elegant, more intuitive? Look at all the method calls and casts you have to do just to iterate through a list of arbitrary objects in Java.
That’s the sort of thing I didn’t really notice until I started working with Smalltalk. Now I shake my head and wonder how much better life would be if it had been Smalltalk rather than Sun’s Java that had been poised to catch the wave in the 90s.
Now, some Smalltalk constructs still feel more awkward to me than the Java equivalents. For example:
if (boolValue) {
doThis();
} else {
doThat();
}
feels more intuitive to me than:
boolValue
ifTrue: [ doThis ]
ifFalse: [ doThat ]
But I realize this is a matter of taste, and being more comfortable with familiar syntax.
Frankly, I like Perl’s flexibility even better in this regard:
doThis() if $boolVal;
or
$boolVal or doThat();
Actually, this is all a good argument for programmers to be multi-lingual. I don’t think anyone can be a “master” of any language until they are fluent in many.
Here is a good article on the advantages of dynamic typing. I am becoming convinced that the “advantage” of static typing is that it provides minor babysitting services for bad programmers. Try writing a Java package that performs all sorts of calculations with int, double, float, and long data types and has to go back and forth between them…. the need to cast, convert, and develop special ways of handling “loss of precision” will drive you nuts.