Tuesday, March 3, 2009

Smalltalk: power of simplicity and personal mastery

One of the principles stated in Design Principles Behind Smalltalk by Daniel H. H. Ingalls which appeared in the BYTE Magazine, August 1981 was that the Smalltalk project had a bias towards:
Personal Mastery: If a system is to serve the creative spirit, it must be entirely comprehensible to a single individual.

Well my Smalltalk is not great, I have read some of the free books on Stephane Ducasse's website, and yet I still find Smalltalk to be intuitive and easy to do things with. I'm not sure if it was the way the language was designed, the class design (libraries) or a combination of the 2.

Let me give a simple example, but first a disclaimer; I could have used excel or some other mechanism to do this but it was more of a learning experience.
Ok so basically I had 2 CSV reports that i had run on 2 environments that I wanted to compare to make sure that there were no differences. Thats really simple and I didn't explore many options, I just fired up Pharo and started typing into a workspace:

firstReportList:= (CSVParser parse: (FileDirectory default readOnlyFileNamed: 'resources\first.csv')).
firstReportList := firstReportList asOrderedCollection.
firstReportList allButFirstDo: [:entry | entry at: 5 put: nil.].

secondReportList := (CSVParser parse: (FileDirectory default readOnlyFileNamed: 'resources\second.csv')).
secondReportList :=secondReportList asOrderedCollection.
secondReportList allButFirstDo: [:entry | entry at: 5 put: nil.].

Transcript clear.
Transcript nextPutAll: (liveList = wareList) asString; cr; flush.
Transcript nextPutAll: firstReportList size asString; cr; flush.
Transcript nextPutAll: secondReportList size asString; cr; flush.


CSVParser was a small nifty library by Avi Bryant that I had previously loaded in my image. The line firstReportList allButFirstDo: [:entry | entry at: 5 put: nil.]. was put in to ignore the heading on the first line and then for all the other rows blank out (by making it nil) a column that was deliberately different in the 2 files with which I did not want to compare.

Now firstly how sweet is that!
Secondly I didn't get any differences so I was happy with the code i.e. I didn't have to write code to spit out the differences although I feel that would have been trivial.
Thirdly try doing that in my staple language Java...umm no thanks...I know its possible but not as succintly as Smalltalk. I'm sure Lisp or Perl would have a nifty way of doing this, and probably Smalltalks distant cousins offspring Ruby.

I can't wait for what Daniel Ingalls ended the article with (I think many people want to be part of this too :) and its not Ruby )
Natural Selection: Languages and systems that are of sound design will persist, to be supplanted only by better ones.


On another language note I see the Lift web framework written in Scala was released, I took a very brief look at some of their examples and I was not impressed with the mental hoops it took to make it do something simple. Seaside seems simpler to grasp and as expressive if not more....but I'd still need to look at it in more detail.

No comments: