. .

smalltalk

Can and Should

June 13, 2010 9:56:07.495

One of the things that often ensnares new Smalltalkers (and sometimes keeps them ensnared) is the simple power of the language - the things you can do that are either hard or impossible in whatever languages you used before. For me, when I first started using Smalltalk (many years ago now, I'll admit) it was constructed message sends.

Consider a simple set of messages you might have in a class: #doThis1, #doThis2, #doThis3 (and so on). The idea is simple - you have a set of messages that have almost the same name, but differ in their prefix or postfix. So... the seemingly elegant solution is something like this:


executeMyMethodWith: argument
	| extension |
	extension := argument printString.
	message := ('doThis', extension) asSymbol.
	self perform: message

Seems simple, right? The code is fairly straightforward, and all execution funnels into that one method. Except... when you come back to the code a few months/years later (or worse, a follow on developer does), and find the method #doThis4 - one of the first things you do is check for senders - and you find none.

I've been bitten by that more than once. It's easy to start blowing away the "unneeded" methods to spruce things up, and then have the application die in a stream of MNUs. After a bit of cursing at the debugger, the problem becomes clear, but a lot of time and energy was wasted in the process.

What that gets to is this: just because you can do something in Smalltalk doesn't mean that you should do it. Cleverly constructed message sends are almost always a mistake (it's a nice thing to be able to use when you need it but - there's almost always a simpler solution). Clever handling of #doesNotUnderstand: is another one. Useful for proxy objects, usually just a bomb waiting to go off for anything else. Likewise, message eating nil code. That tends to work fine right up to the point when it doesn't, and then it's nearly impossible to figure out why things are working badly.

Other languages have their can/should not partterns as well, but Smalltalk seems to have a few that really excite people before they get bitten by them. You kind of need to take the Spiderman thing seriously - "With great power comes great responsibility".

posted by James Robertson

 Share Tweet This