Beginning Programming All-in-One For Dummies. Wallace Wang

Читать онлайн книгу.

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

Snapshot shows the step over command lets you skip, or “step over,” the lines stored in a subprogram.

      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

      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.

Snapshot shows watching and changing variables can show you how a program reacts to different data.

      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.

      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.

      In the old days, programs were


Скачать книгу