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

Чтение книги онлайн.

Читать онлайн книгу Beginning Programming All-in-One For Dummies - Wallace Wang страница 42

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

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

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 often created by an individual or small teams of people who worked in the same room. If they needed to talk to each other or share code, they could simply copy files from one computer to another.

      The problem is that at any given time, only one person can work on a program. Because most large programs are divided into multiple files, all files get stored in a central repository so there’s only one copy of a program.

      When someone needs to edit the program, they can check out the file they need. When they check out the file, the repository stops anyone else from checking out that same file. That way two or more people don’t try to modify separate copies of the same file and then wind up not knowing which version of the file to use in the future.

      When someone gets done editing a file, they can return it back to the repository for someone else to check out. Although there’s only one copy of a file that others can check out, the repository will often save the previous versions of each file. That way, if a modified version of a file causes catastrophic errors, it’s easy to revert back to the previous version of that same file.

      Sometimes two or more programmers will be allowed to check out the same file, and when they’re done, the version control software can merge the two different file versions into a single, new version. However, merging changes made in separate copies of a file can cause problems if the changes in one file interfere with the changes in another file.

      For example, suppose one programmer deletes a function in a file because it causes problems, but a second programmer writes new code that relies on that function. Merging the two changed files will then be tricky because if the deleted function is added or kept out of the file, it risks causing problems either way.

      Whether a program is small or massive, it can benefit from version control management. Version control makes sure you always work with the latest approved versions of a file and that you’ll always have backup copies to fall back on in case you make a mistake.

      If you work alone, version control management can keep you from losing crucial data. If you work in teams, version control management can improve everyone’s efficiency by making

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