Leaky Abstractions and Language Design

ziggy on 2004-10-25T13:50:06

I stumbled across this page on the c2 wiki today. It's a discussion about why this:

foreach my $item (@list) {
	...do something here...
}
is less desiarable to this smalltalk/rubyish construct:
@list.foreach {|item| 
	...do something here...
}
In Perl (and Ruby), the syntactic difference is subtle. In Java, the syntactic difference is larger. (See the Wiki discussion for an example.) The main difference is that throwing a closure at a foreach method on a collection allows you to use a named function just as easily -- instant reuse! So stop copying the body of a for loop over and over again (or just as bad, write it from scratch every time you need it) and start composing code through combinations of higher order operations.

All of this is well and good in the abstract. But at some point, this code has to execute. It's nice to talk about ivory tower constructs that are platonically "better" than the brute force coding. How does it all work in practice?

Ward Cunningham has some insight as to why Java programmers are so hesitant to adopt these Smalltalk memes:

I read in a recent performance tuning book that inner classes hava a ram footprint of about 3K each before they are even instantiated. Compare this to two or three bytes per (latent) block in Smalltalk and you get a feeling for why each language has its own conventions. -- WardCunningham
Hm. I smell a leaky abstraction.


Damn you real world.

schwern on 2004-10-25T19:48:32

In Perl function calls are expensive, so we don't slice things as finely as we really should some times.

In Java they made the poor decision early on to not make the basic data types real objects. And then there's that whole "Final" thing. And now this.

Smalltalk... they seem to have unabashadly thrown their faith in Moore's law and advanced compiler design. Although I've been flipping through a Smalltalk 80 book and remember they did some interesting hack with small integers where the first 256 pointers were all allocated to the numbers -127 to 128 so you could represent small ints as objects without extra memory. And that there was a limit of 32K objects because pointers were 16bits long. But that's all internal stuff and hopefully its gone now.