This week I started reviving my stiff Smalltalk skills. What better way than to use a tutorial that just throws you in on a big project? Stephan B Wessels wrote such a tutorial. You won’t want to do it, if you’d never programmed with Smalltalk as it doesn’t explain you how the language works and what standard classes there are, which messages they understand etc. But if you know that already, you’ll be thrown in into an excellent tutorial showing you how to use Squeak and how to design greater projects, using all the important tools Squeak offers.
Now while going through this tutorial I encountered something that caught my attention – an approach to manage your to-dos – not by commenting but by enhancing the Object class. For someone that is used to code in Java this per se sounds like a funny approach. As I worked with Smalltalk already it wasn’t really the fact, that you edit the Object class but rather the editing you did.
If we implement an instance method #revisit: on Object a handy technique is revealed for collecting all your notes.
The #revisit: method doesn’t do anything. But we can pass a String argument and so we leave ourselves a little note. As a convention I usually put my initials and the date when I created the note in my text.
Here’s the code for Object>>revisit:
revisit: aString “Does nothing”
And this is how you use it (line 3!):
anyMethod: someValue “This method does something” self revisit: ‘2010-08-05, pygospa: Still need to find a better name for the method selector’ ^ self doesSomething: someValue
Now the trick is that in any Smalltalk environment there’s a method finder, and you can filter the search by “implementors” as well as “senders”. The later will list up all classes as well as the message send with its arguments, so you’ll see all those little notes you left anywhere in the code.
Now on the one hand I thought: “Wow, this is a neat little hack” – but on the other hand, of course, “it’s a hack” – and I’ll say a hack that my professors would consider quite dirty. I mean, the whole purpose of having classes, objects and methods is to encapsulate data and functions to manipulate this data. The whole purpose of having inheritance is to reuse code, to abstract similar behaviour for generalizations and to specialize it for concrete implementations. Additionally, in Smalltalk every message is returned by an answer – another idea that stresses the philosophy of objects communicating with messages, rather than making system calls.
This beautifully idealized world of object oriented programming is (in my opinion) totally violated by the above explained approach. The method finder is violated to not find methods, but to create to-do lists. The Object class is violated to not pass on behaviour, but passing on a handle with which one can abuse the method finder, the sub classes and the Object class. And the classes violate the method by sending a message that doesn’t do anything, that doesn’t help the program in any way. That doesn’t serve the object oriented approach. A message is the kind request by an object to ask another object to do something. That’s not happening. It doesn’t belong into the code – it’s something for the programmer and therefore it’s a comment, rather than anything else.
Now I wondered what other Smalltalkers think about this approach and first answers where, that there are quite some people who do use this approach, calling the message #revisit #toDo, etc. I even heard of people writing tests that’ll verify that there are no #toDos left, before they close a version. Of course the idea is again quite neat, and I admire the idea.
Now what shocked me most is that I learned that this approach is actually already implemented in Squeak! The superclass of Object is ProtoObject, and if you look carefully into ProtoObject, you’ll find ProtoObject>>flag: aSymbol.
Here’s the code of #flag: aSymbol
flag: aSymbol "Send this message, with a relevant symbol as argument, to flag a message for subsequent retrieval. For example, you might put the following line in a number of messages: self flag: #returnHereUrgently Then, to retrieve all such messages, browse all senders of #returnHereUrgently. " ^ self
So even in Smalltalk itself, the mother of all OO languages and the cleanest OO implementation does use this hack to handle todo lists.
So now I wonder: what do you think about this approach? Do you use it? If so, or if not: why? Do you think this approach is nice and clean, or would you think it’s “cheating”. Shouldn’t an IDE rather provide methods that allow you to generate documentations and todo lists, etc. out of comments? Kind of a “SmalltalkDoc” inspired by the JavaDoc approach?