My Stuff
Love
Respect
Admiration
My Amps
Links
Archives


Add this feed to a running copy of BottomFeeder

Linux World 2006 Speaker

Thursday, April 22, 2004

 
Slave To The Lambda

I find myself using more and more block closures in Smalltalk. I attribute it mainly to Lisp. But, I just can't seem to resist using them to make my code more concise. Last night, I was working on the Seaside Presentation and seaside uses blocks in the rendering of html. It makes the html code incredibly small and easy to read. Most of the code that I wind up writing for web applications generally tends to be get very ugly when it gets around the html generation. This was tue especially in the Java world. But, the code I write in Seaside is Smalltalk and the code is generally smaller than the html that I would have generated. Very nice. I attribute most of the elegance and size of the code to blocks. For example, I found myself writing the following code a lot in VisualAge:

| stream |
stream := CfsReadFileStream open: someFileName.
stream isCfsError ifTrue: [Error signal: 'BADNESS'].
["do some stuff on the stream"
] ensure: [stream close].

Well, I decided to move this code to CfsReadFileStream so that now all I have to write is:
CfsReadFileStream open: someFileName do: [:aStream | "do some stuff on the stream"].

and the method looks like this:
CfsReadFileStream>>open: aFileName do: aOneArgBlock
| stream |
stream := CfsReadFileStream open: someFileName.
stream isCfsError ifTrue: [Error signal: 'BADNESS'].
[aOneArgBlock value: stream] ensure: [stream close].

Blocks allow you to put the code that you repeat a lot (which generally just wrap around other code) to places where the responsibility belongs. Now, my code dealing with streams looks a lot cleaner. I noticed that Ruby handles its files in the exact same way and in fact, they take it one step further (the block you pass in gives you each line if you wish).

I will never give up my block closures ever again....=)

Comments




Metalheads Against Racism



This page is powered by Blogger. Isn't yours?