Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Smalltalk

Introduction to the Smalltalk Programming Language

4.70/5 (37 votes)
21 Aug 2019CPOL11 min read 102.4K  
A tip for learning object-oriented programming using a language best-suited for this purpose

What is Smalltalk?

Smalltalk is an object-oriented programming language with a rich history and a storied legacy. It was born at Xerox PARC in the 1970s, created by the brilliant and visionary team of Alan Kay, Dan Ingalls, and Adele Goldberg. Smalltalk was created to investigate teaching programming to children. Understandably, it's a very small and simple language, the simplest of the major programming languages.

To understand the essence of Smalltalk’s philosophy, watch this video clip of Alan Kay’s tribute to Ted Nelson. Some more insight comes from Alan Kay’s “The Early History Of Smalltalk” (©1993 ACM):

Quote:

Smalltalk is a recursion on the notion of computer itself. Instead of dividing “computer stuff” into things each less strong than the whole — like data structures, procedures, and functions which are the usual paraphernalia of programming languages — each Smalltalk object is a recursion on the entire possibilities of the computer. Thus its semantics are a bit like having thousands and thousands of computers all hooked together by a very fast network.

and

Quote:

Smalltalk’s contribution is a new design paradigm — which I called object-oriented — for attacking large problems of the professional programmer, and making small ones possible for the novice user. Object-oriented design is a successful attempt to qualitatively improve the efficiency of modeling the ever more complex dynamic systems and user relationships made possible by the silicon explosion.

Here were some of Smalltalk's greatest achievements:

  • Smalltalk introduced the world to the language virtual machine, on which Java and Ruby are based, as well.
  • Smalltalk pioneered JIT (just-in-time) compilation.
  • From Smalltalk came the first modern IDE (integrated development environment), which included a text editor, a class browser, an object inspector, and a debugger.
  • Smalltalk was the first graphical language tool to support live programming and advanced debugging techniques such as on-the-fly inspection and code changes during execution in a very user-friendly format.
  • Since Smalltalk-80 (in 1980), it has had first-class functions and closures which, oddly enough, make Smalltalk quite good for functional programming.
  • Smalltalk introduced the software architectural pattern MVC (Model-View-Controller).
  • To a large extent, Smalltalk was responsible for giving us Test-Driven Development (TDD) and extreme programming (XP), which are both very influential in today’s standard agile practices.
  • Smalltalk made “duck typing” a household word.
  • Smalltalk pioneered the development of object databases of which GemStone/S is a great example.
  • Smalltalk gave us the first refactoring browser.
  • Smalltalk was instrumental in developing the graphical user interface (GUI) and the “what you see is what you get” (WYSIWYG) user interface.
  • Steve Jobs was inspired by Xerox PARC’s GUI and WIMP (windows, icons, menus, pointer) to completely realign Apple’s strategy; the GUI was a direct outflow of Smalltalk work.

Smalltalk was once a very popular language. It made its debut in August of 1981 on the cover of BYTE magazine:

Image 1

Since then, Smalltalk directly inspired a generation of other OOP languages including Objective-C, Erlang, CLOS, Ruby, Python, Perl, PHP, Dart, Java, Groovy, and Scala. Apple even created a Smalltalk for the Macintosh.

Smalltalk’s popularity peaked in the 1990s when it was the most popular OOP language after C++. According to a 1995 IDC report, OOP language market shares were:

  1. C++ — 71.3%
  2. Smalltalk — 15.1%
  3. Objective-C — 5.7%
  4. Object Pascal — 4.2%
  5. CLOS — 2.5%
  6. Eiffel — 1.1%
  7. all others — 0.2%

Here’s a page from Computerworld, November 6, 1995, showing Smalltalk and C++ duking it out:

Image 2

Smalltalk was so good for business use that in the 1990s, IBM chose Smalltalk as the centrepiece of their VisualAge enterprise initiative to replace COBOL:

Image 3

In the early 2000s, the U.S. joint military used Smalltalk to write a million-line battle simulation program called JWARS. It actually outperformed a similar simulation called STORM written in C++ by the U.S. Air Force. That by itself was an astonishing testament to the capabilities of the language.

Today, Smalltalk is still used by enterprises around the globe. Among its more prominent users are:

Just to name a few. In my home country, Smalltalk is used by Communications Security Establishment (CSE), Canada’s national cryptologic agency.

Cincom, Instantiations, and GemTalk Systems are major Smalltalk vendors.

Smalltalk is the most productive of all the major programming languages, according to this Namcook Analytics study (Table 16, measured by “economic productivity” in terms of number of work hours to deliver 1,000 function points):

  1. C — 26,273
  2. Fortran — 22,394
  3. JavaScript — 15,929
  4. Forth — 14,636
  5. Haxe — 14,636
  6. Lisp — 14,636
  7. C++ — 12,697
  8. Go — 12,697
  9. Java — 12,697
  10. PHP — 12,697
  11. Python — 12,697
  12. C# — 12,309
  13. Dart — 11,620
  14. F# — 11,312
  15. Ruby — 11,312
  16. Erlang — 10,758
  17. Elixir — 9,845
  18. Haskell — 9,845
  19. Julia — 9,465
  20. Perl — 9,465
  21. Delphi — 8,289
  22. Objective-C — 7,848
  23. Visual Basic — 7,848
  24. Eiffel — 7,156
  25. Smalltalk — 6,879

Demystifying Object-Oriented Programming

As Alan Kay said:

Smalltalk is a Software Internet

A parallel to the ARPA internet ideas: virtual computers universally connected through virtual networks

There are only objects (objects are made from objects, the network is made from objects, etc.)

(No applications, no file system, just synergies of the virtual computers)

The language is the language of messages between objects

Some objects act as places for making combinations of objects

Objects may be viewed at a place and be integrated with each other

This idea also directly inspired the design of the Erlang language, which can rightly be called "object-oriented" according to Alan Kay's conception.

An object is very much like a computer with its own private internal state and a communications protocol. You communicate with an object by sending it messages. You ask an object to do something for you by sending it a message and it responds, just as a real computer server in a network might do. And just like in a real computer server, you are not privy to its internal state.

An object is not an Abstract Data Type

…where Abstract Data Type is a highfalutin term for a type of data structure.

OOP languages like Java and C++ are a bastardization of Alan Kay’s OOP conception. They make OOP harder than it has to be, and are an endless source of confusion for many. As Robert C. Martin says in “OOP vs FP,” objects are bags of functions, not bags of data. Objects are not data structures.

Inheritance is not mandatory in Smalltalk. You may use composition or aggregation as appropriate for your programming solution. Any hysteria over the "flaws" of inheritance is just that: hysteria...and ignorance. As a tool, inheritance is certainly applicable and appropriate in many situations, for example, GUI programming. Especially if you employ inheritance as a means of specialization and not code reuse.

Getting Started with Pharo

There are many different flavours of Smalltalk. The one I'll be discussing here is called Pharo. It's an open source variant of Smalltalk modernized for the Twenty-First Century.

The Pharo Consortium helps to make the language enterprise-ready. Some notable Consortium members are:

  1. JPMorgan Chase, a major American banking and financial services company
  2. Thales, a giant French engineering firm specializing in aerospace, defence and security
  3. GemTalk Systems, a leading vendor in object-oriented DBMS
  4. Inria, the French Institute for Research in Computer Science and Automation
  5. ESUG (European Smalltalk User Group)

Installing Pharo

From http://pharo.org/web/download, you can find downloads for Windows, macOS, and Linux. What you are actually downloading is the Pharo Launcher, a powerful tool that allows you to create a variety of Pharo images. You can then launch any of those images as you like.

For Linux, you need to change directory to where the Pharo Launcher was downloaded to and execute the script:

cd pharo-location-folder
./pharo

You can put this into a shell script (I called mine start) and run it as a program by setting the permission (see the properties of the script file).

Launching the Pharo Image

When you start up the Pharo Launcher, you see two sections: Templates and Existing images (which is initially empty). From the Templates side, pick the latest stable official distribution (Pharo 6.1 at the time of writing). Right-click and choose “Create image”.

Image 4

To launch any of the Existing images, right-click and choose “Launch”. Easy-peasy.

Getting Acquainted with the Pharo IDE

Pharo’s System Browser is the main interface to the Pharo programming system. It’s much like today’s IDEs such as Eclipse and IntelliJ IDEA, only much simpler and more elegant. To open the System Browser, left-click on open space to bring up the World menu, then select System Browser.

The System Browser consists of several panes:

  • the Packages pane is where you see all the logical groupings of classes
  • the Classes pane is where you see all the classes for a particular package
  • the Protocols pane is where you see the logical groupings of methods for a particular class
  • the Methods pane is where you see the methods for a particular protocol
  • the Edit pane is where you can view, edit, or create new methods and classes
  • the Quality Assistant pane is where you can see helpful hints and tips for whatever you are currently editing

Image 5

Now, you shall create the canonical Hello World program. First, you need to create a package and class for it. You shall name the class 'Greeter' and the package ‘HelloWorld’. Edit or type the following in the Edit pane:

Object subclass: #Greeter
    instanceVariableNames: ''
    classVariableNames: ''
    package: 'HelloWorld'

Don’t forget to save by pressing Ctrl-S or Cmd-S (in the Mac).

Now, you need to create a method or function for your Greeter class. The standard first method is ‘initialize’ which is invoked when the Greeter class is instantiated. For simplicity, skip this.

Create a method called ‘sayIt’ and print “Hello World” in this method.

Select “no messages” in the Protocols pane so that the IDE knows you are working with methods. Edit or type the following in the Edit pane:

sayIt
    Transcript show: 'Hello World'

And save.

Your Hello World program prints to the Transcript (similar to a console). To open the Transcript, left-click on open space to bring up the World menu, select Tools, and then select Transcript.

Finally, to run your program, you need to open the Playground. To open the Playground, left-click on open space to bring up the World menu, then select Playground. In Playground, enter the following text:

Greeter new sayIt

Select the text, right-click and choose “Do it”. You should immediate see “Hello World” in the Transcript window.

Congratulations! You’ve created your first Pharo program.

Features and Syntax

Smalltalk's syntax is based almost entirely on message passing: sending messages to objects. Even its control structures like conditionals and loops are implemented using messages. This makes the language so simple that its complete syntax fits on a post card!

Image 6

There are three kinds of messages: unary, binary, and keyword (which takes arguments). The object is specified first, followed by the message. Examples:

Date today. "unary message, which requires no other information"

1 + 3. "binary message, which sits between the receiver object and argument object"

window alert: 'hello world!'. "keyword message, which takes one or more arguments"

By the way, in Smalltalk, comments are delineated by double quotes.

The precedence rule says unary messages are performed first, then binary, then keyword. Otherwise, operations are performed from left to right. This can make arithmetic look slightly odd:

5 + 3 * 2. "produces 16, not 11. You can force conventional arithmetic precedence with parentheses:"
5 + (3 * 2). "a bit unusual but not a hardship"

To illustrate control structures in Smalltalk, here’s an if statement in several languages:

"Smalltalk if statement"
time > 120 
   ifTrue: [Transcript show: 'Time expired.'; cr.
            time := 0]
   ifFalse: [Transcript show: 'Time remaining: ', 
             (120 - time) printString, ' minutes'; cr]

# Python if statement
if time > 120:
    print 'Time expired.'
    time = 0
else:
    print 'Time remaining: ', 120 - time, ' minutes'

/* C language if statement */
if (time > 120) {
    printf("Time expired.\n");
    time = 0;
}
else
    printf("Time remaining: %i minutes\n", 120 - time);

(In Smalltalk, the comma operator performs string concatenation. The cr message inserts carriage return.)

Smalltalk is dynamically typed

This makes Smalltalk as flexible and agile as other dynamically typed languages like Python, JavaScript, Ruby, Perl, PHP, Clojure, Elixir, Racket.

Smalltalk is reflective

This means that a Smalltalk program is able to inspect its own structure and computation at runtime. This confers enormous power allowing programs to extend themselves with new classes and methods or ask “who sent this message to me?”

Computational reflection is used to implement a powerful way to handle errors. When an object is sent a message it doesn’t implement, it receives a doesNotUnderstand: message, along with a reification of the original message. There are many things that the program can do with the doesNotUnderstand: message, including extending itself with new functionality!

Smalltalk is image-based

The Smalltalk image allows you to save the execution state of your program at any time and resume execution later on from exactly where you left off! This is very similar to how the system image in OS virtualization software like VMware and VirtualBox works. This is also similar to how the web browser’s DOM or Excel spreadsheet works.

Image persistence can be used as a kind of database where all the data in your program is persisted to hard drive. There’s no need to futz around with an ORM or third-party database product.

Smalltalk has a live coding IDE

You can alter your program while it's running! Live coding and debugging is a powerful way to program and is the principal reason for Smalltalk's tremendous productivity.

Smalltalk has lambdas

This makes Smalltalk a functional language, as well, except that it doesn't have immutability. (Pharo now supports immutability.)

Summary

After two decades since Smalltalk peaked in popularity, it is high time we gave this language a second look. It's the ideal way to learn object-oriented programming. It's a fantastic way to amplify your programming productivity. And it's the most fun you can have with programming.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)