Terrapin Resources

Crush the Bugs!

Nobody writes a perfect program. Some claim to be able to, but only one person has proven that he can write bug-free code. But that is a long story.

Why bugs? Why not errors, mistakes, or whatever? The story behind the word “bug” goes back to 1947, to the time when computers were big monsters with vacuum tubes instead of silicon chips. These tubes were seething hot and needed a voltage of 300–400 volts. One day, an unfortunate bug (a moth) managed to crawl into the computer. Eventually, the high voltage killed and fried the bug, causing the computer to crash. The technician who found the bug taped it to the log and wrote “First actual case of bug being found!”. The computer was a Mark II at Harvard University.

Bugs are often hard to find. This chapter describes various ways that Logo helps you to find and squash these bugs.

This chapter tells you all about the various options Logo offers to debug a program.

Let us start with the Stop button. There is a Stop button in the Listener panel as well as in the Icon Bar. These buttons will be bright red when a program runs. Click any of these button to stop your Logo programm immediately.

But there is more - Logo’s built-in debugger. Usually, the Debugger panels are hidden from view, and you won’t even know that they exist. It can be invoked with the help of the PAUSE and STEP commands; see below for details.

TO have the debugger pop up when Logo runs into a runtime errors, please enable the debugger in the Settings dialog.

First of all, there is good news. You can pause your program at any time, or you can even stop your runaway program. The Listener panel has two buttons for this purpose:

   
Click this button to pause your program and to enter the debugger. Logo displays this button only if the debugger is enabled in the Settings dialog.
The Stop button stops the execution of Logo and returns to toplevel.

The Icon Bar contains the very same buttons, because the Listener panel may be hidden (see, for example, the FULLSCREEN command).

The FIFTY Bug

Let us begin with a very common mistake, the FIFTY bug. You simply supplied the wrong value to a procedure, for example, and Logo already starts to complain. Enter this in the editor:

TO POLYGON :N :SIZE LMAKE “ANGLE 360 / :N DRAW REPEAT :N [PART :SIZE :ANGLE] END TO PART :DIST :ANGLE FD :DIST RT :ANGLE END

POLYGON draws a polygon with :N being the number of corners and :SIZE the length of one side. For a better explanation of the debugger, the drawing itself is in the PART procedure, which takes the distance to travel and the angle to turn the turtle after travelling. Drawing a pentagon would look like this:

Now, try to feed a bad value to the procedure. Let’s say we use “FIFTY instead of the value 50, which is clearly wrong. Try this, and Logo runs into an error and switches to the Debugger view:

This is quite different from the usual view. Let us start with the Listener panel. The panel shows the error, and the message “Paused at PART, line 2”. This is where Logo stumbled and stopped. The Listener panel suddenly displays a number of buttons and a slider in its title bar, but everything is grayed out except for the Halt button to the right. This is because Logo cannot continue after the error, so all you can do is to halt the program.

But before halting the program, you can look at a lot of interesting things displayed in the right panels.

The top panel displays all of the names available to Logo at the time Logo paused. This includes the names that you created with the MAKE command, which usually end up at the top level. Additionaly, Logo created names when it entered a procedure. For the POLYGON procedure, these were :N and :SIZE, for example. Therefore, the Names panel displays the name and its value along with the name of the procedure that created them. You can even alter these values, but during a runtime error, this makes little sense because all of these names will go away once Logo leaves the procedure that created them.

The lower panel displays the currently active procedure with the current line highlighted. This panel goes along with the middle panel:

The middle panel is the Procedure Call Chain panel. It displays all currently active procedures. Since you called POLYGON, and POLYGON called PART, you see two entries.

This panel actually controls the contents of the other two panels. If you click a different line, the two other panels switch to the current procedure (and line) of the selected procedure call, and the Names panel displays all names available to Logo at that level. In this example, if you click the lower line, Logo switches to show the POLYGON procedure, and the line is where Logo called the PART procedure, which is yet has to return from. The Names panel now displays the names available to POLYGON, which excludes the names defined within PART.

Also note that ANGLE is defined in both places because PART redefines ANGLE.

Let us look closer at the middle panel. The bottom line is the command that you entered (thus the procedure name “(toplevel)” which means that the command was entered in the Listener.)

That command invoked the POLYGON procedure. The two lines above seem identical, but the lower line shows the line before it is executed, and the third line from the bottom shows the code inside the REPEAT sommand, where Logo already replaced :N with the value 5. THe second line from the top displays the invocation of the fierst linbe of the PART procedure, while the top line dsplays the active line, where :SIZE has been replaced with the value “FIFTY”, and that’s where the problem lies!

You could use the Listener to enter commands as well. In case of a runtime error, all you can do is to halt the program, but looking at all the data in the Debugger view will certainly help you find out what went wrong.

Pausing a Logo Program

Often, you would want to pause a running Logo program and see what it does exactly. Logo offers several ways to pause a program.

  • Click the Pause button.
  • Or add a PAUSE command to your procedure.
  • Or use the STEP command.

The PAUSE command lets Logo stop and enter the debugger. Use it to interrupt Logo before it enters a section of code that you wish to debug.

The STEP command takes a procedure name or a list of procedure names as input. When Logo enters one of the stepped procedures, it will pause at the beginning of that procedure. Try for example STEP "POLYGON.

Actually, the STEP command can do much more. It can also stop Logo when the value of a name changes, or then a property value changes. Please see the command description for details.

Use the UNSTEP command to undo the effects of the STEP command.

Just for completeness, there are TRACE and UNTRACE commands available that work like the STEP and UNSTEP commands, but instead of pausing the program and displaying the debugger, they write messages to the Listener panel. This output can be quite overwhelming, since it contains a lot of information.

During a PAUSE

Run the above procedure after issuing the command STEP "POYLGON. Use the command POLYGON 5 50, which should draw a 5-sided polygon with 50 pixels per side. Obediently, Logo pauses at line 2 and displays the debugger.

Note that all controls in the Listener headier are available now that Logo is paused, so this is a good time to explain the controls, from right to left:

Control Name Explanation
Halt Halts a program.
Pause Pauses a program. Click this button anytime while Logo is running to pause the program and display the debugger.
Step Each click on the Step button lets Logo execute the next line, and then pause again. This way, you can step through your program line by line. If your procedure calls another procedure, the debugger steps into that procedure.
Step Out Let Logo continue, but pause when it is about to return from the current procedure so you can see the value that Logo is about to return.
Step Over This is almost the same button as the Step button, but Logo will not step into a procedure call; instead, it will run the procedure in realtime and continue at the next line of the current procedure.
Continue Close the debugger and continue running.

The Animate slider lets you monitor the execution of a program step by step, where you decide how fast Logo runs. If you move the slider to the left, Logo stops execution. Move it slowly to the right, and Logo starts execution line by line very slowly. Move it farther to the right, and Logo will run faster. Using this slider, you can waich Logo run your program, and you decide how quickly this will be.

If you click any other button while the slider is not at the far left position, Logo will stop the animation and wait for further commands.

During a pause, the Names panel lets you change the value of any name. You can also enter any Listener commands that you would like. Note that if you issue any edit command, the editor will be loaded with whatever you asked it to load, but it will not be visible until you switch to a view that displays the editor.