ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


Smalltalk for Everyone Else
Pages: 1, 2, 3

Now use the browser to add a few methods to your CronGen class. First, middle-click (or, on Windows with a default configuration, right-click) in the class category pane of the browser. Select find class... and enter "CronGen" in the resulting dialog. Don't worry about method categories for now. Instead, select -- all -- as the category. The code pane at the bottom of the browser will display a template reminding you of the syntax of the method. Now add accessors for your command instance variable:



command
    ^command

command: cmdString
    command := cmdString

Note that the two new messages both have command as a selector. One is a unary message that simply returns the command instance variable, while the other is a keyword message that accepts a cmdString, which it assigns to the command instance variable. This is a fairly typical syntactical pattern when adding setter/getter methods to a Smalltalk object.

Now create some class-side methods. To do so, select class at the bottom of the class pane. Add the myDayMapInit from earlier in this article. Also add an accessor for the map that treats it as a singleton, initializing it if necessary and returning it:

DayMap
    DayMap ifNil: [ DayMap := self myDayMapInit].
    ^DayMap.

The browser isn't the only development tool in the Squeak image. Evaluate this code in a workspace:

CronGen DayMap inspect.

Inspector Screenshot
Figure 3. Using the Smalltalk inspector

This will open an inspector on the DayMap singleton (Figure 3). In this case, because the map is a dictionary, a DictionaryInspector is opened. Inspectors allow you to examine the values associated with a given instance. The dictionary inspector allows you to look at the member variables of the dictionary DayMap and to look at the elements that it contains.

You can see that the tight integration of the language, the class library, and the development tools facilitate a unique development style. To manipulate and inspect the objects of your application, simply send messages to your object instances. Perhaps the most important of these messages is the halt message. Kent Beck, the father of eXtreme Programming, has reportedly advised "Don't think, just set the halt," allowing you to inspect the live system and fix defects.

The walkback from a halt
Figure 4. The walkback from a halt

Imagine that your application that uses the CronGen class is having a problem where some of the CronGen instances have their command set to a number rather than a string. Edit the command: method to add a line to the beginning of the method:

(cmdString isKindOf: Integer) ifTrue: [ self halt. ].

This will trigger a walkback at the point where the halt is sent (Figure 4). From the walkback, it's possible to drop into a debugger, from which you can inspect the state of your program (Figure 5).

Debugger Screenshot
Figure 5. The Smalltalk debugger in action

To see this in action, evaluate this code in the Workspace, one line at a time, after having made the previous change to your image:

        cg := CronGen new.
        cg command 1.

Squeak also provides extensive facilities for navigating pre-existing code. This helps both with understanding the services available in a base image and to come to an understanding of locally developed legacy code. In a running Squeak image, click on the tab on the right border of the main Squeak window. Drag the icon for Method Finder to the desktop.

Method Browser Screenshot
Figure 6. The Smalltalk method browser

The Method Finder does just that. Type a method-name fragment in the upper right window, and you'll receive a list of candidate method names (Figure 6). Selecting one of those lists the classes that implement the selected method, which you can then browse. The System Browser provides similar services with context menus that allow you to browse senders and implementers of any selected method.

Smalltalk's long history ensures that there is a great quantity of information available to you for further study. The Squeak.org website provides a central point of information about the Squeak implementation of Smalltalk, while Smalltalk.org serves a similar purpose for the language in general. Stéphane Ducasse, a prominent Squeak community member, has provided a great community service by petitioning the rights holders of several out-of-print Smalltalk books to allow him to post online versions of Smalltalk books. The Squeak mailing lists are populated with an extremely friendly, knowledgeable, and helpful community that resemble the language they work with: broad and deep and repaying manyfold the effort you put in.

Keith Fieldhouse is a software developer and writer living in upstate New York with his wife and two young daughters.


Return to ONLamp.com.



Sponsored by: