Tutorial :(Mis)Understanding Smalltalk and TDD



Question:

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.


Solution:1

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']  


Solution:2

I hate to say it ('cause you clearly indicated you don't want to hear it), but get into the IDE. Trying to get your head around smalltalk without using the IDE is like going to Paris and eating at McDonalds. Sure, you're in Paris, but you aren't really exposing yourself to what it's all about.

The key thing about smalltalk is that it's all objects. All the way down (integers and characters are objects) and all the way up (classes, methods, the browsers, the IDE itself) are all objects. If you insist on fighting it you'll have about as much luck as someone wanting to learn how to write C by dragging and dropping things.


Solution:3

If you downloaded Cincom Smalltalk Non-Commercial, there are a number of online tutorials. Start here:

http://www.cincomsmalltalk.com/userblogs/cincom/blogView?content=tutorials

If you downloaded Squeak, start here:

http://wiki.squeak.org/squeak/792

And yes, you really do need to use the IDE to work effectively with Smalltalk.

On testing, load SUnit. In Cincom Smalltalk, it's a loadable component; I've covered loading (and using) it in the video tutorials linked above. I'm not entirely sure how to load it for Squeak, or whether it's part of the base there, but it's certainly available for it.


Solution:4

Other than a request for a tutorial, I don't see a question here. Could you clarify what it is you want to know?

A good list of resources for a beginner can be found in this question: https://stackoverflow.com/questions/481976/is-there-a-dive-into-smalltalk-book


Solution:5

So, about asserts, Squeak Smalltalk already brings Object>>assert: So, I suppose you can do:

self assert: self isNotEmpty.  self assert: result dimensions equal: (self height)@(anotherMatrix width)  

If you are using GNU smalltalk, this might answer how to do assertions there: Smalltalk and Assertions

Niko


Solution:6

Regarding asserts, please look at the other question recently posted.

Regarding TDD, yes, calling gst-sunit -f matrix.st -f matrix-test.st BaseMatrixTests is sort of the best way. Everything else just builds on that, for example these could be thee alternatives:

  • make all TestCase subclasses for your package inherit from a phony subclass so that you can say AllMatrixTests* on gst-sunit's command line (when you add more tests).
  • file in matrix.st from matrix-test.st, thus eliminating one -f option.
  • create a Makefile and package.xml file to create a .star file for your package, as described here. Then you can do just gst-sunit -pMatrix.


Solution:7

It has been suggested above to add #assert: to Object, but rather I'd add #assert to BlockClosure (or whatever [] class is in GNU Smalltalk).

assert      this value ifFalse: [AssertionFailure signal: 'Assertion failed']  

and thus use as in

[ value notNil ] assert.  [ value > 0 ] assert.  [ list isEmpty not ] assert.  

etcetera.


Solution:8

http://stores.lulu.com/store.php?fAcctID=2748699 (Also you will find the link to download the pdf)


Solution:9

I'd rather type my .sts than fish around the hierarchy explorer to put the methods in their place).

You say this now and I felt the same way until I invested some time into learning how to use these tools.

And I say this as a current user of Vim and former user of Emacs. I really have become quite productive using the class browser and other tools in Pharo now that I've learned the ropes.

That being said, if you really want to go your own way, GNU Smalltalk is the right choice for you.

I would like to point out at this point though that at least Pharo has a Matrix class which you can use for inspiration. Squeak should, too. Perhaps you will learn how useful the class browser is examining Matrix. ;)


Solution:10

Smalltalk Primer is an introduction to The Essentials of Message-Oriented Programming with Smalltalk


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »