. .

st4u

ST 4U 401: Bad Choices with DNU

June 7, 2013 11:33:14.757

Today's Smalltalk 4 You looks at how you can get into trouble with message construction and #doesNotUnderstand:. If you have trouble viewing it here in the browser, you can also navigate directly to YouTube. To watch now, click on the image below:

DNU and perform.

If you have trouble viewing that directly, you can click here to download the video directly. If you need the video in a Windows Media format, then download that here.

You can also watch it on YouTube:


Today we'll take a look at a common pattern in Smalltalk development that can get you into real trouble - overriding #doesNotUnderstand: to construct messages and send them via #perform: - like this:


doesNotUnderstand: aMessage
	| string |
	
	string := aMessage selector asString.
	self doOp: string

The problems with this are twofold - first, your API is effectively hidden - using standard senders/implentors searches, you don't find it. For follow on developers, it's as if your API doesn't exist. Second, you'll probably end up implementing something like this:


doOp: aString
	|msg |

	msg := (aString, ':') asSymbol.
	(self class allSelectors includes: msg)
		ifFalse: [^self].
	self perform: msg with: 1

Which swallows your exceptions. This kind of code seems very cool the first time you write it, but do yourself a favor - avoid it. There are uses for DNU (mostly when creating proxy objects) - but you should think carefully before using this kind of pattern

Need more help? There's a screencast for other topics like this which you may want to watch. Questions? Try the "Chat with James" Google gadget over in the sidebar.

Tags: ,

Enclosures:
[st4u401-iPhone.m4v ( Size: 2526268 )]

posted by James Robertson

 Share Tweet This