The title is a reference to "The Princess Bride", and the phrase I wanted was "I don't think that word means what you think it means". I'm taking that from Neal Ford's rather confused post about meta-programming. His example is code generation, and he has a fairly deep misunderstanding of how Smalltalk works (and, quite probably, of how Ruby works as well) - let's have a look. He goes into an implementation of has_many in Ruby on Rails:
class Order < ActiveRecord::Base
has_many :lineitems
end
For those not familiar with Ruby, this is a method, defined as a class-level initializer (just like an instance code block in Java, a chunk of curly-brace code in the middle of a class definition, which Java picks up and executes as the class is instantiated). So, ultimately, this is the Ruby equivalent of a static method call, which gets called as the class is created.
He then goes into Smalltalk:
Let's talk about Smalltalk, which has first-class meta-programming. You could easily build has_many in Smalltalk, implemented as a button you click in the browser which launches a dialog with properties that allow you to set all the characteristics embodied in the Ruby version. When you are done with the dialog, it would go do exactly what Ruby does in Rails: generate a bunch of methods, add them to the class (stuff like the find_* and count_* methods). When you are done, all the methods would be there, as instance methods of your class.
OK, so at this point, the behavior is the same in Smalltalk as in Rails. But there is one key difference: The Smalltalk version using code generation. It's a sophisticated version of a code wizard, generating the code using meta-programming techniques. The Ruby version uses code synthesis: it generates the code at runtime, not build time. Building stuff at runtime means more flexibility. But that is a minor point compared to this one: In the Smalltalk version, you use the dialog and properties to generate all the methods you need.
I suppose a Smalltalker could do it that way, but it's not how it would normally be done. I've seen a lot of projects that do the kind of dynamic, runtime code generation he's talking about - but let me quote Alan Knight, who was talking about this in the IRC channel this morning:
aknight notes, for jarober's edification, that the GlorpActiveRecord stuff does have a hasMany method
and does no code generation at all from it
and yet, somehow it works
In the Glorp implementation of ActiveRecord (which will be part of our Seaside support, by the way), Glorp reads the database schema, figures out what's there, and then sets up the ability to read the appropriate database records without code generation.
I think a lot of people have strange ideas about how Smalltalk works, simply because the image throws them for a loop. Just because the image allows for some nifty development tools doesn't mean that everything done there is utterly alien.
Technorati Tags:
ruby, meta-programming