Notes — Clean Code : (2) Meaningful Names

Yi Jyun
1 min readDec 5, 2020

筆記

這邊可以歸納為概念以及 Code 兩個方面

概念

這邊比較屬於通用的概念,不會特別針對 language 的特性做建議。

Intention-Revealing Names

把意圖作為變數的名稱。

int d; // elapsed time in days
---
int elapsedTimeInDays;

Avoid Disinformation

避免使用錯誤資訊(disinformation)去誤導別人的理解

  1. 含義不清的名稱
int aix;

2. 名稱與型態不符合

accountList = {}

Use Searchable Names

在定義 constant 或命名變數名稱,使用比較容易搜尋到的命名

#### constant
circumference = diameter * 3.14
---
PI = 3.14
circumference = diameter * PI
###### variable name
for (int a = 0; a < all_user; a++);
---
for (int index_user = 0; index_user < all_user; index_user++);

Use Solution Domain Names

use computer science (CS) terms, algorithm names, pattern names, math terms, and so forth

Use Problem Domain Names

use the name from the prob- lem domain. At least the programmer who maintains your code can ask a domain expert what it means.

Code

主要針對程式語言的建議。例如 class name、function name 諸此之類。

Variable name & Class name

  1. 採用名詞名詞片語的命名
Customer, WikiPage, Account, and AddressParser

Method Names

採用動詞動詞片語的命名

string name = employee.getName(); 
customer.setName("mike");
if (paycheck.isPosted())...

--

--