📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Learn Object Oriented Programming – Your First Half Hour!

Code With Huw29:20

Transcription

What the heck is all this now?

If you're used to programming in C++, C#, Java, or some other object-oriented language, this is all going to look very, very strange. Yet, this is what object orientation is all about. Using this software, I can write bits of code and show the results with a click of a mouse while browsing the class library and methods in Windows.

Welcome to the world of Smalltalk! As you'll soon discover, things are done differently here.

This is the second video in a short series all about the big ideas of object orientation. Those big ideas were largely developed in the Smalltalk language, and although lots of other languages borrowed ideas from Smalltalk, some things got forgotten along the way. In this series, I want to give you a glimpse of some of those forgotten gems.

I'm Hugh, and this is a series based on the Smalltalk V tutorial that you can download for free. See the link down below. Smalltalk V was a version of Smalltalk released in the 1980s. You can see it here running in MS-DOS and from the early 90s in Windows 3.1.

In this series, I'll be updating the tutorial to work with Squeak, which is a modern version of Smalltalk that you can download for free.

In most of these lessons, I'm going to be following the Smalltalk V tutorial in the order of the chapters in the book. However, since Chapter 2 is all about the Smalltalk V programming environment, I'm going to begin by taking you quickly through the Squeak environment so that you'll be able to follow along with me if you want to.

I'm assuming that by now you've already downloaded and installed Squeak. If not, pause the video and do so first.

Of course, the environment of Squeak is different in many ways from that of the 1980s and 1990s Smalltalk V. Even so, the core tools are broadly similar. Here, I'll just highlight a few of the tools, menus, and keystrokes that you'll need to get yourself started.

You start Squeak just by double-clicking squeak.exe. When you load Squeak for the first time, this is what you'll see. Well, you might see a few prompts first asking you to set some options, but then once you've gone past those, this is the basic view of Squeak.

Squeak has this help file, which contains quite a bit of handy documentation, and there is also online documentation and videos that explain all the features in greater depth. You might want to take a look at some of those later on. But there's more help here if you look at this help file, and I'd suggest you probably want to start off by taking a look at this: the terse guide to Squeak.

Now, having done that, the first thing I'd suggest you do is to save an image. Click the little Smalltalk mouse button up here, select "Save As," and let's call it "smalltalkVtutorial.image" and accept that.

An image stores the complete state of your Smalltalk environment, including everything from the position of windows to the classes or objects that you may have added during a programming session. This means that you'll be able to restart Squeak exactly where you left off.

However, there's also the danger that you may accidentally change Squeak in ways that you didn't intend by adding, deleting, or modifying classes and methods accidentally. But if you've saved an image just for this tutorial, then you should be okay. The original Squeak image will be left unchanged.

Now, the next time you start Squeak, as I'll do here, you may be prompted to pick an image. You see, I've got the standard image here and the one I just saved. Well, I'm going to select the one I saved, and my system, as I left it with all the windows and so on, is restored from that image.

Now, as you can see from all these drop-down menus that appear, there are quite a lot of tools in Squeak. But for now, you can ignore most of these. In fact, when using Squeak or other Smalltalk systems, you may find that pop-up menus, mouse menus, are more often used than the drop-down ones.

Now, which menus appear depends on where you click and which mouse button you click. If I click on the background with the left mouse button, this is the menu you'll see. If I click on the background with the right mouse button, I get this menu.

Again, use the online Squeak documentation and the built-in help to find more information on these menus. I'll explain the options we need as they arise in these lessons.

There are two really important windows that we'll be using a great deal, though, so let me show you those straight away.

Left-click the background and select "Browser." Now you can see this pop-up window appear. I can enlarge it a bit so you can see what's here. This is the class browser. It shows you all the classes in the Smalltalk class library.

The class categories are shown in the left pane, and these group together classes that have something in common. You can see I can select each of these, and alongside, you can see different classes appear in this pane. The classes here are indented, so you can see which classes inherit from other classes.

So, here's SmallFloat64, which is a descendant of Float, which is a descendant of Number, and so on.

Now, alongside in this next pane here, let's go back to this select integer. You can see that there are various other identifiers. These are called method protocols, so they define various types of methods, again categorizing the methods.

Alongside those are the methods themselves. If I go to here, Arithmetic, I look at these methods here, and you can see methods listed in this pane. Down here in the bottom pane, you can see the actual code of each method.

Now, if you know the name of the class you're looking for, you can right-click in this left-hand pane. So I've right-clicked the mouse here and selected "Find Class." Let's say I'm looking for a class called Bag. There you go, it shows me the class, and now I can quickly see what methods are provided to me by that class.

Now, the other window we're going to be using a lot from the outset is a workspace. So I left-click the background and select "Workspace." That pops this window. Let me put it somewhere where I can more easily use it.

Now, in here, I can just enter bits of code to try them out. So let's try something simple: some arithmetic. 10 multiplied by 5.

Now, if I want to see the result, I can just right-click again. There's a mouse menu there, and select "Print It." It shows me the result: 50. Or, as a shorthand, I can just press Alt + P, and that does the same thing.

Okay, so that's been a really quick introduction to Squeak. Let's now go straight into Chapter 1 of the Smalltalk V tutorial, and I'd suggest you read all of that chapter in your own time, as it does a good job of explaining what, in Smalltalk terms, object orientation is all about.

For this lesson, I'm going to skip straight to the section on Smalltalk compared with conventional languages. By conventional languages, the Smalltalk V tutorial means procedural languages, that is, languages without object orientation.

Now, this being the 1980s, the language for comparison with Smalltalk is Pascal. Pascal was a much more widely used language than it is today. There were numerous Pascal compilers available, including the exceptionally popular Turbo Pascal, which, in fact, is the language that I first learned when I started to learn programming.

Now, it doesn't matter if you're unfamiliar with Pascal. Many of the points being made in the tutorial equally apply to other languages such as C# and Java, which is odd really since C# and Java are object-oriented languages. So, in principle, you might think that they should really have more in common with Smalltalk than with Pascal.

Well, I leave you to make up your own mind about that. If you plan to follow along with me, load up Squeak and then left-click to open up a workspace. This is going to give you somewhere where you can enter code to try it out.

Now, remember that the Smalltalk V tutorial will often have more detailed explanations than I'll give, so be sure to read that too.

Now, I'm going to load that up and go straight to page seven of the tutorial, which is where we first see some code samples.

Right, starting off: assignment to a scalar variable. This means doing simple operations like adding the values of two variables using operators such as plus and an assignment operator. The assignment operator in both Smalltalk and Pascal happens to be ":=".

So let's get back into Squeak and try this out. In a workspace, you can just enter something like "a := 10 + 5." Then right-click and select "Do It."

Now, "Do It" is an instruction to Smalltalk to evaluate that bit of code. Having evaluated that code, I can now see if the assignment has been done to this variable a.

So this time, I'm going to right-click on the line of the variable and select "Print It." And there you go! I can see that the evaluation is 15. a has been assigned the value 15.

So, assignment in Pascal and in Smalltalk looks pretty much the same. Assignment also looks similar to other languages like C# or Java, apart from the assignment operator, which in those languages doesn't use a colon.

In fact, operators in Smalltalk are not quite what they seem, but I'll have more to say on that later on.

Next up in the Smalltalk V tutorial: a series of statements or expressions. Here again, the differences between Smalltalk and most other languages are trivial. The main thing to be said is that Pascal code shown here, and it's the same in many other widely used languages, terminates the statements with a semicolon, whereas Smalltalk uses a period or a full stop.

Let's look at this in Squeak. I've entered the code already here to evaluate it. I can just right-click, select it, right-click, and "Do It," or I could press Alt + D as an alternative keystroke.

Then I can evaluate each of those variables using the command "Print It" as before, putting my mouse on the right line and selecting "Print It." So I can see X is zero, or as a shortcut, I can use the keyboard hotkey Alt + P for "Print It," and it shows the answer.

Notice, by the way, when you're reading the Smalltalk V tutorial that uses the command "Show It." "Show It" is the same in Smalltalk V as "Print It" is in Squeak.

Let's have a look at the next one: w = hello. So this shows the value that's been assigned to each of these variables, and finally, Z is also hello because Z has been assigned the value of w.

Okay, now things start to get a bit more interesting in the next section of the tutorial: a function call with one argument.

Now, in most languages, this is how you call a function and pass an argument to it. This tutorial shows it in Pascal, but it's not much different in a language like C# or Java.

Now, look at how Smalltalk does it. If I turn to Squeak, here is my code in Squeak. I've got an array, which I assigned to the variable a. So I press Alt + D to do it, to evaluate it, and then Alt + P to print it, evaluate and display its result, which is the size is five.

Now, to get through the rest of this chapter more quickly, I've opened up this workspace in which I've already written the code samples from the entire chapter.

Now, looking at this code, apart from the parentheses, the first thing you'll probably notice is that the argument name comes before the function name. Why is that?

Let's see what the tutorial says. In Smalltalk, calling a function is known as sending a message. In this case, the message "size" is sent to the contents of the array variable.

If you haven't programmed in Smalltalk before, this may sound like gobbledygook: the message "size" is sent to the contents of the array variable. What on earth does that mean?

In fact, it's going to take a while to explain messages and why they're important. Suffice it to say, this is one of the big ideas of Smalltalk.

Yet, while modern object-oriented languages make a big deal about objects, messaging has largely been ignored. Alan Kay, the principal designer of Smalltalk, has said that he regrets coining the term "objects" because it gets many people to focus on the lesser idea. The big idea is messaging. That is what the kernel of Smalltalk, Squeak, is all about.

Messages are all over the place in Smalltalk. In fact, even operators like the addition and assignment operators we saw earlier are really messages. The Smalltalk V tutorial has a lot to say about that subject later on.

Function calls with two arguments: bear in mind that when the tutorial talks about function calls, it's referring to function calls in a traditional non-object-oriented language. Function calling is an idea that is well understood by programmers of Pascal, C, C#, and so on.

A Smalltalk equivalence is a bit different. Instead of calling a function, a Smalltalk programmer passes a message to an object. Yep, we are back at those messages again. I did tell you that this is an idea that we'll be seeing quite a lot of.

Now, let's see what it says in Smalltalk. For a two-argument message, the arguments precede and follow the message name. In Smalltalk, the standard arithmetic operations are performed via messages.

In the first example, the message "max:" is sent to the contents of variable x1 with the contents of x2 as the argument. The result returned is assigned to the variable x.

In the second example, the message "plus" is sent to the contents of variable p with the contents of variable q as the argument, and the result returned is assigned to the variable y.

Oh right, so clearly we're starting to get into deep water here. Let's have a look in Squeak.

I've slightly adapted this code in this section here, so let me just do this. That's to run the code, if you like, and evaluate these expressions.

So, "Do It" and "Print It" to run and display the results. Print this one first, so that's 12. And then this down here, and that evaluates to 32.

Now, the rest of this chapter in the book covers quite a bit more of the essential Smalltalk syntax, and you may want to read through this after watching this video.

I'm going to skip through it quite quickly because I want to get into some more of the big ideas of object orientation rather than going through all the details of the Smalltalk syntax.

A function call with three arguments: now here is an example of a two-part method. If you're used to functions in other languages, this looks a bit odd. The method here is called "between:and:," and as with most method names in Smalltalk, a colon is put after the name when a piece of data, that's an argument, is expected next.

Now here, x is a numerical object, and at the moment, its value is 12. That's because of the value we assigned earlier on in this code up here. I can just verify that by printing it: Alt + P. Yep, still 12.

So I send the message "between:and:" to x, and let's have a look. I test it with 4 and 9: "x between: 4 and: 9." False, it isn't. I then try it again: "x between: 10 and: 20." Well, yes, it is. It returns true.

Incidentally, when you come across methods like this, you may want to know exactly what they do and which sort of objects can be sent such a message. Squeak has a special browser tool to help you find messages.

So let me just open this up. I left-click the background to show the world menu, then I click "Open Message Names," and here I can search for the message names that I'm looking for.

So I enter the message name in the pane up here and press enter. So let's look for "between" and press enter to search, and it's found this here.

Now, this shows me the class to which it corresponds: "Magnitude between:and:," and the actual code of the method is down here.

Now, you may be surprised to find that in Smalltalk, methods are often very, very short. This method compares the minimum and the maximum values with the value of the receiver object, by which I mean the object to which the "between:and:" message is sent.

In my code, the receiver, as you can see here, is the variable x, and the code of the method, the receiver, is shown as "self."

So this code tests if the value of the first argument, min, is lower than or equal to the value of self, which in my case is my x object. If so, it returns false. This is the return symbol in Smalltalk.

If that test is true and min is greater than the value of x, then this next bit of code tests if self is less than or equal to the second argument, max, and it returns the result of that test, which would be either true or false.

Now, don't worry if this syntax seems odd and confusing. There isn't really very much syntax in Smalltalk, as a matter of fact, and you will quickly get used to it.

For now, when browsing methods, be sure to read the comments. Comments in Smalltalk are put between double quotes like this.

And if you want to know more about the class to which the method belongs, click the browse button in the message browser, and that brings up the class browsing tool. You can find the class in the class hierarchy, and you can see that "Magnitude" is the ancestor of "Number" and "Float," and so on.

Subscripted variable access: this is the tutorial example that shows how to access an array element. I've rewritten this a bit in Squeak so that I can show you a bit more clearly how this works.

Now, in my example, I first create an array like this. Let me just evaluate it with Alt + D, and I can retrieve an element from that array by using the "at:" method with an index value, a number, 2.

So, do that, and I can evaluate x to check that that has been assigned the value at index 2. But when I try to modify the array, so at index 2, put this new value, I get this error: "Modification forbidden."

It turns out that the array is declared like this: this can't be modified. But there's a way around that. I can declare an array with elements separated by dots. This is now a modifiable array.

So let's do it, evaluate it. I can do this again, evaluate that, and check the array as before. It has the value 2. Yep, that's okay.

Now, this failed when I used the previous sort of array, but this new array should be modifiable. So let's see if I can put this string "XXX" at index 2. So I'll do it to evaluate it.

Let's have a look at a at this point, and you can see that the string "XXX" has been put at that new position.

Let's try something a bit more complicated. So a at 5: the element at 5, put that at index 1. Do that, and let's have a look at the array. You can see that indeed the value from the array at index 5 has been put at index 1.

Incidentally, notice that the arrays are indexed from one upwards, so the first element is at index one, whereas in many other languages that you may be familiar with, that would be at index zero.

If statements in Smalltalk: conditional statements are enclosed in square brackets. I've rewritten the example in the tutorial to show how this works.

So here, I've set a to 5, and I've evaluated it so that a should now have that value. Then I want to test if x1 is less than x2. Recall that I set the values of x1 and x2 earlier to be 12 and 10.

I can just check this will have that value by printing it: 12 and x2: 10. Okay, now I can test if x1 is less than x2, and if true, the code in square brackets runs and adds one to a.

So let's evaluate that and print the value of a. No, it's false, so a is returned as 5. But with this one, I can now test if x1 is greater than x2.

Do that and print a, and this time it has been incremented by one. So here, "if true" is a message that is sent to a Boolean object, and here that Boolean object is the result of this test, and it can be either true or false.

Iterative statements: by now, the syntax of Smalltalk should, I hope, be familiar enough to you to understand what's going on here.

Now, let's have a look in Squeak. So here, I've slightly adapted the example code to sum up this array of integers, and I've added this return statement at the end so I can print the calculated result.

In the second example, I iterate over the array a and put 0 at each position. Notice the variable "colon:IE" here followed by an upright bar. That's Smalltalk's way of declaring a variable that is local to the block of code between square brackets.

Let me quickly show you this code in action. Alt + D to evaluate all this, then I'll select this block of code and Alt + P to print it. You can see that it returns 15, the sum of the numbers, and this puts 0 at all the indexes.

So Alt + P to print it, and there's my array full of zeros.

Returning function results: the tutorial explains that a caret, this little upward-pointing symbol, the one normally above six on your keyboard, is the way of returning a value.

Well, we've seen that in use already. So in my code here, for example, I return the value of sum here.

Okay, let's move on to the next section: storage allocation and deallocation. Often, you will create new objects just by assigning values to variables, as I have done many times in this lesson.

You can also create objects using the "new" method, and you don't need to deallocate the memory set aside for those objects when you finish with them. The memory will be cleared up or garbage collected automatically.

Garbage collection is done in many modern languages too. It wasn't so common when the Squeak tutorial was written, which is why it's explained in more depth here.

Anyway, here's a simple example of storage allocation. So I've created a new array here. Evaluate that, and you can see if I print it, there's my empty array.

Now, I can't actually put anything in that array at the moment because I haven't allocated any slots for the elements. So if I try and evaluate this, up comes that error message: "Subscript out of bounds."

But I could allocate two slots here by passing two when I called "new." Let's do that, and now try to put "hello" again. This time, no error.

And if I print it, there is "hello" in the first slot, a nil value in the second slot.

A complete program: this is the final program in Chapter 1 of the tutorial. Now, here is a long comparison between Smalltalk and Pascal, which if you like, you can read at your leisure.

However, the whole program is then rewritten in this much-reduced version, and that's the version that I've translated into Squeak.

It prompts the user to enter a string, then it iterates over each character, assigning that character to this variable c. If c is a letter, then it adds it to a bag. A bag is one of many types of collection in Smalltalk, and again, you can browse the collections using the browser.

Well, let's try this out. So I'm going to print, evaluate, and print all of this. So it prompts me to enter a string. So I'll enter "time honored hello world," capital H, capital W. Click accept, and you can see down here that it's created this array, or this paragraph of characters, and each character is set in lower case, where the character is preceded to show it's a character by a dollar symbol.

The only thing I've had to change in my code compared to the code that you find in the Smalltalk V tutorial is the class of the text entry dialog. In Smalltalk V, it was called "Prompter." In Squeak, it's called "Fill in the Blank," and I use the "request" method to get some text in this line here.

And that's it! We've been through all the fundamental Smalltalk syntax that you need to get started with programming in Squeak.

This has been quite a long lesson, but we've only just scratched the surface of object-oriented programming. The next section of the tutorial, "The World According to Objects," is where it gets really interesting.

If you've only programmed in a modern object-oriented language like Java, C#, Ruby, or C++, this next section might have a few surprises in store.

As you can see, those pesky messages are back again. This is where we really start to find out what they're all about and why they're so important.

Now, you may want to bookmark this playlist so that you can follow the lessons in order, and subscribe to my channel and click the bell to be notified when I upload new lessons.

I'll see you again soon for more explorations in the world of object orientation!