17 lines
756 B
Markdown
17 lines
756 B
Markdown
## 命名规则
|
||
|
||
> [《布尔变量起名的艺术》](https://thatamazingprogrammer.com/posts/stop-naming-your-variables-flag-the-art-of-boolean-prefixes/)
|
||
|
||
### 使用正确的前缀
|
||
|
||
(1)**is**:描述事物的状态,后面跟形容词,比如 isActive,isDeleted,isEmpty。
|
||
|
||
(2)**has**:描述事物的所有权或包含关系,后面跟名词,比如 hasAccess,hasChildren,hasValidationErrors。
|
||
|
||
(3)**can**:描述事物的能力或权限,比如 canEdit,canDelete,canRetry。
|
||
|
||
(4)**should**:描述事物的意图或逻辑,比如 shouldRetry,shouldCacheResponse。
|
||
|
||
### 其他
|
||
|
||
永远不在布尔变量名中使用否定词,比如isDisabled,因为当使用在if语句中会让大脑进行双重否定运算 |