19-12-2012, 05:41 PM
How to Think Like a Computer Scientist
1How to Think Like.pdf (Size: 1.5 MB / Downloads: 52)
The way of the program
The goal of this book is to teach you to think like a computer scientist. I
like the way computer scientists think because they combine some of the best
features of Mathematics, Engineering, and Natural Science. Like mathemati-
cians, computer scientists use formal languages to denote ideas (specically
computations). Like engineers, they design things, assembling components
into systems and evaluating tradeos among alternatives. Like scientists,
they observe the behavior of complex systems, form hypotheses, and test
predictions.
The single most important skill for a computer scientist is problem-solving.
By that I mean the ability to formulate problems, think creatively about
solutions, and express a solution clearly and accurately. As it turns out,
the process of learning to program is an excellent opportunity to practice
problem-solving skills. That's why this chapter is called \The way of the
program."
On one level, you will be learning to program, which is a useful skill by itself.
On another level you will use programming as a means to an end. As we go
along, that end will become clearer.
What is a programming language?
The programming language you will be learning is Java, which is relatively
new (Sun released the rst version in May, 1995). Java is an example of a
high-level language; other high-level languages you might have heard of
are Python, C or C++, and Perl.
As you might infer from the name \high-level language," there are also low-
level languages, sometimes called machine language or assembly language.
Loosely-speaking, computers can only run programs written in low-level lan-
guages. Thus, programs written in a high-level language have to be trans-
lated before they can run. This translation takes time, which is a small
disadvantage of high-level languages.
What is a program?
A program is a sequence of instructions that species how to perform a com-
putation1. The computation might be something mathematical, like solving
a system of equations or nding the roots of a polynomial, but it can also be
a symbolic computation, like searching and replacing text in a document or
(strangely enough) compiling a program.
What is debugging?
For whimsical reasons, programming errors are called bugs and the process
of tracking them down and correcting them is called debugging.
There are a three kinds of errors that can occur in a program, and it is useful
to distinguish them to track them down more quickly.
Syntax errors
The compiler can only translate a program if the program is syntactically
correct; otherwise, the compilation fails and you will not be able to run your
program. Syntax refers to the structure of your program and the rules about
that structure.
For example, in English, a sentence must begin with a capital letter and end
with a period. this sentence contains a syntax error. So does this one
For most readers, a few syntax errors are not a signicant problem, which is
why we can read the poetry of e e cummings without spewing error messages.
Compilers are not so forgiving. If there is a single syntax error anywhere in
your program, the compiler will print an error message and quit, and you
will not be able to run your program.
To make matters worse, there are more syntax rules in Java than there are in
English, and the error messages you get from the compiler are often not very
helpful. During the rst 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 nd them faster.
Run-time errors
The second type of error is a run-time error, so-called because the error does
not appear until you run the program. In Java, run-time errors occur when
the interpreter is running the byte code and something goes wrong.
Java tends to be a safe language, which means that the compiler catches a
lot of errors. So run-time errors are rare, especially for simple programs.
In Java, run-time errors are called exceptions, and in most environments
they appear as windows or dialog boxes that contain information about what
happened and what the program was doing when it happened. This infor-
mation is useful for debugging.
Logic errors and semantics
The third type of error is the logic or semantic error. If there is a logic error
in your program, it will compile and run without generating error messages,
but it will not do the right thing. It will do something else. Specically, 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 semantics, or meaning of the program, are wrong. Identifying
logic errors can be tricky because you have to work backwards, looking at
the output of the program and trying to gure out what it is doing.
Experimental debugging
One of the most important skills you will acquire in this class is debugging.
Although debugging can be frustrating, it is one of the most interesting,
challenging, and valuable parts of programming.
Debugging is like detective work. You are confronted with clues and you
have to infer the processes and events that lead to the results you see.
Debugging is also like an experimental science. Once you have an idea what
is going wrong, you modify your program and try again. If your hypothesis
was correct, then you can predict the result of the modication, and you
take a step closer to a working program.