0%

编程之道 卷四 编码

Book 4 - Coding

Thus spake the master programmer:
“A well-written program is its own heaven; a poorly-written program is its own hell.”

编程大师如是说:
“写得好的程序是它自己的天堂;写的糟的程序是自己的地狱。”

4.1

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity.
一个程序应当轻便而敏捷,它的子程序犹如串在一条线上的珍珠。程序的意图应当始终如一。程序的规模不应过短,也不宜过长;不该有不必要的循环,也不需要多余的变量;不应缺少结构化,也不能过于僵硬。

A program should follow the ‘Law of Least Astonishment’. What is this law? It is simply that the program should always respond to the user in the way that astonishes him least.
一个程序应当遵循“最小惊讶原则”。这个原则又是什么?它就是说用户在使用程序时,感到的惊讶程度应当最小。

A program, no matter how complex, should act as a single unit. The program should be directed by the logic within rather than by outward appearances.
一个程序,无论多么复杂,都应当作为一个独立的单元。程序应当被其内部的逻辑所引导,而不应该是外部表现。

If the program fails in these requirements, it will be in a state of disorder and confusion. The only way to correct this is to rewrite the program.
如果程序不能够满足其需求,将会陷入紊乱状态。这时唯一的解决方案,就是重写这个程序。

4.2

A novice asked the master: “I have a program that sometime runs and sometimes aborts. I have followed the rules of programming, yet I am totally baffled. What is the reason for this?”
一位新手问大师:“我有一个程序运行起来偶尔正常偶尔不正常。我已经遵循了编程的规范,这样我就不明白了。这到底是什么原因引起的呢?”

The master replied: “You are confused because you do not understand Tao. Only a fool expects rational behavior from his fellow humans. Why do you expect it from a machine that humans have constructed? Computers simulate determinism; only Tao is perfect.
大师回复:“你之所以困惑,是因为你没有理解‘道’。只有笨蛋才会相信遵循别人说的规范就能让程序按预期地去执行。为什么你会相信人类所造的机器会产生预期的结果呢?计算机不过是在模拟宿命论;只有‘道’才是完美的。

“The rules of programming are transitory; only Tao is eternal. Therefore you must contemplate Tao before you receive enlightenment.”
“编程的规则都是暂时的;只有‘道’才是永恒的。因此,你在接受启发之前,应当先去沉思一下‘道’。”

“But how will I know when I have received enlightenment?” asked the novice.
“但是我怎么知道我什么时候会接受启发呢?”新手继续问道。

“Your program will then run correctly,” replied the master.
“当你的程序正确运行时,”大师回答。

4.3

A master was explaining the nature of Tao of to one of his novices. “The Tao is embodied in all software - regardless of how insignificant,” said the master.
大师向他的一位新学生讲解道的本质。“道蕴含于一切软件中——不论它多么微不足道”,大师说道。

“Is the Tao in a hand-held calculator?” asked the novice.
“那么,道是否存在于手持计算器中?”,学生问道。

“It is,” came the reply.
“是的”,大师回答。

“Is the Tao in a video game?” continued the novice.
“那么,道是否存在于一个视频游戏中?”,学生接着问。

“It is even in a video game,” said the master.
“对,即使是个游戏”,大师回答。

“And is the Tao in the DOS for a personal computer?”
“那,道是否存在于PC机上的DOS中?”

The master coughed and shifted his position slightly. “The lesson is over for today,” he said.
大师咳嗽了几声,轻轻挪动了一下位置,回答道:“今天的课就上到这里”。

4.4

Prince Wang’s programmer was coding software. His fingers danced upon the keyboard. The program compiled without an error message, and the program ran like a gentle wind.
普林斯·王手下的程序员正在编程。他的手指在键盘上飞舞。程序在编译时没有报任何错误信息,而程序在运行时如同行云流水。

“Excellent!” the Prince exclaimed, “Your technique is faultless!”
“太棒了!”普林斯称赞道:“你的技术真是完美无缺!”

“Technique?” said the programmer turning from his terminal, “What I follow is Tao - beyond all techniques! When I first began to program I would see before me the whole problem in one mass. After three years I no longer saw this mass. Instead, I used subroutines. But now I see nothing. My whole being exists in a formless void. My senses are idle. My spirit, free to work without plan, follows its own instinct. In short, my program writes itself. True, sometimes there are difficult problems. I see them coming, I slow down, I watch silently. Then I change a single line of code and the difficulties vanish like puffs of idle smoke. I then compile the program. I sit still and let the joy of the work fill my being. I close my eyes for a moment and then log off.”
“技术?”这位程序员从他的终端转过身来,“我追随的是道——它超乎了一切技术!当我刚开始编程时,我看到的是一大块的程序。三年之后,我看到的不再是一大块,而是子过程组成的。现在,我什么都看不到了。我的存在也只是无形虚空。我感到很悠闲,我不需要计划,自由地去工作,如同遵循着我的灵魂本性。简而言之,就像是我的程序自己在写那样。当然,有时也会遇到一些难题。看到它们,我会放慢速度,静静观察。接着我改了一行代码,问题瞬间烟消云散。然后我编译这个程序,并坐着上享受工作带来的喜悦充实着我的存在。我闭了会双眼,然后退出系统。”

Prince Wang said, “Would that all of my programmers were as wise!”
普林斯·王感叹道:“什么时候我的所有程序员才能够都达到这样的境界!”