User's Guide

Writing simple scripts

This is the basic structure of a script:

scriptName
    " a comment "
    | temporary variables |
    statements

Naming conventions

All names in scripts are case-sensitive. Script names should begin with a lowercase letter because that is the convention that most Smalltalk programmers follow. Names can be any length and should be as descriptive as possible. Names are often made up of several words written together. By convention, the initial letter of each word, except the first word, is capitalized. This improves readability. The names of variables also follow this convention. Here are some examples:

subtractDays
calculateOvertimePay
currentRow
aString

Comments

Comments are enclosed in double quotation marks (" ") and can be placed anywhere in a script. Scripts usually begin with a comment that describes what the script does just after the selector name.

Tip icon
It's a good idea to practice the examples in the following sections as you read them. You can use the System Transcript to practice.

To display the results of any of the code fragments in this section, simply type it into the System Transcript, select all of the text you typed, and then select Display from the System Transcript's pop-up menu. This runs the selected code and displays the resulting object.


Transcript result

Declaring temporary variables

With VisualAge scripts, unlike many traditional languages, you do not have to worry about data types when you declare variables. Temporary variables are declared simply by listing their names inside vertical bars (|) near the beginning of a script, before any statements. When the script ends, temporary variables are lost.

In the following example, aNumber and aString are temporary variables:

doSomething
    | aNumber aString |
    aNumber := 5 * 10.
    aString := 'abc'.

Statements

The statements in a script are simply a series of message expressions. Message expressions can also include assignment statements, because messages always answer a result object.

Separate the statements of a script with a period (.).

To end a script and return its answer, use a caret (^) at the beginning of a message expression. If you do not explicitly return an answer, the receiver of the message is returned instead.

The following script takes two arguments, rate and hours, and returns pay.

calculatePay: rate hours: hours
    | pay |
    pay := hours * rate.
    ^pay

Reserved words

The following words have special meaning:

self
An object uses this name to send a message to itself

super
An object uses this name to access a script further up the inheritance hierarchy. If you have a script with the same name in both your class and superclass, and the script in the superclass is the one you want to run, use super

nil
Refers to an undefined object

true
Boolean value representing logical truth

false
Boolean value representing logical false


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]