Book Four Coding
第四篇 編碼
Thus speak the master programmer:
"A well-written program is its own heaven; a poorly-written program 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 neither too litter 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 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 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?"
The master coughed and shifted his position slightly. "The lesson is over for today," he said.
一位大師正在向他的一名初學編程的弟子解釋道的真諦. "此道體現在所有的軟件當中 ---- 不管它看上去多麼無足輕重. " 大師說著.
"此道體現在手掌計算器中嗎? " 初學者問.
"是的."
"此道體現在電子游戲中嗎? " 初學者繼續問.
"此道也體現在電子游戲之中. " 大師說.
"那麼此道體現在個人電腦的DOS系統中嗎? "
大師輕咳一聲, 並稍稍挪動樂一下位置. "今天的課就到這裡吧. " 他說.
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 all 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 my programmers were as wise!"
一位項目經理手下的一名程序員正在編寫軟件. 他的手指在鍵盤上飛舞著, 在程序的編譯過程中沒有出現任何錯誤的信息. 程序運行起來就像一陣和風.
"太好樂!" 經理高興地大叫樂起來, "你的技藝簡直完美無缺. "
"技藝? " 程序員說著便從他的終端機前轉過身來, "我遵循的是道 ---- 所有的技藝遠不能及! 當我剛開始編程時, 我眼前看見的是整個問題亂成一團. 三年之後, 我再也看不見這種一團糟的情形樂. 相反, 我用樂各種各樣的子程序. 但現在, 我什麼也看不見樂. 我的整個身心存在於一種無形的虛空離, 我的知覺是空蕩蕩的.
我的精神隨其本能而動, 不需要計劃就能自由的工作. 總而言之, 是我的程序自己寫出樂自己. 誠然, 有時會有一些難題. 我看見那些難題向我走來, 於是我放慢樂速度, 默默地注視著它們. 然後我更改樂一行代碼, 那些問題就煙消雲散樂. 然後我完成程序的編譯. 我靜靜地坐著, 讓工作的歡愉遍布全身. 我閉上雙眼, 歇息片刻, 然後退出系統. "
經理說: '我希望所有的程序員都這麼聰明! "

Leave a comment