The practice of programming


Chapter 4 Interfaces

A good programmer should always be good at designing. The essence of design is to balance competing goals and constraints. When we do programming, we need to design a friendly, portable and flexible interface.

Among the issues to be worked out in a design are:

  interfaces,  information hiding,  resource management,  error handling

These factors are not separated, but combined.

So, how to do a good designer? On the other word, how to handle the issues above perfectly?

The first step is to construct a general frame, leaving out the details. Then we need to test it by thinking of every possible situation, and improve it. By the way, it is essential for our frame to work in changeable environments. In this circumstance, we need specifications to constraint our work. Generally speaking, specifications had better be worked out in advance.

To prosper an interface must be well suited for its task-simple, general, regular,predictable, robust-and it must adapt gracefully as its users and its implementation change.

  • Hide implementation details
  • Avoid global variables
  • Don't reach behind the user's back
  • Do the same thing the same way everywhere

About Resource Management:

  • Initializationma
  • Maintaining state
  • Sharing and copying
  • Cleaning up (Free a resource in the same layer that allocated it)

To avoid problems, it is necessary to write code that is reentrant,  which means that it works regardless of the number of simultaneous executions.

The text of error messages, prompts, and dialog boxes should state the form of valid input.

Charpter 5 Debugging

  • Good Clues
  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
  • No Clues

  1. Make the bug reproducible
  2. Divide and conquer
  3. Study the numerology of failures
  4. Display output to localize your search
  5. Write self-checking code
  6. Write a logfile
  7. Draw a picture
  8. Use tools
  9. Keep records
  • Last Resorts

    This may be  the time to use a good debugger to step through the program.

  • Non-reproducible Bugs

  1. Check whether all variables have been initialized
  2. If the bug changes behavior or even disappears when debugging code is added, it may be a memory allocation error

Chapter 6 Testing

  • Test as You Write the Code

  1. Test code at its boundaries
  2. Test pre- and post-conditions
  3. Use assertions
  4. Program defensively
  5. Check error returns
  • Systematic Testing
  1. Test incrementally
  2. Test simple parts first
  3. Know what output to expect
  4. Verify conservation properties
  5. Compare independent implementations
  6. Measure test coverage
  • Test Automation

  1. Automate regression testing
  2. Create self-contained tests
  • Test Scaffolds

  • Stress Tests

  • Tips for Testing

  1. Programs should check array bounds (if the language doesn't do it for them), but the checking code might not be tested if the array sizes are large compared to typical input.
  2. Make the hash function return a constant, so every elemen1 gets installed in the same hash bucket.
  3. Write a version of your storage allocator that intentionally fails early, to test your code for recovering from out-of-memory errors.
  4. Before you ship your code. disable testing limitations that will affect performance.
  5. Initialize arrays and variables with some distinctive value, rather than the usual default of zero; then if you access out of bounds or pick up an uninitialized variable, you are more likely to notice it.
  6. Vary your test cases
  7. Provide ways to make the amount and type of output controllable when a program is run; extra output can help during testing.
  8. Test on multiple machines, compilers, and operating systems.

Chapter 7 Performance

Before changing a program to make it  faster, be certain that it really is too slow, and use timing tools and profilers to discover where the time is going.

In any case, times have changed, and both main memory and secondary storage are amazingly cheap. Thus the first approach to optimizing space should be the same as to improving speed: don't  bother.

When you're trying to improve the speed or space consumption of a program, it's a good idea to make up some benchmark tests and problems so you can estimate and keep track of performance for yourself.

Chapter 8 Portability

Our message is this: try to write software that works within the intersection of the various standards, interfaces and environments it must accommodate. Don't fix every portability problem by adding special code; instead, adapt the software to work within the new constraints. Use abstraction and encapsulation to restrict and control unavoidable non-portable code. By staying within the intersection of constraints and by localizing system dependencies, your code will become cleaner and more general as it is ported.

  • Language

  1. Stick to the standard
  2. Program in the mainstream
  3. Be ware of language trouble spots......
  • Headers and Libraries

  1. Use standard libraries
  • Program Organization

  1. Use only features available everywhere
  2. Avoid conditional compilation
  • Isolation

  1. Localize system dependencies in separate files
  2. Hide system dependencies behind interfaces
  • Data Exchange

  1. Use text for data exch
  • Byte Order

  1. Use a fmed byte order for data exchange
  • Portability and Upgrade

  1. Change the name ifyou change the specification
  2. Maintain compatibility with existing programs and data
  • Internationalization

Chapter 9 Notation

  • Regular Expressions

   The best-known regular expression tool is the program grep.

   Unfortunately, not every system comes with grep  or an equivalent. Some systems include a regular expression    library, usually called regex  or regexp,  that you can use to write a version of grep.  If neither option is available,    it's easy to implement a modest subset of the full regular expression language.

  • Programs that Write Programs
  • Using Macros to Generate Code

  • Compiling on the Fly


Summary

The book generally introduces how to program practically, including the following topics:

  • Style
  • Algorithms and Data Structures
  • Design and Implementation
  • Interfaces
  • Debugging
  • Testing
  • Performance
  • Portability
  • Notation

It emphasizes the practical problems we may meet in the programming. I think the most important thing is also practice, and learn by doing. Before we do, we need a general structure, a plan, a standard to help us better maintain, transmit a program. When we do, we need a good style, algorithms, and interfaces. When we finished, or half-finished, we need test, debug to improve it.

Some details need more studying because of my narrow knowledge. I'll look back this book from time to time.

That's all.

Book Review of “The practice of programming” (Ⅳ)的更多相关文章

  1. Book Review of “The practice of programming” (Ⅲ)

    The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...

  2. Book Review of “The practice of programming” (Ⅱ)

    The practice of programming Chapter 2 Algorithms and Data Structures Searching sequential search (li ...

  3. Book Review of "The Practice of Programming" (Ⅰ)

    The Practice of Programming In the preface, the author illustrates four basic principles of programm ...

  4. 2015年第2本(英文第1本):《The Practice of Programming》

    2015年计划透析10本英文原著,最开始选定的第一本英文书是<Who Moved my Cheese>,可是这本书实在是太短.太简单了,总体的意思就是要顺应变化,要跳出自己的舒适区,全文不 ...

  5. net programming guid

    Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...

  6. FRP represents an intersection of two programming paradigms.

    FRP represents an intersection of two programming paradigms. Functional programming Functional progr ...

  7. [转]9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路

    1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...

  8. 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路

    1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...

  9. (转)9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路

    原文:http://www.cnblogs.com/figure9/archive/2013/01/09/2853649.html 1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也 ...

随机推荐

  1. jquery取iframe中元素

    采取方法: $("#iframe_path").contents().find(".select_path_hide").val(); DOM方法:父窗口操作I ...

  2. Java Cache

    Ehcache http://www.ehcache.org/ Memcached http://memcached.org/ Server http://blog.couchbase.com/mem ...

  3. iOS-@2x,@3x是什么意思

    当我们在公司使用UI给出的图片时候,xxx.png,xxx@2x.png,xxx@3x.png的时候,不知道分别代表着什么! 本人也是菜鸟一枚,全凭自己尝试理解而已,在尝试中得出下面的结论: xxx. ...

  4. 大数据技术大合集:Hadoop家族、Cloudera系列、spark、storm【转】

    大数据我们都知道hadoop,可是还会各种各样的技术进入我们的视野:Spark,Storm,impala,让我们都反映不过来.为了能够更好 的架构大数据项目,这里整理一下,供技术人员,项目经理,架构师 ...

  5. linux一台机器文件传到另一台机器上

    登录一台机器35.73: scp -P 端口 要传的文件 user@xxx.xxx.xxx.xxx:/目标文件夹/ 例子 :scp -r -P3561 /home/ismp/build/app/bec ...

  6. zookeeper基本概念及原理

    zookeeper是一个分布式的,开源的分布式应用程序,该程序主要用于管理其他分布式应用程序.其他分布式应用程序可以基于zookeeper实现数据同步,配置维护和命名服务等等.zookeeper是Ha ...

  7. Redisson教程

    Redisson入门 Author:Ricky  Date:2017-04-24 Redisson概述 Redisson是架设在Redis基础上的一个Java驻内存数据网格(In-Memory Dat ...

  8. ubuntu 安装低版本firefox

    firefox 57以后很多插件不支持了,ubuntu16自带火狐版本59,想换回56. 1.下载想换回的版本 https://ftp.mozilla.org/pub/mozilla.org/fire ...

  9. Spoken English Practice(I won't succumb to you, not ever again)

    绿色:连读:                  红色:略读:               蓝色:浊化:               橙色:弱读     下划线_为浊化 口语蜕变(2017/6/28) ...

  10. 微信公众号 openId 支付 php中file_get_contents与curl性能比较分析

    w http://www.jb51.net/article/57238.htm