1. 程式人生 > >《程式設計之道》(之前的那篇好象出錯了 重貼一次)

《程式設計之道》(之前的那篇好象出錯了 重貼一次)

The Tao Of Programming —— 程式設計之道
Translated By Geoffrey James


[小趙]翻譯整理

說明:
  本文是<<程式設計之道>> <<程式設計之禪>><<計算機時代的寓言>>其中的一冊, 由小趙翻譯整理, 另外, 這三冊書的完整電子文件由小趙根據市面上的<< 程式設計之道>>一書(包括了上面的三卷)錄入整理, 隨後將一起刊登(參見《程式設計之道》全譯文)。

Table of Contents 目錄

Book 1 -- The Silent Void
Book 2 -- The Ancient Masters
Book 3 -- Design
Book 4 -- Coding
Book 5 -- Maintenance
Book 6 -- Management
Book 7 -- Corporate Wisdom
Book 8 -- Hardware and Software
Book 9 -- Epilogue


--------------------------------------------------------------------------------


The Silent Void
Book One
Thus spake the master programmer:
程式設計大師如是說:


"When you have learned to snatch the error code from the trap frame, it will be time for you to leave."
“當你從我手中奪走水晶球時,就是你離開的時候了。”
1.1
Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.
寂靜的虛空裡誕生了神祕的東西,這種東西恆久存在永不消失,它是所有程式的根源所在,我不知道怎麼形容它,姑且稱它為程式設計之道。
If the Tao is great, then the operating system is great. If the operating system is great, then the compiler is great. If the compiler is greater, then the applications is great. The user is pleased and there is harmony in the world.
如果道是完美的,那麼作業系統就是完美的,如果作業系統是完美的,那麼編譯囂就是完美的,如果編譯囂是完美的,那麼應用程式就是完美的,所以使用者心滿意足,整個世界因此和諧。

The Tao of Programming flows far away and returns on the wind of morning.
程式設計之道去如黃鶴來如晨風。

1.2
The Tao gave birth to machine language. Machine language gave birth to the assembler.
道生機器語言,機器語言生彙編囂。
The assembler gave birth to the compiler. Now there are ten thousand languages.
彙編器生編譯器,最後產生上萬種高階語言。

Each language has its purpose, however humble. Each language expresses the Yin and Yang of software. Each language has its place within the Tao.
不論多麼的微不足道,每種語言都有它自己的目的,每種語言都表達了軟體的陰陽兩極。每種語言都各得其道。

But do not program in COBOL if you can avoid it.
但是儘量不要用COBOL語言。

1.3
In the beginning was the Tao. The Tao gave birth to Space and Time. Therefore, Space and Time are the Yin and Yang of programming.
道之初,帶來了空間和時間,所以,空間和時間是程式設計的陰陽兩極。
Programmers that do not comprehend the Tao are always running out of time and space for their programs. Programmers that comprehend the Tao always have enough time and space to accomplish their goals.
不懂程式設計之道的程式設計師常常把空間和時間消耗殆盡,得道的程式設計師則總是有足夠的空間和時間去完成程式設計任務。

How could it be otherwise?
否則會是什麼樣呢?

1.4
The wise programmer is told about the Tao and follows it. The average programmer is told about the Tao and searches for it. The foolish programmer is told about the Tao and laughs at it.
上士聞道,從而行之。中士聞道,謹而尋之。下士聞道,大笑之。
If it were not for laughter, there would be no Tao.
大笑不足為道。

The highest sounds are the hardest to hear. Going forward is a way to retreat. Greater talent shows itself late in life. Even a perfect program still has bugs.
希音不聞,進即是退,大囂晚成。任何程式都有漏洞。


--------------------------------------------------------------------------------


The Ancient Masters
Book Two
Thus spake the master programmer:
程式設計大師如是說:


"After three days without programming, life becomes meaningless."
三日不程式設計,食肉無味。

2.1
The programmers of old were mysterious and profound. We cannot fathom their thoughts, so all we do is describe their appearance.
遠古時代的程式設計大師們高深莫測,我們不能揣測他們的所思所想,只能描述外表所見。
Aware, like a fox crossing the water. Alert, like a general on the battlefield. Kind, like a hostess greeting her guests. Simple, like uncarved blocks of wood. Opaque, like black pools in darkened caves.
他達明,如狐狸過水;機警,如戰場上的將軍;和善,如主婦款待客人;簡單,呆若木雞;混沌,如深淵之水。

Who can tell the secrets of their hearts and minds?
誰能道盡他們的所有?

The answer exists only in the Tao.
答案僅存於道。

2.2
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!"

“我不知道是圖靈夢見自己變成機器還是機器夢見自己變成圖靈。”
2.3
A programmer from a very large computer company went to a software conference and then returned to report to his manager, saying: "What sort of programmers work for other companies? They behaved badly and were unconcerned with appearances. Their hair was long and unkempt and their clothes were wrinkled and old. They crashed out hospitality suites and they made rude noises during my presentation."
一個大公司的程式設計師參加一個軟體會議後向他的主管彙報:“那些別的公司的程式設計師都是些什麼樣的人呀?他們舉止不雅,不修邊幅,頭髮蓬亂,衣服破舊,根本不熱情好客,還在我說話的時候亂嚷嚷。”
The manager said: "I should have never sent you to the conference. Those programmers live beyond the physical world. They consider life absurd, an accidental coincidence. They come and go without knowing limitations. Without a care, they live only for their programs. Why should they bother with social conventions?"
他的主管說:“我不應該讓你參加這次會議,這些程式設計師生活在現實世界之外。他們認為生活是可笑的,一場意外的偶然而已。他們來去自由,無所牽掛,他們只為他們的程式生活。為什麼要用世俗的煩擾去擾亂他們呢?”

"They are alive within the Tao."
“他們生活在道中”。

2.4
A novice asked the Master: "Here is a programmer that never designs, documents, or tests his programs. Yet all who know him consider him one of the best programmers in the world. Why is this?"
一個初學者問主管經理:“有一個程式設計師,他從來不預先設計,也不寫文件,甚至不測試他的程式,但是知道他的人都認為他是世界上最偉大的程式設計師,為什麼呢?”
The Master replies: "That programmer has mastered the Tao. He has gone beyond the need for design; he does not become angry when the system crashes, but accepts the universe without concern. He has gone beyond the need for documentation; he no longer cares if anyone else sees his code. He has gone beyond the need for testing; each of his programs are perfect within themselves, serene and elegant, their purpose self-evident. Truly, he has entered the mystery of the Tao."
經理說:“那個程式設計師掌握了道。他不需要預先進行設計;系統崩潰時他也從不煩燥,只是接受發生的一切而不管發生的事是好是壞 。他不需要寫文件,他從不顧及有沒有人看他寫的程式碼。他也不需要進行測試;他寫的每個程式都有一個完美的自我,平靜而優雅,它們的目的不言自明。他已經真正掌握了道的精髓。”


--------------------------------------------------------------------------------

Design
Book Three
Thus spake the master programmer:
程式設計大師如是說:

"When program is being tested, it is too late to make design changes."

“程式被測試時再去改變它的設計已經太晚了。”
3.1
There once was a man who went to a computer trade show. Each day as he entered, the man told the guard at the door:
曾經有一個人參加計算機展覽,每天他進門時都對門衛說:

"I am a great thief, renowned for my feats of shoplifting. Be forewarned, for this trade show shall not escape unplundered."
“先警告你,我是偷盜高手,我入室偷盜的本領聞名遐邇。這次展覽會也再劫難逃。”
This speech disturbed the guard greatly, because there were millions of dollars of computer equipment inside, so he watched the man carefully. But the man merely wandered from booth to booth, humming quietly to himself.
警衛因此大動干戈,因為裡面有價值百萬的計算機裝置,所以他牢牢盯緊了這個人,但是這個人只是從一個攤位逛到另一個攤位,一邊平靜地喃喃自語。

When the man left, the guard took him aside and searched his clothes, but nothing was to be found.
當這個人離開時,警衛搜了他的身,但是什麼也沒有發現。

On the next day of the trade show, the man returned and chided the guard saying: "I escaped with a vast booty yesterday, but today will be even better." So the guard watched him ever more closely, but to no avail.
展覽會的第二天,這個人又對警衛說:“昨天我滿載而歸,但是今天我會收穫更大。”所以警衛更加小心地盯緊他,但是這樣做完全於事無補。

On the final day of the trade show, the guard could restrain his curiosity no longer. "Sir Thief," he said, "I am so perplexed, I cannot live in peace. Please enlighten me. What is it that you are stealing?"
展覽會的最後一天,警衛再也不能忍住他的好奇心了。“小偷先生,”他說,“ 你說我驚慌不安,請告訴我,你到底偷了什麼?”。

The man smiled. "I am stealing ideas," he said.
這個人笑笑說:“我在偷想法。”

3.2
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 the Tao before transcending structure."
有一位程式設計大師,他寫非結構化的程式,一位初學者刻意模仿他,也寫非結構化的程式。當他讓大師看他的進步時,大師批評了他的非結構化程式:“ 對一位程式設計大師合適的東西未必對一個初學者同樣合適,在超越結構化之前,你必須理解程式設計之道。”
3.3
There was once a programmer who was attached to the court of the warlord of Wu. The warlord asked the programmer: "Which is easier to design: an accounting package or an operating system?"
###有人問一位程式設計師,“一個財務軟體和一個作業系統哪個更容易設計?”
"An operating system," replied the programmer.
“是作業系統”,這位程式設計師回答說。

The warlord uttered an exclamation of disbelief. "Surely an accounting package is trivial next to the complexity of an operating system," he said.
此人大惑不解。他說:“顯然一個財務軟體比起作業系統來說其複雜性是微不足道的”。

"Not so," said the programmer, "when designing an accounting package, the programmer operates as a mediator between people having different ideas: how it must operate, how its reports must appear, and how it must conform to the tax laws. By contrast, an operating system is not limited my outside appearances. When designing an operating system, the programmer seeks the simplest harmony between machine and ideas. This is why an operating system is easier to design."
程式設計師說:“不,設計財務軟體時,一個程式設計師必須成為持不同意見的使用者與計算機的一箇中介,他必須瞭解使用者的操作習慣,報表要是什麼形式,如何遵循稅法。相反,一個作業系統完全與這些外部的東西無關。設計作業系統,程式設計師只需要達到自己的設想與機器之間的簡單的和諧。這就是為什麼作業系統反而比財務軟體更容易設計。”

The warlord of Wu nodded and smiled. "That is all good and well, but which is easier to debug?"
這些人笑著說。“不錯,但是哪一個更容易除錯呢?”

The programmer made no reply.
程式設計師沒有回答。

3.4
A manager went to the master programmer and showed him the requirements document for a new application. The manager asked the master: "How long will it take to design this system if I assign five programmers to it?"
一個專案經理帶給程式設計大師一個專案的需求,然後問大師:“如果我給你5個程式設計師,要多少時間設計這個專案?”
"It will take one year," said the master promptly.
“一年”,大師說。

"But we need this system immediately or even sooner! How long will it take it I assign ten programmers to it?"
“但是我們等不了那麼長時間,越快越好,如果10程式設計師呢?”

The master programmer frowned. "In that case, it will take two years."
大師皺了一下眉頭說:“那就要花2年”。

"And what if I assign a hundred programmers to it?"
“那,100年程式設計師呢?”

The master programmer shrugged. "Then the design will never be completed," he said.
大師聳了聳肩說:“那這個專案就永遠完不成了。”

--------------------------------------------------------------------------------

Coding
Book Four
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 strings 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 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 sometimes 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 the 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 the Tao is perfect.
大師說:“你困惑是因為你不瞭解道。只有傻瓜才會相信只要遵循別人所說就能得到想當然的結果。為什麼你要從一個人類自己構造的機器中去得到想當然的結果呢?計算機只是決定論的產物;只有道才是唯一完美的。

The rules of programming are transitory; only the Tao is eternal. Therefore you must contemplate the 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 the Tao 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.
“也在遊戲機裡?”,初學者又