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

2005年1月11日

如何寫出無法維護的程式 - 命名 16-20

原作 : How To Write Unmaintainable Code ©1997-2004 Canadian Mind Products
中譯 : Fred F.M. Wang 2005/1/10
 
16. Names From Mathematics用數學運算子的名稱:
Choose variable names that masquerade as mathematical operators, e.g.:
openParen = (slash + asterix) / equals;

17. Bedazzling Names 用令人迷惑的名字
選用與內涵無關的字作為變數名稱,這可以混淆讀者,因為他們試著去了解程式邏輯時,很難不去聯想到該字的意義,而這個字的意義和資料本身是不相干的。
: Choose variable names with irrelevant emotional connotation. e.g.:
marypoppins = (superman + starship) / god;
This confuses the reader because they have difficulty disassociating the emotional connotations of the words from the logic they're trying to think about.

18. Rename and Reuse 重新命名及重用命名
這招在Ada這個程式語言特別有效。讓一些地方參考到舊的名字,留下陷阱給別人
: This trick works especially well in Ada, a language immune to many of the standard obfuscation techniques. The people who originally named all the objects and packages you use were morons. Rather than try to convince them to change, just use renames and subtypes to rename everything to names of your own devising. Make sure to leave a few references to the old names in, as a trap for the unwary.
Reuse Names
: Java lets you create methods that have the same name as the class, but that are not constructors. Exploit this to sow confusion.

19. When To Use I 使用i的時機
不要把i用在最內層的迴圈變數。大膽的用i在其他目的,特別是非整數變數,然後用n作為迴圈索引。
: Never use i for the innermost loop variable. Use anything but. Use i liberally for any other purpose especially for non-int variables. Similarly use n as a loop index.

20. Conventions Schmentions
不管Sun Java命名慣例。例如inputFilename與inputfileName只有大小寫的差異。
: Ignore the Sun Java Coding Conventions, after all, Sun does. Fortunately, the compiler won't tattle when you violate them. The goal is to come up with names that differ subtlely only in case. If you are forced to use the capitalisation conventions, you can still subvert wherever the choice is ambigous, e.g. use both inputFilename and inputfileName. Invent your own hopelessly complex naming conventions, then berate everyone else for not following them.

譯者註 :
16-20條實際要說明的是
a.不要使用數學運算子的名稱命名
b.不要使用大家都不熟悉的字
c.不要重用定義過的名稱做兩種不同的用途
d.通常用在單一迴圈或簡單的迴圈
e.遵守Sun Java Coding conventions
2005/01/10 Fred Wang(http://fredwang.blogspot.com )

相關文章

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

沒有留言:

張貼留言

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