1.3 How to Learn C++
The only way to learn how to program is to
write programs. You'll learn a lot more by writing
and debugging programs than you ever will by reading this book. This
book contains many programming exercises, and you should try to do as
many of them as possible. When doing the exercises, keep good
programming style in mind. Always comment
your programs, even if
you're doing the exercises only for yourself.
Commenting helps you organize your thoughts, and commenting your own
programs is good practice for when you go into the
"real world."
Don't let yourself be seduced by the idea that,
"I'm only writing these programs
for myself, so I don't need to comment
them." First of all, code that looks obvious to you
when you write it can often be confusing and cryptic when you revisit
it a week later. Writing comments also helps you organize your ideas.
(If you can write out an idea in English, you are halfway to writing
it in C++.)
Finally, programs tend to be around far longer than expected. I once
wrote a highly system-dependent program that was designed to work
only on the computer at Caltech. As I was the only one who would ever
use the program, it would print the following message if I got the
command line wrong:
?LSTUIT User is a twit
A few years later I was a student at Syracuse University. The chief
secretary at the School of Computer Science needed a program similar
to my Caltech listing program, so I adapted my program for her use.
Unfortunately, I had forgotten about my funny little error message.
Imagine how horrified I was when I came into the Computer Science
office and was accosted by the chief secretary. This lady had so much
power she could make the dean cringe. She looked at me and said,
"User is a twit, huh?" Luckily she
had a sense of humor, or I might not be here today.
Sprinkled throughout are not only examples of working programs (to
show you how to do things), but also examples of broken programs
where we ask you to go through the program and figure out
what's wrong. Often the problem is very subtle, such
as a misplaced semicolon or use of = instead of
==. These programs let you learn how to spot
mistakes in a small program. That way when you make similar mistakes
in a big program—and you will make
mistakes—you will be trained to spot them.
|