Beginning Programming All-in-One For Dummies. Wallace Wang
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 the number 15. But what happens if the user types in an invalid number, such as 500000 or –1700000?
To find out how and why your program seems to randomly change the temperature, you can step through your program and watch the number stored as the age. By changing your variable while the program is running, you can type in different temperature
values to see how your program responds.
When testing different values, always start off with values for which you already know how your program should respond. For example, a temperature of 0 in Celsius should be 32 in Fahrenheit. Likewise, a temperature of 100 in Celsius should be 212 in Fahrenheit.
Without the ability to change the value of variables while the program is running, debugging a program is much slower and more tedious. By changing the value of variables while the program is running, you can test different values without having to trace through the program multiple times using different values. Just run the program once, and change the value of the variable as many times as you want, as shown in Figure 4-9.
FIGURE 4-9: Watching and changing variables can show you how a program reacts to different data.
Saving Time with Third-Party Components
Programmers are naturally lazy and often look for the simplest way to solve any problem. When faced with creating a program, programmers prefer to cheat by using third-party components, which are programs that somebody else has created already (and, hopefully, tested).
Third-party components usually offer commonly needed features, such as a word processor, a spreadsheet, or a database, that you can plug into your own program. So, instead of having to write your own word processor, you can buy a word processor component and plug it into your own program — now your program has word-processing capabilities with little additional programming on your part.
Third-party components can give your program instant features, but they can also give you instant problems, too. If the third-party component doesn’t work right, your program won’t work right either, and you can’t fix the problem until the company that sells the third-party component fixes the problem. Basically, third-party components put you at the mercy of another company. If that other company stops updating and improving their component, you’re stuck with an outdated and possibly buggy component.
Depending on the features, third-party components can range in cost from a few hundred dollars to a few thousand dollars or more. Most third-party components aren’t cheap, but because they can save you a lot of time, they may be worth the price.
Optimizing a Program with a Profiler
Not all parts of a program are equal. Some parts of a program may run only once or twice, whereas other parts of a program may run hundreds or even thousands of times. For example, suppose you have a program that stores names and addresses. To use this program, you must first type in your name and password before you can sort and search through the program’s list of names and addresses.
In this example, the part of the program that asks for a username and password runs only once, but the part of the program that searches and sorts through your list of names and addresses may run multiple times. So, which part of your program determines its speed? The part that runs once (asking for a username and password) or the part that runs multiple times (searching and sorting names and addresses)?
Obviously, if you wanted to speed up your program, you’d focus on the part that runs most often, and that’s what a profiler does. A profiler examines your program and identifies the most frequently used parts of that program. After you know which parts of your program run most often, you can work on making that part of the program run faster, a process known as optimizing.
Profilers typically use two methods for examining a program:
Sampling: Sampling examines your entire program at fixed time intervals. Through sampling, a profiler can get a rough estimate on which parts of your program (often referred to as hot spots) are being run most often.
Instrumentation mode: After you use sampling to identify hot spots, you can use this mode to examine the program in more detail to determine exactly what each program line does and how much time it takes.By identifying the hot spots, you can speed up your entire program by rewriting or optimizing those frequently run parts of the program. By optimizing a small part of your program, such as 10 percent of it, you can often improve the entire program’s performance dramatically.
Managing Source Code
In the old days, programs were