Fly With My Heart

誓言无声,人生无悔。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

The Practice of Programming
Brian Kernighan, Rob Pike

Style
  1. Use descriptive names for globals, short names for locals.
  2. Be consistent.
  3. Use active names for functions.
  4. Be accurate.
  5. Indent to show structure.
  6. Use the natural form for expressions.
  7. Parenthesize to resolve ambiguity.
  8. Break up complex expressions.
  9. Be clear.
  10. Be careful with side effects.
  11. Use a consistent indentation and brace style.
  12. Use idioms for consistency.
  13. Use else-ifs for multi-way decisions.
  14. Avoid function macros.
  15. Parenthesize the macro body and arguments.
  16. Give names to magic numbers.
  17. Define numbers as constants, not macros.
  18. Use character constants, not integers.
  19. Use the language to calculate the size of an object.
  20. Don't belabor the obvious.
  21. Comment functions and global data.
  22. Don't comment bad code, rewrite it.
  23. Don't contradict the code. Clarify, don't confuse.
Interfaces
  1. Hide implementation details.
  2. Choose a small orthogonal set of primitives.
  3. Don't reach behind the user's back.
  4. Do the same thing the same way everywhere.
  5. Free a resource in the same layer that allocated it.
  6. Detect errors at a low level, handle them at a high level.
  7. Use exceptions only for exceptional situations.
Debugging
  1. Look for familiar patterns.
  2. Examine the most recent change.
  3. Don't make the same mistake twice.
  4. Debug it now, not later.
  5. Get a stack trace.
  6. Read before typing.
  7. Explain your code to someone else.
  8. Make the bug reproducible.
  9. Divide and conquer.
  10. Study the numerology of failures.
  11. Display output to localize your search.
  12. Write self-checking code.
  13. Write a log file.
  14. Draw a picture.
  15. Use tools.
  16. Keep records.
Testing
  1. Test code at its boundaries.
  2. Test pre- and post-conditions.
  3. Use assertions.
  4. Program defensively.
  5. Check error returns.
  6. Test incrementally.
  7. Test simple parts first.
  8. Know what output to expect.
  9. Verify conservation properties.
  10. Compare independent implementations.
  11. Measure test coverage.
  12. Automate regression testing.
  13. Create self-contained tests.
Performance
  1. Automate timing measurements.
  2. Use a profiler.
  3. Concentrate on the hot spots.
  4. Draw a picture.
  5. Use a better algorithm or data structure.
  6. Enable compiler optimizations.
  7. Tune the code.
  8. Don't optimize what doesn't matter.
  9. Collect common subexpressions.
  10. Replace expensive operations by cheap ones.
  11. Unroll or eliminate loops.
  12. Cache frequently-used values.
  13. Write a special-purpose allocator.
  14. Buffer input and output.
  15. Handle special cases separately.
  16. Precompute results.
  17. Use approximate values.
  18. Rewrite in a lower-level language.
  19. Save space by using the smallest possible data type.
  20. Don't store what you can easily recompute.
Portability
  1. Stick to the standard.
  2. Program in the mainstream.
  3. Beware of language trouble spots.
  4. Try several compilers.
  5. Use standard libraries.
  6. Use only features available everywhere.
  7. Avoid conditional compilation.
  8. Localize system dependencies in separate files.
  9. Hide system dependencies behind interfaces.
  10. Use text for data exchange.
  11. Use a fixed byte order for data exchange.
  12. Change the name if you change the specification.
  13. Maintain compatibility with existing programs and data.
  14. Don't assume ASCII.
  15. Don't assume English.
posted on 2008-04-03 19:08  FlyWithMyHeart  阅读(627)  评论(0编辑  收藏  举报