推荐:《The Practice Of Programming》

方工 2002-12-01 06:06:15
Title The Practice Of Programming
Authors Brian W. Kernighan and Rob Pike

Excerpts from "The Practice Of Programming"
(From the Preface)
Have you ever...

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 alternatives, 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.



...全文
92 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kenryHuang 2003-01-01
  • 打赏
  • 举报
回复
这本书是我最喜欢的一本程序设计书。
借用《人月神话》上的一句话,这本书就是我买的计算机书籍中
的“神品”
wangxj0600 2002-12-31
  • 打赏
  • 举报
回复
up
vata 2002-12-07
  • 打赏
  • 举报
回复
呵呵.
方工 2002-12-01
  • 打赏
  • 举报
回复
Appendix: Collected Rules
Each truth that I discovered became a rule that served me afterwards in the discovery of others.

René Descartes, Le Discours de la Méthode

Several chapters contain rules or guidelines that summarize a discussion. The rules are collected here for easy reference. Bear in mind that each was presented in a context that explains its purpose and applicability.

Style
Use descriptive names for globals, short names for locals.

Be consistent.

Use active names for functions.

Be accurate.

Indent to show structure.

Use the natural form for expressions.

Parenthesize to resolve ambiguity.

Break up complex expressions.

Be clear.

Be careful with side effects.

Use a consistent indentation and brace style.

Use idioms for consistency.

Use else-ifs for multi-way decisions.

Avoid function macros.

Parenthesize the macro body and arguments.

Give names to magic numbers.

Define numbers as constants, not macros.

Use character constants, not integers.

Use the language to calculate the size of an object.

Don't belabor the obvious.

Comment functions and global data.

Don't comment bad code, rewrite it.

Don't contradict the code.

Clarify, don't confuse.

Interfaces
Hide implementation details.

Choose a small orthogonal set of primitives.

Don't reach behind the user's back.

Do the same thing the same way everywhere.

Free a resource in the same layer that allocated it.

Detect errors at a low level, handle them at a high level.

Use exceptions only for exceptional situations.

Debugging
Look for familiar patterns.

Examine the most recent change.

Don't make the same mistake twice.

Debug it now, not later.

Get a stack trace.

Read before typing.

Explain your code to someone else.

Make the bug reproducible.

Divide and conquer.

Study the numerology of failures.

Display output to localize your search.

Write self-checking code.

Write a log file.

Draw a picture.

Use tools.

Keep records.

Testing
Test code at its boundaries.

Test pre- and post-conditions.

Use assertions.

Program defensively.

Check error returns.

Test incrementally.

Test simple parts first.

Know what output to expect.

Verify conservation properties.

Compare independent implementations.

Measure test coverage.

Automate regression testing.

Create self-contained tests.

Performance
Automate timing measurements.

Use a profiler.

Concentrate on the hot spots.

Draw a picture.

Use a better algorithm or data structure.

Enable compiler optimizations.

Tune the code.

Don't optimize what doesn't matter.

Collect common subexpressions.

Replace expensive operations by cheap ones.

Unroll or eliminate loops.

Cache frequently-used values.

Write a special-purpose allocator.

Buffer input and output.

Handle special cases separately.

Precompute results.

Use approximate values.

Rewrite in a lower-level language.

Save space by using the smallest possible data type.

Don't store what you can easily recompute.

Portability
Stick to the standard.

Program in the mainstream.

Beware of language trouble spots.

Try several compilers.

Use standard libraries.

Use only features available everywhere.

Avoid conditional compilation.

Localize system dependencies in separate files.

Hide system dependencies behind interfaces.

Use text for data exchange.

Use a fixed byte order for data exchange.

Change the name if you change the specification.

Maintain compatibility with existing programs and data.

Don't assume ASCII.

Don't assume English.

From The Practice of Programming by Brian W. Kernighan and Rob Pike; Addison-Wesley, 1999; ISBN 0-201-61586-X
方工 2002-12-01
  • 打赏
  • 举报
回复
Table of Contents
Preface

Chapter 1: Style
1.1 Names
1.2 Expressions and Statements
1.3 Consistency and Idioms
1.4 Function Macros
1.5 Magic Numbers
1.6 Comments
1.7 Why Bother?
Chapter 2: Algorithms and Data Structures
2.1 Searching
2.2 Sorting
2.3 Libraries
2.4 A Java Quicksort
2.5 O-Notation
2.6 Growing Arrays
2.7 Lists
2.8 Trees
2.9 Hash Tables
2.10 Summary
Chapter 3: Design and Implementation
3.1 The Markov Chain Algorithm
3.2 Data Structure Alternatives
3.3 Building the Data Structure in C
3.4 Generating Output
3.5 Java
3.6 C++
3.7 Awk and Perl
3.8 Performance
3.9 Lessons

Chapter 4: Interfaces
4.1 Comma-Separated Values
4.2 A Prototype Library
4.3 A Library for Others
4.4 A C++ Implementation
4.5 Interface Principles
4.6 Resource Management
4.7 Abort, Retry, Fail?
4.8 User Interfaces

Chapter 5: Debugging
5.1 Debuggers
5.2 Good Clues, Easy Bugs
5.3 No Clues, Hard Bugs
5.4 Last Resorts
5.5 Non-reproducible Bugs
5.6 Debugging Tools
5.7 Other People's Bugs
5.8 Summary

Chapter 6: Testing
6.1 Test as You Write the Code
6.2 Systematic Testing
6.3 Test Automation
6.4 Test Scaffolds
6.5 Stress Tests
6.6 Tips for Testing
6.7 Who Does the Testing?
6.8 Testing the Markov Program
6.9 Summary

Chapter 7: Performance
7.1 A Bottleneck
7.2 Timing and Profiling
7.3 Strategies for Speed
7.4 Tuning the Code
7.5 Space Efficiency
7.6 Estimation
7.7 Summary

Chapter 8: Portability
8.1 Language
8.2 Headers and Libraries
8.3 Program Organization
8.4 Isolation
8.5 Data Exchange
8.6 Byte Order
8.7 Portability and Upgrade
8.8 Internationalization
8.9 Summary

Chapter 9: Notation
9.1 Formatting Data
9.2 Regular Expressions
9.3 Programmable Tools
9.4 Interpreters, Compilers, and Virtual Machines
9.5 Programs that Write Programs
9.6 Using Macros to Generate Code
9.7 Compiling on the Fly

Epilogue
Appendix: Collected Rules
Index

590

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧