Table of Contents | Previous Page | Next Page
Copyright Don Sleeth, 1998 ©

Page 4 - Programming With Style

Programming with style

A computer program has to achieve more than just doing the job. Let's take a look at what a good program should do. Here are some goals to keep in mind when writing a program:

  1. It must work every time for every one. In other words it can't depend on the user doing things in the right order, or depend on the turtle facing the right direction or some other condition. It must account for and handle all those possibilities itself.

  2. The program code must be understandable and easily read by humans. This is very important. Sometimes you will write a program and then want to change or improve it at a later date. If you have written a messy hodgepodge of code, even if you do find the place to change, you may cause another problem.

  3. The program must be consistent with the language and with itself. This means that your choice of names for your procedures should make sense and follow the same consistent pattern throughout. Consistency is a very important quality for a good programmer.

  4. The program must be maintainable. In the early years of programming, programmers tried to think of all possible features and put them in the program. Now, after learning that that was impossible, we acknowledge that our program will need changing and try to build it so it is easy to change. One thing that helps in this regard is to use small, one purpose only procedures.

We won't achieve all of these goals every time, but they are guidelines to keep in mind.

Where to write your program?

First, let's talk about where not to write your program. If you right-click on a turtle, you see a dialog box which allows you to enter your instruction for the turtle. This is NOT a good idea! Goal number 2 above says that the program must be easily read. How can it be easily read if instructions are scattered all over the place with individual turtles?

The dialog boxes that are associated with the controls such as turtles and buttons are not where you want to hide your beautifully crafted code.

If you click on the menu selection Pages and then Procedures, you are presented with a blank page. This is where as much of your code as possible belongs.

Write a procedure.

Okay, we now have an instruction that makes the turtle draw a box. Here it is again:

Our beginning project is to develop procedures that make the turtle move in a variety of ways. If we left off the instruction pd (pen down) then we would have an instruction to make the turtle move in a square. Let's make that our first procedure.

You can teach MicroWorlds new words by writing procedures. If you wanted to teach your little sister how to set the table, you might start like this:

To SetTheTable
get a plate from the cupboard
walk to the table
put it on the table
return to the cupboard
get a glass
… and so on
end

You give her a list of instructions using words that she knows. You are, in a way, teaching her a new phrase "set the table". We are going to do the same with MicroWorlds.

This is the essence of programming, namely adding new procedures and capabilities to the existing ones that came with the language. What shall we call our new word? How about Square. Then, when we want the turtle to move in the shape of a square, we can just type Square, instead of that long instruction we developed above.

On the procedures page, type the following

to Square
    forward 50 right 90 forward 50 right 90 forward 50 right 90 forward 50 right 90
end
	
All procedures start with the word to and end with the word end. A procedure like this adds a new word to MicroWorlds vocabulary. Let's try it out! Press Ctrl-F to return to the program page. In the Command Center, type cc Enter if you need to clean out old messages and then type Square and press Enter.

Hmm, mine didn't work very well. I think the problem is that the turtle went so fast, you couldn't see it.

It is very common that things don't work quite right the first time. This is not a failure, it just means we can tweak our procedure to make it better!

Before we slow the movement down, there is another problem with our Square procedure. As it is now, it is not really very readable. Let's write the same long instruction over many lines, making it much more readable. We can do that on the procedure page whereas it is not as easy in the command center, or heaven forbid, a dialog box. MicroWorlds Logo just ignores the fact that the instruction is spread over many pages. MicroWorlds Logo is really just interested in whether the inputs and outputs are all satisfied.

Change your procedure so it looks like this:

to Square
    forward 50 right 90
    forward 50 right 90
    forward 50 right 90
    forward 50 right 90
end
	

There, that is much more readable and you can see at a glance how it works. Notice that our procedure Square takes no inputs (yet ;-) ) and has no output so its puzzle piece looks like this:

It is a "satisfied" instruction all by itself.

Now back to improving our first procedure. To slow the movement of the turtle down, let's add a built-in procedure, wait, after each turn that the turtle makes. Wait takes one input, a number.

Change your procedure so it looks like this:

to Square
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
    forward 50 right 90 wait 1
end
	
Hit Ctrl-F and we'll give it a try! Hey, that's much better. On the last line of the Square procedure, you see that wait 1 is there, even though the movement of the turtle is finished. We could remove this last wait 1 but there is little to gain and later we may want to use this procedure to make the turtle move round and round the square many times. In that case having the wait 1 will make the movement even.

Now we have a procedure to be proud of, but we still don't have a program.

Save your work

So that you can continue with this project at another time, please save your work as Page4

Table of Contents | Previous Page | Next Page

.