Intro Naming By CleanCode

Intro Naming By CleanCode

Intro Naming By CleanCode

Example

1.不要把容器型態加入變數名稱中

ListaccountList
Better
ListaccountGroup
ListbunchOfAccounts
Listaccounts

2.產生有意義的區別

public static void copyChars(char a1[], char a2[])

Better

public static void copyChars(char source[], char destination[])

無意義的字詞
Product
ProductInfo
ProductData
以上三個命名只是名稱不同,但無法看出三者意義不同

getActiveAccount();
getActiveAccounts();
getActiveAccountInfo();

3.使用能唸出來的名稱

無法唸
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private final String pszqint = “102”;
/* … */
};

compare

可以唸
class Customer {
private Date generationTimestamp;
private Date modificationTimestamp;
private final String recordId = “102”;
/* … */
};