This page intentionally left blank Python for Software Design



tải về 1.38 Mb.
Chế độ xem pdf
trang6/83
Chuyển đổi dữ liệu13.08.2023
Kích1.38 Mb.
#55046
1   2   3   4   5   6   7   8   9   ...   83
- Python for Software Design How to Think Like a Computer Scientist-Cambridge University Press (2009)

algorithms.
1.3
WHAT IS DEBUGGING?
Programming is error-prone. For whimsical reasons, programming errors are called
bugs and the process of tracking them down is called debugging.
Three kinds of errors can occur in a program: syntax errors, runtime errors, and
semantic errors. It is useful to distinguish between them in order to track them down
more quickly.
1.3.1
Syntax Errors
Python can only execute a program if the syntax is correct; otherwise, the interpreter
displays an error message. Syntax refers to the structure of a program and the rules
about that structure. For example, parentheses have to come in matching pairs, so
(1 + 2)
is legal, but 8) is a syntax error.
In English, readers can tolerate most syntax errors, which is why we can read the
poetry of e. e. cummings without spewing error messages. Python is not so forgiving.


4
The Way of the Program
If there is a single syntax error anywhere in your program, Python will display an
error message and quit, and you will not be able to run your program. During the
first few weeks of your programming career, you will probably spend a lot of time
tracking down syntax errors. As you gain experience, you will make fewer errors and
find them faster.
1.3.2
Runtime Errors
The second type of error is a runtime error, so called because the error does not
appear until after the program has started running. These errors are also called
exceptions because they usually indicate that something exceptional (and bad) has
happened.
Runtime errors are rare in the simple programs you will see in the first few chapters,
so it might be a while before you encounter one.
1.3.3
Semantic Errors
The third type of error is the semantic error. If there is a semantic error in your
program, it will run successfully in the sense that the computer will not generate any
error messages, but it will not do the right thing. It will do something else. Specifically,
it will do what you told it to do.
The problem is that the program you wrote is not the program you wanted to write.
The meaning of the program (its semantics) is wrong. Identifying semantic errors
can be tricky because it requires you to work backward by looking at the output of
the program and trying to figure out what it is doing.
1.3.4
Experimental Debugging
One of the most important skills you will acquire is debugging. Although it can
be frustrating, debugging is one of the most intellectually rich, challenging, and
interesting parts of programming.
In some ways, debugging is like detective work. You are confronted with clues, and
you have to infer the processes and events that led to the results you see.
Debugging is also like an experimental science. Once you have an idea about what is
going wrong, you modify your program and try again. If your hypothesis was correct,
then you can predict the result of the modification, and you take a step closer to a
working program. If your hypothesis was wrong, you have to come up with a new
one. As Sherlock Holmes pointed out, “When you have eliminated the impossible,
whatever remains, however improbable, must be the truth” (A. Conan Doyle, The
Sign of Four).
For some people, programming and debugging are the same thing. That is, pro-
gramming is the process of gradually debugging a program until it does what you
want. The idea is that you should start with a program that does something and make


1.4 Formal and Natural Languages
5
small modifications, debugging them as you go, so that you always have a working
program.
For example, Linux is an operating system that contains thousands of lines of code,
but it started out as a simple program Linus Torvalds used to explore the Intel 80386
chip. According to Larry Greenfield, “One of Linus’s earlier projects was a program
that would switch between printing AAAA and BBBB. This later evolved to Linux”
(The Linux Users’ Guide Beta Version 1).
Later chapters will make more suggestions about debugging and other programming
practices.

tải về 1.38 Mb.

Chia sẻ với bạn bè của bạn:
1   2   3   4   5   6   7   8   9   ...   83




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương