The introduction to On To Smalltalk follows. Additional
information about this book, along with access to software, is available
via http://www.ai.mit.edu/people/phw/Books/
How On To Smalltalk Teaches You Smalltalk
The purpose of this book is to help you learn the
essentials of Smalltalk programming. In this section, you learn why you
should learn Smalltalk and how this book is organized.
Object-oriented programming languages, such as Smalltalk, encourage
you to design programs around classes, such as the Integer
class, along with classes that you define yourself. Typically, you define
classes and class hierarchies that reflect important general properties of
individual nails, horseshoes, horses, kingdoms, or whatever else happens to
come up naturally in your application.
In the vernacular of Smalltalk programming, messages are sent to
receivers, and receivers return answers. To do the necessary
computation, Smalltalk finds the appropriate method, given the
receiver and the message, and that method specifies exactly how the
computation is to be performed.
For example, Smalltalk allows you to send a message, sqrt, to a
receiver, 2, which is a member of the Integer class. To
handle the message, Smalltalk finds the appropriate way to compute the
required square root for receivers belonging to the Integer class;
then, Smalltalk produces the answer, 1.41421, which is an instance
of the Float class.
Visually, the receiver has center stage; 2 is a receiver that knows
how to find a method that can deal with the sqrt message.
Receiver
Message |
| v
v *-------*
sqrt ------> | |
| 2 |
1.41421 <--- | |
^ *-------*
|
Answer
In contrast, procedure-oriented programming languages, such as C,
encourage you to think in terms of functions and
procedures, instead of in terms of classes and class
hierarchies.
Thus, to compute the square root of 2 using a procedure-oriented
programming language, you must identify the appropriate function,
which, when applied to 2---a datum of the integer data
type---answers 1.41421, the function's value. Visually, the
function has center stage; 2 is just the function's argument:
Data-type specific function
|
Argument | Value returned
| v |
*-* *---------------------* |
v | | v
2 -------> | integer-square-root |-------> 1.41421
| |
*---------------------*
In this book, you learn what object-oriented means and why many
programmers prefer object-oriented languages. For now, it suffices to know
that Smalltalk is an object-oriented programming language, whereas most
programs have been written by programmers using procedure-oriented
programming languages.
Among the object-oriented programming languages, Smalltalk has attractive
characteristics, such as the following:
- Smalltalk is object-oriented from the ground up, in contrast to other
popular object-oriented languages, such as C++, which programmers
created by pasting a veneer onto a preexisting language.
- Smalltalk programming is done in powerful development
environments, which enable efficient program writing, editing, testing,
debugging, and maintenance. Smalltalk development environments
provide myriad built-in classes and methods.
- Smalltalk development environments encourage the development of
gorgeous graphical user interfaces, liberally decorated with fancy
graphical elements.
You learn that Smalltalk is said to be a pure object-oriented language
because every entity in Smalltalk is an object and every object can receive
and send messages. Being a pure object-oriented language is one of
Smalltalk's distinguishing strengths.
The enormous power of Smalltalk development environments is derived from
an unusual armamentarium of powerful tools:
- Smalltalk development environments provide powerful browsers
that allow you to examine all class and method definitions---even the
built-in classes and methods.
- Smalltalk development environments provide powerful debugging
tools. Using these tools, you see the message sequences that lead to
program interruption, you inspect parameter and variable values to zero
in on errors, you modify method definitions, and you restart or resume
your interrupted program.
- Smalltalk classes and methods are compiled as you write them,
incrementally. You never have to waste time waiting for a compiler to
translate an entire program or program module into machine instructions.
Also, Smalltalk provides for automatic memory recycling. When you use a
language such as C++, you have to remember to free the memory allocated
to program elements, such as class instances, once you are finished with
them. Failing to free memory produces a memory leak that may
exhaust all the memory available to your program, leading either to erratic
behavior or to a total crash.
Smalltalk frees memory automatically, by performing automatic
garbage collection, so you never need to worry about memory leaks, or to
waste time looking for one. Thus, you are more productive, and are less likely
to be driven crazy by doing tedious, mind-numbing debugging.
There are two principal reasons why you should learn Smalltalk:
- Smalltalk's inherent characteristics ensure that your productivity
using Smalltalk is likely to exceed your productivity in other
programming languages.
- The supply of powerful Smalltalk software modules, provided by
Smalltalk development environments, enables you to develop
applications principally by gluing together existing software, rather
than by writing new software from scratch.
Capabilities of a commercial Smalltalk, VisualWorks, are also explained.
Four principles determined this introductory book's organization and style:
- The book should get you up and running in the language quickly.
- The book should answer your basic questions explicitly.
- The book should encourage you to develop a personal library of
solutions to standard programming problems.
- The book should deepen your understanding of the art of good
programming practice.
To get you up and running in Smalltalk quickly, the sections in this book
generally supply you with the most useful approach to each programming
need, be it to display characters on your screen, to define a new method,
or to read information from a file.
To answer your basic questions explicitly, this book is divided into section
that generally focus on one issue, which is plainly announced in the title
of the section. Accordingly, you see titles such as the following:
- How to Write Arithmetic Expressions
- How to Define Simple Methods
- How to Create Classes and Instances
- How to Benefit from Data Abstraction
- How to Design Classes and Class Hierarchies
To encourage you to develop a personal library of solutions to standard
programming problems, this book introduces many useful,
productivity-increasing, general-purpose, templatelike patterns---sometimes
called idioms by experienced programmers---that you can fill in to
achieve particular-purpose goals.
Idioms are introduced because learning to program involves more than
learning to use programming-language primitives, just as learning to
speak a human language involves more than learning to use vocabulary words.
To deepen your understanding of the art of good programming
practice, this book emphasizes the value of such ideas as data
abstraction and procedure abstraction, along with
principles such as
the explicit-representation principle,
the modularity principle,
the no-duplication principle,
the look-it-up principle,
and the is-a versus has-a principle.
In this book, single-idea segments, analogous to slides, are arranged in
sections that are analogous to slide shows. The segments come in several
varieties: basic segments explain essential ideas; sidetrip
segments introduce interesting, but skippable, ideas; practice
segments provide opportunities to experiment with new ideas; and
highlights segments summarize important points.
Finally, the book develops simple, yet realistic, Smalltalk programs,
which you see in many versions as your understanding of the language
increases. In its ultimate version, one of the programs rates foods
according to their protein, carbohydrate, and fat content.
Highlights
- Smalltalk is an object-oriented programming language. Object-oriented
programming languages emphasize user-defined classes and class
hierarchies.
- In Smalltalk, receivers perform computations in response to messages,
and return answers. Methods specify exactly how the computations are to
be performed.
- Smalltalk is popular because it is an object-oriented language from
the ground up, because Smalltalk development is done in a powerful
development environment, and because Smalltalk facilitates the
development of graphical user interfaces.
- This book gets you up and running in Smalltalk quickly; it answers
your basic questions explicitly; it equips you with program patterns
that you can adapt to your own purposes; and it deepens your
understanding of the art of good programming practice.