Название: Beginning Programming All-in-One For Dummies
Автор: Wallace Wang
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119884422
isbn:
Program errors are called bugs, so a debugger helps you find and eliminate bugs in your program.
Two common debugger features include
Stepping or tracing
Variable watching
Not all bugs are created equal:
Some bugs are just annoying, such as the wrong color on a drop-down list.
Some bugs are critical, such as a bug that adds two numbers wrong in an accounting program.
Any bug that keeps a program from running correctly is a showstopper.
Stepping line-by-line
Stepping or tracing lets you run your program line-by-line, so you can see exactly what the program is doing at any given time. The second you see your program doing something wrong, you also see the exact command in your program that caused that problem. Then you can fix the problem, as shown in Figure 4-6.
FIGURE 4-6: Stepping through a program, line-by-line, can help you find errors or bugs in your program.
Sometimes when programmers find one error and fix it, their fix accidentally creates another error in the program.
Here are the two types of debuggers:
Source level: Lets you examine your source code line-by-line. So if you write a program in C++, a source-level debugger shows you each line of your entire C++ program.
Machine language: Lets you examine the machine language code, line-by-line, that your compiler created from your source code. Programmers often use machine-language debuggers to examine programs when they don’t have access to the source code, such as a computer virus or a rival’s program.
Stepping line-by-line through a small program may be feasible, but in a large program that consists of a million lines of code, stepping line-by-line would take far too long. So, to make stepping easier, most debuggers include breakpoints and stepping over/stepping out commands.
Breakpoints
A breakpoint lets you skip over the parts of your program that you already know work. So, if you have a program that’s 10,000 lines long and you know the problem is somewhere in the last 1,000 lines of code, there’s no point in stepping through those first 9,000 lines of code.
A breakpoint lets you tell the computer, “Skip from the beginning of the program to the breakpoint, and then step through the rest of the program line-by-line.” Figure 4-7 shows how you can highlight a line with a breakpoint. That way your program runs from the beginning to the first breakpoint. After your program stops at a breakpoint, you can step through the rest of your program line-by-line.
FIGURE 4-7: Breakpoints let you skip over parts of your program that you don’t want to examine line-by-line.
Over and out
The stepping over and stepping out commands are used to debug a large program that consists of multiple subprograms. Normally, stepping would force you to examine every subprogram, line-by-line. However, what if you know the problem isn’t in a specific subprogram?
By using the step over
and step out
commands, you can avoid stepping through lines of code stored in a subprogram.
STEP OVER
To avoid stepping through every subprogram, debuggers let you use the step over
command. This command tells the computer, “See that entire subprogram? Treat it as a single command, and don't bother stepping through it line-by-line.” Figure 4-8 shows how the step over
command works.
FIGURE 4-8: The step over
command lets you skip, or “step over,” the lines stored in a subprogram.
The step over
command lets you completely skip over any lines of code stored inside of a subprogram.
STEP OUT
Suppose you start examining a subprogram line-by-line and suddenly want to stop. As an alternative to stepping through the rest of the subprogram, you can use the step out
command, which tells the computer, “Stop stepping through the subprogram line-by-line right now!”
Watching variables
When you step or trace through a program, line-by-line, you can see how the program works. For more insight into your program's behavior, you can watch your variables.
You can read more about variables in Book 2, Chapter 2. For now, just think of a variable as a temporary place to store data, such as a number or a word.
Watching a variable lets you see what data your program is storing and using at any given time. That way, if your program is supposed to print a name but actually prints that person’s phone number, you can step through your program line-by-line and watch to see which line stores the wrong data for the program to print.
Not only can you “watch” how your program stores data, but a debugger lets you change data while your program is running. By changing data, you can see how your program responds.
For example, suppose a program converts temperatures between Celsius and Fahrenheit. If you store a valid temperature, such as 15, you can see how your program handles СКАЧАТЬ