The Codeless Code: Case 14 Hello-World(你好世界)
On their first day in the temple, the Java master posed the following three questions to the novices:
修道院新收的僧侣刚入院的第一天,Java大师就给这些新手们出了三个问题:
“Oftimes, when we acquire a new language or library, the first program we write is Hello, World. Its purpose is to inscribe these words in some fashion that will demonstrate the expected behavior of the technology. After this is done, the program is never run again.
“有时候,我们上手一门新的编程语言或者库,第一步通常都是编写hello-world,并且让它跑起来。这么做的目的是为了学会用想上手的语言去表达更多的意思。当完成这一步之后,就再没有跑过这个程序了。”
“Now: why does it say hello, and not also goodbye?
“现在呢,我想问大家:为什么用到的词汇是hello,而不是goodbye?”
“And: why say hello to the world, when the audience is but a single soul who will discard it after its first cry is uttered?
“并且,为什么当每个听众的孤独灵魂在听到它第一声啼哭之后都会遗弃它的情况下,它还是要向世界去问好?”
“And: why should it utter a greeting, which will never be answered in kind?”
“以及:为什么当它永远不会得到对等的友好回敬的时候,它还是要里面的去问候?”(读者注:大多新手学一门编程语言,都是简单的跑个hello-world就算是上手了,之后就再无深挖,如果程序有灵魂,必定亦会向所有浅尝辄止者质问这三个问题)
Speak me a word which means hello and goodbye—is it not the first cry of an infant?
请给我讲一个表示“你好”和“再见”的词——(这个词)难道不就是婴儿的第一声啼哭吗?
Find me a pinnacle from which the world may be seen—in whose eye, then, is reflected every tiger, and the desert of endless sand?
给我找一个可以俯视世界的尖塔——在(这个塔)的眼睛里不就可以看到每一只老虎,可以看到无边无际的沙漠吗?
(读者注:这个是上文中大师提的三个问题的答案,之所以是“你好"而不是"再见",是因为这是一个新的开始,不应该以再见开头;之所以这个世界万物以痛吻它的时候,它还要报之以善良,是因为它的境界远高于世界万物,万物皆可容忍于心中,这预示着在作者心中,所诞生的每门编程语言都凝结了大智慧,如矗立于万物之上的尖塔一般)
The Codeless Code: Case 15 Immutable(不变性)
A novice was recently arrived from the Temple of Three Stones where only C was used. He was new to the ways of Java, and soon stumbled upon the immutability of Strings.
一个最近从三石修道院(那是一座只使用C语言的修道院)专修过来的新进僧侣,他对Java一窍不通,很快就在字符串的不变性上摔了跟头。
“I wish only to convert the contents of this field to mixed case!” he cried. “Yet this requires the construction of a StringBuffer and a new String subsequently, with many untouched characters copied twice between them! For what purpose were Strings designed thus?”
“我只是想将这段内容给转成混合示例”,他哭诉着,“但这需要StringBuffer框架和用于接收的新的字符串,需要将许多字符原封不动的移动两次!把这个字符串操作设计成这个样子,究竟是为了什么呢?”
A learned sister heard, and advised him: “The brass coin passed from hand to hand may still be exchanged on the morrow; not so the egg.” The novice was enlightened.
一个正在研修的嬷嬷听到后,对他说:“一个铜钱手换手的传递一下,明天都会发生变化,更不用说一个鸡蛋了!”(读者注:修女的意思是说,连一枚铜钱,一颗鸡蛋,交换一下之后,都会发生变化,更不用说代码中的变量了,所以,就该把需要交换的对象都给写死),这个新僧听了这个,里面明白了Java中将交换设置的如此复杂的用意。
Soon the novice was defining all his classes to be immutable. Not one property could be modified: a new instance had to be created with the necessary changes. His constructors were sometimes astonishingly large and complex.
很快,这个新僧就定义好了他的所有类,并且把属性都设置成不可更改。没有任何人可以对他写的类进行修改:一个对象在进行创建的时候,必须依据类来进行修改才能用。用他所定义的构造函数有时候出乎意料的冗长和复杂。
The Java master was told of this, and said: “Let the form of an egg be cast in brass, and given to the nun to breakfast upon.”
Java大师知道了之后,吩咐寺众说道:“去用黄铜铸灌一个鸡蛋形状的铜像,并当作早餐,喂那位嬷嬷让她吃掉。”
In this manner was the nun charged with correcting the novice.
用这种方式,那个新进的僧侣从嬷嬷那以为学会的东西,被修正了回来。