The art of UNIX programming精彩片段

一个好习惯是很必要的。吾日三省吾身,到了晚上,应该想想今天干了什么事,有什么可以改进的地方,什么地方做得还不够。但是一思考就控制不住要写一些东西。由于最近在看The art of UNIX programming,所以贴一些认为很有道理的片段上来。

This is the Unix philosophy. Write programs that do one thing and do it well.Write programs to work together. Write programs to handle text streams.because that is a universal interface.

(下面每一条都有必要详细阅读,由于其中蕴含了很多道理需要具体解说,如果感兴趣建议阅读原书)
1. Rule of Modularity: Write simple parts connected by clean interfaces.
2. Rule of Composition: Design programs to he connected to other programs.
3. Rule of Clarity: Clarity is better than cleverness.
4. Rule of Simplicity: Design for simplicity: add complexity only where you must.
5. Rule of Transparency: Design for visibility to make inspection and debugging easier.
6. Rule of Robustness: Robustness is the child of transparency and simplicity.
7. Rule of Least Surprise: In interface design. always do the least surprising thing.
8. Rule of Repair: When you must fail, fail noisily and as soon as possible.
9. Rule of Economy: Programmer time is expensive: conserve it in preference to machine time.
10. Rule of Generation: Avoid hand-hacking: write programs to write programs when you can.
11. Rule of Reresentation: Use smart data so program logic can be stupid and robust.
12. Rule of Separation: Separate policy from mechanism separate interfaces from engines.
13. Rule of Optimization: Prototype before polishing. Get it working before you optimize it.
14. Rule of Diversity: Distrust all claims for "one true way"
15. Rule of Extensibility: Design for the future, because it will be here sooner than you think.

UNIX的这种设计模式已经被证明是有效的(关键是Productive)。和自己平时的行为相比对,不禁汗颜。它把计算机从业者的心理描述得很透彻,尤其是一些危害生产力的行为,比如下面这段描述的:
There are many pressures which tend to make programs more complicated (and therefore more expensive and buggy). One is technical machismo. Programmers are bright people who are (justly) proud of their ability to handle complexity and juggle abstractions. Often they compete with their peers to see who can build the most intricate and beautiful complexities. Just as often, their ability to design outstrips their ability to implement and debug. and the
result is expensive failure.

不难发现,UNIX与C++创立的是初衷一样,很强调development time而不是machine time. 它也反复强调一个时间复杂度小的算法在实际中往往并不是最优选择,因为
(1) 这样的算法复杂度常数往往很大,如最坏情况下O(nlogn)的排序算法一般会比最坏情况下O(n^2)的快排慢不少。而我们在实际应用中往往碰不到规模如此大的数据,使得这种“快的算法”能显现出它的优势来。
(2) 随着现在机器的速度越来越快,所谓的“快算法”往往也快不了多少,就算对于千万级别的数据快个几毫秒,对用户体验也没有丝毫影响。
(3) 这样的“快算法”往往非常复杂,实现难度大而且容易出错。维护成本高。
从这三个角度来看,选择一个时间复杂度低的算法往往不是最明智的选择。也许我们需要抛弃直觉的冲动,而在分析后作出切合实际的选择。

从以上分析可以看出,UNIX开发哲学强调实用高效而不是何种复杂的技巧或不切实际的规范。也许可以用这样的段落作为总结:
The Unix philosophy (like successful folk traditions in other engineering disciplines) is bottom-up. not top-down. It is pragmatic and grounded in experience. It is not to he found in official methods and standards, but rather in the implicit half-reflexive knowledge. The expertise that the Unix culture transmits. It encourages a sense of proportion and skepticism and shows both by havthg a sense of (often subversive) humor.

posted on 2010-02-15 00:32  grapeot  阅读(665)  评论(0编辑  收藏  举报

导航