The Codeless Code: Case 14-15 Hello-World(你好世界)、Immutable(不變性)

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.

用這種方式,那個新進的僧侶從嬤嬤那以爲學會的東西,被修正了回來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章