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

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

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

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

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

everyone makes a mistake writing a program. That mistake could be as simple as incorrectly typing a command or forgetting a closing parenthesis, or it could be as complicated as an algorithm that works perfectly except when receiving certain data. Because writing error-free programs is nearly impossible, most programmers use a special tool called a debugger.

      

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

Snapshot shows stepping through a program, line-by-line, can help you find errors or bugs in your program.

      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.

      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.

Snapshot shows 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

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

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