Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Mis)Understanding Smalltalk and TDD

I'm trying to learn Smalltalk by doing, so I'm getting a grip on the syntax and style by buiding a simple "Matrix" class.

First of all, I'd be grateful if linked to a good Smalltak tutorial (although this is totally optional), preferably one that doesn't involve using the GUIs (I'd rather type my .sts than fish around the hierarchy explorer to put the methods in their place).

Then, on TDD: For this project I'm calling gst-sunit -f matrix.st -f matrix-test.st BaseMatrixTests, and there's bound to be a better way than that. Is there?

And finally, on asserts: I'm trying to write a method and put asserts within, eg.:

Matrix>>multiplyBy: anotherMatrix [
    [ self isNotEmpty ] assert.
    "Do Multiplication"
    [ result dimensions = (self height)@(anotherMatrix width) ] assert.
]

How can I do that kind of asserts?

Edit: Questions marked explicitly.

like image 455
Tordek Avatar asked Mar 22 '09 00:03

Tordek


1 Answers

Okay, several pieces here.

First, I agree with markusQ, although I completely sympathize: I'd rather be able to write my code in EMACS directly. Bt one of the things about Smalltalk is that it really is very unforgiving of people who don't want to do things the Smalltalk Way. In this case, the Smalltalk Way is to use the browsers.

Second, part of the reason that this is the Smalltalk Way is that Smalltalk is, in a lot of ways, not like other languages. There is really, for all practical purposes, no way to make a "separate" Smalltalk executable: all you can do is make an image of Smalltalk with some relatively small fragments of your own code added in. When you write code using an external editor, as with the syntax you show, you're literally just hand typing an import/export format that is somewhat easier to hand type than XML. But only somewhat.

The moral is, do it the Smalltalk way, with the browsers.

There are some fairly good tutorials for Smalltalk about. I usually use Squeak, so the ones I've seen are using Squeak, as here.

On TDD, you're in luck because Smalltalk was the first place to get xUnit. For Smalltalk, it's called SUnit, and there's a good tutorial here.

You're using assertions there in what appears to be basically a "design by contract" approach. There has been work done on adding design by contract to Smalltalk, as here. For simple assertions, you can add code as in this SO question.

assert: aBlock 
    "Throw an assertion error if aBlock does not evaluates to true."
    aBlock value
        ifFalse: [AssertionFailure signal: 'Assertion failed']
like image 98
Charlie Martin Avatar answered Nov 09 '22 00:11

Charlie Martin