Skip to content

Commit

Permalink
add #24
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Apr 24, 2024
1 parent 0aafa6e commit 266e51e
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Go tips from [Phuong Le](https://twitter.com/func25).
| 21 | Prefer using a pointer receiver when defining methods | QingyaFan |
| 22 | Simplify function signatures with structs or variadic options | zhubiaook |
| 23 | Skip the 'Get' prefix for getters | HBUzxl |
| 24 | | |
| 24 | Avoid repetition in naming | smallnest |
| 25 | Prefer 'chan struct{}' over 'chan bool' for signaling between goroutines | justlorain |
| 26 | | |
| 27 | Filter without any allocation | devin7788 |
Expand Down
61 changes: 61 additions & 0 deletions src/024.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

# Tip #24 避免命名中的重复

> 原始链接:[Golang Tip #24: Avoid repetition in naming](https://twitter.com/func25/status/1759196416961032620)
>
在编写代码时,我们通常以动词开头给函数命名,比如 get、set、fetch、update、calculate 等等...

## 1、包名与导出符号名称
在为对外可见(即在包外可见)的元素命名时,应避免重复使用包名。

否则,由于在包外使用这些符号时包名已经可见,会导致名称过长且更为重复:
![](./images/024/1.png)

这个“改进版”消除了重复。

当我们使用它时,语义自然:`chocolate.NewBar()`,清晰地创建了一个新的巧克力棒,没有冗余。

## 2、变量名与类型
我们通常不需要在变量名中重复其类型。

通常从上下文或使用方式即可清楚得知。
![](./images/024/2.png)

然而,存在一些例外情况,应当予以考虑。

如果你同时拥有 `[]Car``map[string]Car`(可能是出于快速查找的目的),那么为了清晰起见,可以这样做。

“但如何命名呢?carList 和 carMap?”

`CarList``carMap` 是不错的解决方案。

但我们可以通过指出数据的形式或状态使其更清晰,如:`[]Car cars``map[string]Car carLookup`

以下为另一个示例:
![](./images/024/3.png)

在第二种方案中,显而易见其为字符串和输入值。

## 3、避免重复归结于“上下文”
迄今为止我们讨论的所有内容都归结于“上下文”
- 包名
- 方法名
- 类型名
- 文件名

这些应指导你选择既简单又具有信息性、避免不必要的重复的名称。

接下来讨论一些与“上下文”相关的其他情况:

- 带有类型名的方法:
![](./images/024/4.png)

- 函数及其参数:
![](./images/024/5.png)

- 在函数内部,特别是在处理与函数目的密切相关参数或数据时,以一个不好的示例为例:
![](./images/024/6.png)

我们将函数名和局部变量名都进行重命名:
![](./images/024/7.jpeg)
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [Tip #21 定义方法时,优先使用指针作为接收器](./021.md)
- [Tip #22 使用结构体或变长参数简化函数签名](./022.md)
- [Tip #23 省略 getter 方法的'Get'前缀](./023.md)
- [Tip #23 避免命名中的重复](./024.md)
- [Tip #25 在 goroutines 之间进行信号传递时,使用 'chan struct{}' 而不是 'chan bool'](./025.md)
- [Tip #27 原地过滤](./027.md)
- [Tip #28 将多个if-else语句转换为switch](./028.md)
Expand Down
Binary file added src/images/024/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/024/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/024/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/024/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/024/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/024/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/images/024/7.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 266e51e

Please sign in to comment.