> For the complete documentation index, see [llms.txt](https://ethan-lin.gitbook.io/refactoring/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ethan-lin.gitbook.io/refactoring/dan-wei-zhuan-huan/4-simple-renaming.md).

# 任务2：简单的重命名

怎么样？30 分钟内能完成你期待的重构目标吗？现在请你暂且将前一课的重构成果保存到分支上，一起来体会下常见的重构过程。

在开展重构时，也许有同学会觉得应该从逻辑最纠结的地方改起，因为重构的价值最大。但往往从最简单的地方开始，从外围慢慢清扫会更顺利，一来可以帮助你找到重构的节奏感和信心，二来外围的清扫往往会降低后续清理复杂逻辑的难度。当看到做完简单的修改测试依然变绿时，愉悦感会使你越发喜欢重构这件事情。

而味道最明显、最容易发现和处理的，莫过于那些词不达意、甚至还充斥着拼写错误的变量名和方法名了。“神秘命名”正是《重构》一书里首当其冲的第一种坏味道了。

命名无小事。这节课的任务很简单，就是给这些变量和方法一个更好的名字。

对了，还有个提醒，那个字符串`"f"`显然也令人抓狂，但它是我们下一节课的重构对象，所以请大家在这次重构中先放它一马，聚焦于其他的代码元素。

![](/files/-Lr9XDVwPdm0bYngsAc4)

## 你的任务

Java：

```
git checkout task-2
./gradlew test
```

JavaScript：

```
git checkout task-2
npm test
```

1. 将代码中你觉得不好的变量命名和函数命名改正过来（暂时不用改动表示英尺的`"f"`变量）

## 思考

* 有没有做到每修改一处代码就运行测试？
* 你是如何为变量和方法重命名的？全局搜索/替换？使用快捷键？不同的办法之间有什么优劣？
* 好的命名应该是什么样子？平时工作中的代码元素（变量、函数、类名、常量等）有没有命名不好的例子？
* 如果你不知道 IDE 对“重命名”这个重构操作有快捷键支持，请你搜索一下，应用到重构中去
* 重构的节奏顺畅吗？若有不顺畅的地方在哪里？

## 参考资料

* 《重构 2》6.7 变量改名（Rename Variable）
* 《重构 2》6.5 改变函数声明（Change Function Declaration）
* 《编写可读代码的艺术》第 2、3 章“把信息装到名字里”及“不会误解的名字”


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ethan-lin.gitbook.io/refactoring/dan-wei-zhuan-huan/4-simple-renaming.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
