Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

Chapter 15

Debugging Programs

In This Chapter

Understanding computer bugs

Finding syntax errors

Discovering run-time errors

Figuring out logic errors

Nobody writes programs that work 100 percent correctly all the time. The problem is that programming means giving the computer extremely

detailed instructions. Give the computer one wrong instruction or one misplaced instruction, make one wrong assumption or omit one necessary instruction, and the computer has no idea what to do next, which can cause your program to fail or, in programming lingo, to crash.

If a program doesn’t work correctly, programmers never say, “My program has a problem.” Instead, programmers use their own lingo and say, “My program has a bug.” Although eliminating all bugs from a program is impossible, Liberty BASIC (and many other language compilers) provides special features to help you track down the obvious bugs and wipe them out so that your program works well enough for people to actually use it.

Anatomy of a Computer Bug

Computer bugs tend to fall into the following three categories:

Syntax errors: This type of error occurs if you spell something wrong or type a command incorrectly, such as misspelling PRINT as PRRINT or typing too many commas.

Run-time errors: These errors occur if your program runs into something unexpected, such as if you ask the user to input an age and the user types a negative number.

Logic errors: These bugs occur if your instructions don’t work as you intend them to, but the computer performs these flawed instructions anyway, creating unpredictable results.

216 Part III: Advanced Programming with Liberty BASIC

Although bugs riddle every program, most bugs are relatively harmless or cause only minor problems, such as displaying a menu incorrectly at unpredictable times. Bugs that keep a program from working at all are more serious. Any bug that keeps a company from shipping (and selling) a program is known as a showstopper.

Syntax Errors

Syntax errors are often misspellings of valid commands or omissions (or misplacement) of crucial characters such as a comma or a left parenthesis. If you misspell a BASIC command such as PRINT, Liberty BASIC is usually smart enough to highlight the line where the syntax error occurs so that you can fix it later. If Liberty BASIC finds a syntax error, it highlights the line causing the problem and displays a message in the bottom left corner of the screen, as shown in Figure 15-1.

Syntax error in code

Figure 15-1:

Liberty BASIC highlights lines containing syntax errors, such as misspelling a command as buttton instead of button.

Error message

Chapter 15: Debugging Programs 217

The Destruction of Mariner 1

Syntax errors are usually caught because they give an invalid command to the computer, which immediately keeps the program from running at all. The worst syntax errors, however, are those that somehow give a valid but unintended command to the computer. Thus, the program keeps working — but not the way you want it to.

Back in 1962, NASA sent the Mariner 1 probe to study Venus, but before the rocket carrying the probe could make its way into outer space, it veered off course and NASA had to prematurely detonate the rocket. According to one story, the program was supposed to contain a FOR NEXT

loop that told the computer to loop three times, as in the following example:

FOR I = 1, 3

But instead of a comma, the programmer had accidentally typed in a period, as follows:

FOR I = 1.3

So instead of telling the computer to loop three times, this command told the computer to set the value of the variable I to 1.3. The end result was that this error managed to give the computer a valid but incorrect command, causing NASA to lose a multimillion dollar rocket and payload as a result.

Although syntax errors usually prevent a program from working at all, watch out for your own misspellings because the computer will assume that you know what you’re doing, even if you make a mistake. Especially troublesome are those times that you misspell a variable name. Consider, for example, the following program:

PROMPT “How many times do I need to tell you no”; Answeer$ PRINT “Your reply = “; Answer$

END

The preceding program asks the user, “How many times do I need to tell you no?” Then the program stores whatever the user types into the variable Answeer$ (the misspelling of Answer$). Because Liberty BASIC considers Answer$ and Answeer$ as two completely different variable names, the Answer$ variable doesn’t contain any data. If you run this program, it doesn’t print out what the user types, simply because of a single misspelling.

Liberty BASIC can detect syntax errors in misspelled commands, but it cannot detect misspelled variable names. If you insert a space between a function or subroutine name and any parentheses that follow it, Liberty BASIC may treat that as a syntax error, too, although it won’t necessarily tell you so.

Besides misspelling a variable name, watch out for mixing upperand lowercase letters in variable names. Liberty BASIC considers the variable Answer$ as completely different from the variable answer$ simply because one starts with an uppercase letter A and the second one doesn’t.

218 Part III: Advanced Programming with Liberty BASIC

Death from the Therac-25

The Therac-25 was a radiation-therapy machine designed to adminster radiation doses to patients. To prevent excessive doses of radiation, the Therac-25 included a safety mechanism that relied completely on its software to prevent any problems — but the software contained a fatal run-time error.

The Therac-25 offered two modes of operation: X-ray and electron beam. If the technician accidentally selected the X-ray mode first, the Therac-25 would select a high-intensity energy level. If the technician then switched to electronbeam mode right away without completing the

X-ray mode, the Therac-25 would maintain its higher energy intensity level for electron-mode operation, which meant delivering a fatal radiation burn to any unlucky patient lying underneath.

Only after several people suffered severe radiation burns at the hands of the Therac-25 did someone finally discover this hidden run-time error, but by then, it was too late for all the people who’d already been irreparably burned by the Therac-25 and its supposedly fail-safe software safety mechanism.

Because a single misspelling of a variable name can mess up your program, many programmers take shortcuts and choose short, cryptic variable names. Don’t take this route! The time that you save in using short variable names is lost in comparison with the time that you need to decipher what those short variable names represent.

Such errors can prove hard to detect because a misspelling can still enable the program to run, albeit incorrectly. Anytime that your program runs but doesn’t seem to work right, start looking for misspellings or missing elements such as quotation marks, commas, upper and lowercase variable names, and parentheses, any of which may cause the program to fail.

Run-Time Errors

Run-time errors are sneaky little bugs that hide in programs. A program may work correctly right up until it receives data that the programmer never expected, such as a negative number that the user types for his year of birth. Unfortunately, the only time that you can find run-time errors is after they cause the program to crash.

Because run-time errors occur only after your program receives data that it doesn’t know how to handle, the best way to hunt down run-time errors is to run your program over and over, feeding the program extreme values of different data each time.