Lesson 03

Advanced Syntax & New Bot Commands

In our last lesson we learned some new vocabulary and basic syntax.  We learned the difference between Classes and Objects (Instance), Methods and Messages as well as Variables, Message Selectors and Arguments.  We also learned about Scripts (Programs) and Expressions.  Finally, we learned the importance of proper expression order, capitalization and punctuation.

Today, we will expand on these a little further as well as introduce some new commands for our Bot.

Cascading Messages

Just as in English, there are ways to say things and then there are better ways to say those same things.  For instance, we could say:

Clinton, cleanup your room.
Clinton, bring down your laundry.
Clinton, take out the trash.

However, that is more than a bit tedious and Clinton (and everyone else in the room) very quickly gets tired of hearing Clinton’s name repeated.  Normally, we would only say Clinton’s name once (after we get his attention) and then give him a list of things to do. For instance:

Clinton, cleanup your room, bring down your laundry and take out the trash. 

Wouldn’t it be nice if we could do this in Squeak?  Thankfully, you can. It is called “Cascading” your commands.

| pica |
pica := Bot new.
    go: 100 ; turnLeft: 90 ; go: 200 ; turnLeft: 90 ;
    go: 100 ; turnLeft: 90 ; go: 200 ; turnLeft: 90.

Recall the first line is our variable declaration and the second line creates a new Bot and assigns it to our variable (pica).  Now, take careful note of the punctuation.  First, just as our commands in English have to be separated, so too must the commands to our Bot be separated.  In English, we separate each directive with a comma. In Squeak, we separate each message with a semicolon (;). Each command (message) is still formatted the same.  The message selector, followed by a colon (:) and then argument (value). Look now at the end of lines, three, four and five. What is the difference? Well line five ends with a period (.), line five with a semicolon (;) and line three with no punctuation at all!  How is that even legal?

Remember, each expression, or statement must end with a period.  Squeak expressions, just like English sentences, can span multiple lines.  Therefore, the expression does not end until the period!

Looking back at the script above, you might be inclined to ask about the indentation of lines four and five.  For Squeak, this is not required and is more of a stylistic preference. Just as English uses indentation and block quotes to make certain text easier to read, programmers often do the same.  In English, we often indent the first line of a paragraph.  In programming however, we tend to do the opposite and outdent the first line of an expression or block of code and then indent each meaningful layer from there.  This will be more obvious when we study loops.

Complex Messages

Although not quite the same as cascading, we can make our messages more complex and embed operations within our messages.  For instance, the following two expressions are functionally the same.

pica go: 300 
pica go: 100 + 200

For the first line, Squeak will just move pica 300 units.  For the second line, Squeak will first add 100 + 200 getting 300, and then will move pica 300 units.  Keep in mind it is the order of and punctuation that makes this possible. Our object, pica, is listed first, followed by the message selector ‘go’ with a colon.  Everything after the colon must then be the message.  However, the message is actually a math problem. So Squeak solves that first and sends the resulting answer as the argument.

Another form of this is illustrated by the ‘color’ method.  Some of you may have seen this already, but did you notice it in the context of our new vocabulary and grammar lessons?

| pica |
pica := Bot new.
pica color: Color green.

On our third line we invoke the ‘color’ method of pica.  As stated before, everything after the message selector and colon and before the period is the argument.  In this case, that means ‘Color green’.  Recall from our previous discussion that Classes are always capitalized (just like ‘Bot’ on the second line.  So what we are doing here is actually sending the class Color as an argument to the color message of pica.  Sound confusing? It is not really, unfortunately we keep talking about the Color class being sent as an argument to the color message selector to set the color of our bot to something like green.  And you thought homonyms were bad!

Let us go back to our original way of doing just one thing at a time.  If we did, the above script would look like this:

| pica botColor |
pica := Bot new.
botColor := Color green.
pica color: botColor.

In this script, we take the intermediate step of declaring a new variable, botColor.  Just as we create a new Bot and save it to the variable pica, we now create a new Color object and save it to the variable botColor.  Now, when we want to set the color for pica, we pass it our variable botColor, which conveniently holds a Color object that is already green.  The only functional difference between the two scripts is the number of lines and the number of variables used to accomplish the same thing. 


There’s More Than One Way To Do It!  So why does it matter?  What is the difference between simple, complex or cascading expressions?  Style.  That is it.  When it comes right down to it, code is for humans, not computers.  Computers speak in ones and zeroes. The scripts make it easier for humans, not computers, to understand what is happening.  So the “best” code is the code most readable by humans.

New Bot Commands

In these chapters we get a collection of new Bot commands.  Some are obvious, like jump and beInvisible.  Others are obvious what they do, but what is not so obvious is why they exist.  Explore the directional methods. While you are doing so, consider why you might want to use turn vs. turnLeft or turnRight.  Consider also why you might want to use compass directionals instead of either turn method.  Consider the many ways you can get something done while completing the exercises in Chapters 3 and 4.