- 原书名：The Practice of Programming
- 原出版社： Addison-Wesley
With the same insight and authority that made their book The Unix Programming Environment a classic, Brian Kernighan and Rob Pike have written The Practice of Programming to help make individual programmers more effective and productive.
The practice of programming is more than just writing code. Programmers must also assess tradeoffs,Choose among design alternatives, debug and test, improve performance, and maintain software written bythemselves and others. At the same time, they must be concerned with issues like compatibility robustness,and reliability while meeting specifications.
1.2 Expressions and Statements
1.3 Consistency and Idioms
1.4 Function Macros
1.5 Magic Numbers
1.7 Why Bother?
Chapter 2: Algorithms and Data Structures
2.4 A Java Quicksort
2.6 Growing Arrays
2.9 Hash Tables
Chapter 3: Design and Implementation
wasted a lot of time coding the wrong algorithm?
used a data structure that was much too complicated?
tested a program but missed an obvious problem?
spent a day looking for a bug you should have found in five minutes?
needed to make a program run three times faster and use less memory?
struggled to move a program from a workstation to a PC or vice versa?
tried to make a modest change in someone else's program?
rewritten a program because you couldn't understand it?
Was it fun?
These things happen to programmers all the time. But dealing with such problems is often harder than it should be because topics like testing, debugging, portability,performance, design altematives, and style--the practice of programming--are not usually the focus of computer science or programming courses. Most programmers learn them haphazardly as their experience grows, and a few never learn them at all.In a world of enormous and intricate interfaces, constantly changing tools and languages and systems, and relentless pressure for more of everything, one can lose sight of the basic principles--simplicity, clarity, generality--that form the bedrock of good software. One can also overlook the value of tooIs and notations that mechanize some of software creation and thus enlist the computer in its own programming.
Our approach in this book is based on these underlying, interrelated principles,which apply at all levels of computing. These include simplicity, which keeps programs short and manageable; clarity, which makes sure they are easy to understand,for people as well as machines; generality, which means they work well in a broad range of situations and adapt well as new situations arise; and automation, which lets the machine do the work fOr us, freeing us from mundane tasks. By looking at computer programming in a variety of languages, from algorithms and data structures through design, debugging, testing, and performance improvement, we can illustrate universal engineering concepts that are independent of language, operating system, or programIning paradigm.
This book comes from many years of experience writing and maintaining a lot of sotiware, teaching programming courses, and working with a wide variety of programmers. We want to share lessons about practical issues, to pass on insights from our experience, and to suggest ways for progran1mers of a1I levels to be more proficient and productivc.
We are writing for several ki11ds of readers. If you are a student who has taken a programming course or two and would like to be a better programmer, this book will expand on some of the topics fOr which there wasn't enough time in school. If you write programs as part of your work. but in support of other activities rather than as the goal in itself, the information will help you to program more effectively. If you are a professional programmer who didn't get enough exposure to such topics in school or who wotlId like a refresher, or if you are a software manager who wants to guide your staff in the right direction, the material here should be of value.
We hope that the advice will help you to write better programs. The only prerequisite is that you have done some programming, preferably in C, C++ or Java. Of course the more experience you have, the easier it will be; nothing can take you from neophyte to expert in 21 days. Unix and Linux programmers will find some of the examples more famiIiar than will those who have used only Windows and Macintosh systems, but programmers from any environrnent shou1d discover things to make theirlives easier.
The presentation is organized into nine chapters, each focusing on one major aspect of programming practice.
Chapter 1 discusses programming style. Good style is so important to good programming that we have chosen to cover it first. Well-written programs are better than badly--written ones--they have fewer errors and are easier to debug and to modify-so it is important to think about style from the beginning. This chapter aIso introduces an important theme in good programming, the use of idioms appropriate to the language being used.
Algorithms and data structures, the topics of Chapter 2. are the core of the computer science curriculum and a rnajor part of programming courses. Since most readers will aIready be familiar with this material, our treatment is intended as a brief review of the handful of algorithms and data structures that show up in almost every program. More complex algorithms and data structures usual1y evolve from these building blocks, so one shouId master the basics.
Chapter 3 describes the design and implementation of a small program that illus-trates algorithm and data structure issues in a realistic setting. The program is implemented in five languages; cornparing the versions shows how the same data structures are handled in each, and how expressiveness and pertbrmance vary across a spectrum of languages.
Interfaces between users, programs, and parts of programs are fundamentaI in programming and much of the success of software is determined by how well interfaces are designed and implemented. Chapter 4 shows the evolution of a small library for parsing a widely used data format. Even though the example is small, it illustrates many of the concerns of interface design f abstraction, information hiding, resource management, and error handling.