0%

编程之道 卷三 设计

Book 3 - Design

Thus spake the master programmer:
“When the 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 by 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 if I assign ten programmers to it?”
“但我们马上就要得到这个系统,越快越好!如果有10个程序员,又要花多久?”

The master programmer frowned. “In that case, it will take two years.”
大师皱了皱眉头:“这样的话,得两年。”

“And what if I assign a hundred programmers to it?”
“那要是有100个程序员呢?”

The master programmer shrugged. “Then the design will never be completed,” he said.
大师耸耸肩,“那这个项目永远也做不完了。”