Book Review of “The practice of programming” (Ⅳ)
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
- 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
No Clues
- Make the bug reproducible
- Divide and conquer
- Study the numerology of failures
- Display output to localize your search
- Write self-checking code
- Write a logfile
- Draw a picture
- Use tools
- Keep records
- Last Resorts
This may be the time to use a good debugger to step through the program.
Non-reproducible Bugs
- Check whether all variables have been initialized
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
- Test code at its boundaries
- Test pre- and post-conditions
- Use assertions
- Program defensively
- Check error returns
- Systematic Testing
- Test incrementally
- Test simple parts first
- Know what output to expect
- Verify conservation properties
- Compare independent implementations
- Measure test coverage
Test Automation
- Automate regression testing
- Create self-contained tests
Test Scaffolds
Stress Tests
Tips for Testing
- 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.
- Make the hash function return a constant, so every elemen1 gets installed in the same hash bucket.
- Write a version of your storage allocator that intentionally fails early, to test your code for recovering from out-of-memory errors.
- Before you ship your code. disable testing limitations that will affect performance.
- 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.
- Vary your test cases
- Provide ways to make the amount and type of output controllable when a program is run; extra output can help during testing.
- 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
- Stick to the standard
- Program in the mainstream
- Be ware of language trouble spots......
Headers and Libraries
- Use standard libraries
Program Organization
- Use only features available everywhere
- Avoid conditional compilation
Isolation
- Localize system dependencies in separate files
- Hide system dependencies behind interfaces
Data Exchange
- Use text for data exch
Byte Order
- Use a fmed byte order for data exchange
Portability and Upgrade
- Change the name ifyou change the specification
- 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” (Ⅳ)的更多相关文章
- Book Review of “The practice of programming” (Ⅲ)
The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...
- Book Review of “The practice of programming” (Ⅱ)
The practice of programming Chapter 2 Algorithms and Data Structures Searching sequential search (li ...
- Book Review of "The Practice of Programming" (Ⅰ)
The Practice of Programming In the preface, the author illustrates four basic principles of programm ...
- 2015年第2本(英文第1本):《The Practice of Programming》
2015年计划透析10本英文原著,最开始选定的第一本英文书是<Who Moved my Cheese>,可是这本书实在是太短.太简单了,总体的意思就是要顺应变化,要跳出自己的舒适区,全文不 ...
- net programming guid
Beej's Guide to Network Programming Using Internet Sockets Brian "Beej Jorgensen" Hallbeej ...
- FRP represents an intersection of two programming paradigms.
FRP represents an intersection of two programming paradigms. Functional programming Functional progr ...
- [转]9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...
- 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也作为之前三个半月的求职的回顾. 首先说说我拿到的offer情况: 微软,3面->终面,搞定 百度,3面->终面,口头of ...
- (转)9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
原文:http://www.cnblogs.com/figure9/archive/2013/01/09/2853649.html 1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也 ...
随机推荐
- 涨姿势UWP源码——IsolatedStorage
前一篇涨姿势UWP源码分析从数据源着手,解释了RSS feed的获取和解析,本篇则会就数据源的保存和读取进行举例. 和之前的Windows Runtime一样,UWP采用IsolatedStorage ...
- iOS 修改textholder的颜色
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(, , , )]; textField.placeholde ...
- linux的bash与sh的区别
转自:https://zhidao.baidu.com/question/305415121.html https://zhidao.baidu.com/question/176780008.html ...
- Django 中的自定义分页标签
目录结构: 1.在应用下,migrations的同级目录创建templatetags目录(主要两个文件,包含__init__.py) 2.创建分页标签(pagetag.py) #!/usr/bin/e ...
- MySQL 索引设计概要
在关系型数据库中设计索引其实并不是复杂的事情,很多开发者都觉得设计索引能够提升数据库的性能,相关的知识一定非常复杂. 然而这种想法是不正确的,索引其实并不是一个多么高深莫测的东西,只要我们掌握一定的方 ...
- FineReport---数据集
三.文本数据集 文本数据集,就是指数据源是一系列的文本文件,而我们就是要将这些文件作为数据来源,用FineReport来呈现这些数据,并做相应的数据分析. 1)TXT 2)EXCEL 3) 动态的Ex ...
- powershell Start-Sleep
秒: Start-Sleep –s 10 ,毫秒) Start-Sleep –m 10000 语法 Start-Sleep [-seconds] <int> [<CommonPara ...
- Powershell Get Domain Group的几种方法
Group常见属性介绍: 一.Get-ADGroup获取群组(如下例循环获取群组的发送权限) #群组的发送权限info $groups=Get-ADGroup -filter * -SearchSco ...
- <2014 04 15> C++语言回顾精要(原创By Andrew)
C++语言回顾精要 <Visual C++程序设计>张岳新,这本书是很多学校的本科生C++教学用书,今天重新拿来翻了一遍.跟很多国人写的技术书籍一样,写书并不是为了让初学者看懂入门,而是为 ...
- WebService 入门
1. 远程调用技术 2. WebService 概述 WebService 是使用 Http 发送 SOAP 协议数据的一种远程调用技术; WebService 需要开发客户端; WebService ...