《编程之道》 (Tao of Programming) 十三则「精选」
其一
The Grand Master Turing once dreamed that he was a machine. When he awoke, he exclaimed: “I don’t know whether I am Turing dreaming that I am a machine, or a machine dreaming that I am Turing!”
昔日,图灵大师梦见自己变成了一台机器,他醒来后惊呼: 「我不确定我是图灵梦见自己变成了机器,还是我是机器梦见自己变成了图灵!」
其二
Thus spake the Master Programmer: “When a program is being tested, it is too late to make design changes.”
编程大师如是说:「待程序进入测试阶段,任何设计上的修改都太晚了。」
其三
There once was a Master Programmer who wrote unstructured programs. A novice programmer, seeking to imitate him, also began to write unstructured programs. When the novice asked the Master to evaluate his progress, the Master criticized him for writing unstructured programs, saying, “What is appropriate for the Master is not appropriate for the novice. You must understand Tao before transcending structure.”
从前,有位编程大师擅长编写没有结构的程序。他的弟子模仿他,也开始编写没有结构的程序。当弟子请大师评价他的程序,大师批评他:「适合大师的不一定适合初学者。在超越结构之前,你必须先领悟道。」
其四
Thus spake the Master Programmer: “A well-written program is its own Heaven; a poorly-written program is its own Hell.”
编程大师如是说:「优秀的程仿佛置身天堂,差劲的程序如同自取灭亡。」
其五
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 nor 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 users in the way that least astonishes them.
程序应当遵循「最少惊讶原则」。这个原则很简单:程序返回的结果永远符合用户的预期。
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.
如果程序不满足以上要求,它将陷入无序与混乱。唯一的修复办法就是重写程序。
其六
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.
唯有傻子才会期望世人举止理性。你又如何期望世人建造的机器能有理性的行为呢?计算机只能模拟决定论;世间唯有道是完美的。编程无常法,唯道恒久远。在获得启示之前,你必须深思道。
其七
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 a 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.
我追求的是道——超越所有技术的道!我刚开始编程时,眼前的问题堆积成团、杂乱如麻。三年后,我再也没见过这样的问题,因为我学会了子程序。如今,我什么也看不见了。我的存在变得虚无,我感受不到任何事物。我的精神不受约束,全凭本能自由工作。换句话说,我写程序时,代码是一行行自己显现的。当然,有时我会碰到一些棘手的问题。我看着这些问题出现,我慢下来,静静地看着。然后我改动一行代码,问题像轻烟般自己消失了。我开始编译程序。我静静地坐着,感受着快乐盈满身心。我闭目冥想片刻,然后退出系统。
其八
Software rots if not used.
软件没人用,神奇也会化为腐朽。
其九
Does a good farmer neglect a crop he has planted?
好的农民会随意对待自己种的庄稼吗?
Does a good teacher overlook even the most humble student?
好的老师会忽视自己最谦虚的学生吗?
Does a good father allow a single child to starve?
好的父亲会舍得让任何一个孩子挨饿吗?
Does a good programmer refuse to maintain his code?
好的程序员会拒绝维护自己的代码吗?
其十
Why are programmers non-productive? Because their time is wasted in meetings.
为什么程序员没有工作效率?因为要开的会议没完没了。
Why are programmers rebellious? Because the management interferes too much.
为什么程序员不听指挥?因为管理层管得太多太深。
Why are the programmers resigning one by one? Because they are burnt out.
为什么程序员一个接一个辞职?因为他们累得心力交瘁。
Having worked for poor management, they no longer value their jobs.
在管理不善的部门工作,他们再也看不到自己工作的价值。
其十一
In the East there is a shark which is larger than all other fish. It changes into a bird whose wings are like clouds filling the sky.
东方有鱼,其名为鲲。鲲之大,绵绵若千里之远;化而为鸟,其名为鹏。鹏之翼,皇皇若垂天之云。
When this bird moves across the land, it brings a message from Corporate Headquarters. This message drops into the midst of the programmers, like a seagull making its mark upon the beach. Then the bird mounts on the wind and, with the blue sky at its back, returns home.
这只大鸟从东边飞来,带来了企业总部的消息。如海鸥掠过沙滩,大鸟将消息撒播在了程序员之间。随后它背负青天,扶摇直上,缓缓归去。
The novice programmer stares in wonder at the bird, for he understands it not. The average programmer dreads the coming of the bird, for he fears its message. The Master Programmer continues to work at his terminal, unaware that the bird has come and gone.
初级程序员惊奇地盯着大鸟,因为他不明白这意味着什么。中级程序员害怕大鸟的到来,因为它带来了总部的消息。而编程大师一直在电脑前工作,完全没注意到大鸟来过。
其十二
Thus spake the Master Programmer: “Without the wind, the grass does not move. Without software hardware is useless.”
编程大师如是说: 「没有风,草就不会动;没有软件,硬件就没用。」
其十三
Hardware met Software on the road to Changtse. Software said: “You are Yin and I am Yang. If we travel together, we will become famous and earn vast sums of money.” And so they set forth together, thinking to conquer the world.
在通往长江的路上,硬件和软件相遇了。软件说:「你是阴,我是阳。我们结伴而行,日后一定会出名、赚大钱。」于是他们一同出发,想要征服世界。
Presently, they met Firmware, who was dressed in tattered rags and hobbled along propped on a thorny stick. Firmware said to them: “The Tao lies beyond Yin and Yang. It is silent and still as a pool of water. It does not seek fame; therefore, nobody knows its presence. It does not seek fortune, for it is complete within itself. It exists beyond space and time.”
不久,他们碰见了固件。固件衣衫褴褛,拄着一根拐杖,步履蹒跚地走着。固件对他们说:「道在阴阳之外。它似一潭止水,寂静无声。它不求闻达,所以没人知道它在哪儿。它不求财富,因为它自身已完整无缺。它独立于时空存在。」