如果引用或轉貼,麻煩註明出處與本網誌連結,否則視為侵權。

2005年1月3日

如何寫出無法維護的程式 - 命名 1-5

原作 : How To Write Unmaintainable Code ©1997-2004 Canadian Mind Products
中譯 : Fred F.M. Wang 2005/1/3


命名Naming
©1997-2004 Canadian Mind Products

"When I use a word," Humpty Dumpty said, in a rather scornful tone, "it means just what I choose it to mean - neither more nor less."
Lewis Carroll -- Through the Looking Glass, Chapter 6

寫無法維護的程式許多的技巧是在變數與方法命名上,這些技巧並不會被編譯器視為錯誤,這讓你有大量的自由使用這些技巧把維護程式者搞得昏頭轉向。
Much of the skill in writing unmaintainable code is the art of naming variables and methods. They don't matter at all to the compiler. That gives you huge latitude to use them to befuddle the maintenance programmer.

1. New Uses For Names For Baby 用嬰兒命名的方式命名
買本嬰兒命名書,那麼你就不缺變數名稱。例如一些容易輸入的字如Fred。其他像鍵盤上鄰近的字如adsf等。
Buy a copy of a baby naming book and you'll never be at a loss for variable names. Fred is a wonderful name, and easy to type. If you're looking for easy-to-type variable names, try adsf or aoeu if you type with a DSK keyboard.

2. Single Letter Variable Names用單一字母的變數名稱
如果用a, b, c 這類的字就無法用簡單的文字編輯器找到使用它的地方。而且沒有人可以猜的出來這變數是幹麼用的。
: If you call your variables a, b, c, then it will be impossible to search for instances of them using a simple text editor. Further, nobody will be able to guess what they are for. If anyone even hints at breaking the tradition honoured since FØRTRAN of using i, j, and k for indexing variables, namely replacing them with ii, jj and kk, warn them about what the Spanish Inquisition did to heretics.

3. Creative Miss-spelling使用有創意的錯別字
如果你要用描述性的變數及功能名稱,就用拼錯的字,或有些對有些錯的組合如SetPintleOpening Set PintalClosing,這樣就讓一些工具的搜尋功能無法發揮作用。
: If you must use descriptive variable and function names, misspell them. By misspelling in some function and variable names, and spelling it correctly in others (such as SetPintleOpening SetPintalClosing) we effectively negate the use of grep or IDE search techniques. It works amazingly well. Add an international flavor by spelling tory or tori in different theatres/theaters.

4. Be Abstract用抽象的字
在功能及變數的命名上用一些抽象的字,如it, everything, data, handle, stuff, do, reoutine, perform及數字如routineX48, PerformDataFunction, DoIt, HandleStuff and do_args_method等。
: In naming functions and variables, make heavy use of abstract words like it, everything, data, handle, stuff, do, routine, perform and the digits e.g. routineX48, PerformDataFunction, DoIt, HandleStuff and do_args_method.

5. A.C.R.O.N.Y.M.S.用縮寫
用縮寫字讓程式更精簡。真正的男子漢不用解釋這些縮寫, 因為他們生下來就懂得這些字。
: Use acronyms to keep the code terse. Real men never define acronyms; they understand them genetically.

譯者註 :
命名(naming)不清楚是每個程式設計師都會犯的錯誤 (不要告訴我,真正的男子漢生下來就懂得如何正確的命名) ,因此此部分共用32點錯誤的命名方式,前五點如本文就是說,
a.不要用姓名命名
b.不要用單一字母命名
c.不要用錯別字命名
d.不要用抽象字命名
e.不要用縮寫字命名 (沒有人天生就懂)
(2005/01/03 Fred)

相關文章

如何寫出無法維護的程式- 簡介
如何寫出無法維護的程式- 基本原則
如何寫出無法維護的程式- 命名1-5
如何寫出無法維護的程式- 命名6-10
如何寫出無法維護的程式- 命名11-15

沒有留言:

張貼留言

歡迎提供意見, 謝謝 (註 : 留言經過版主審核通過才會發布)