> 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/3-first-impression.md).

# 任务1：重构初体验

这份代码无疑散发出一些明显的坏味道，第一节课的内容也很简单，就是请你自己先动手重构一遍这个题目。不过，重构之前我想先请你思考/回答一个问题，那就是，拿到一份有待重构的代码，你要做的第一件事情是什么？

![](/files/-Lr9ZWmsjV6_nArwDdZr)

你得**先找到待重构代码已有的测试，并把它们先运行起来，确保得到绿色的结果**。如果待重构代码没有测试，你得先补上足够覆盖率的测试才能开始重构。所幸，这份代码不仅拥有一套测试集，它的覆盖率似乎还比较完善。

为什么要做这个动作呢，很明显，就是为了防止后续的任何修改无意破坏了软件的既有功能。这是重构区别于无秩序的结构调整的重要特征：重构的过程不允许破坏任何已有的功能。

好了，现在请拿起你的键盘开始重构吧。不过这仅仅是课程的开始，并不要求你马上把代码重构到完美，这也不是最重要的，更重要的是要关注重构的过程是否顺畅。

## 你的任务

Java：

```
git clone https://github.com/linesh-simplicity/length-java.git
cd length-java
./gradlew clean build
```

JavaScript：

```
git clone https://github.com/linesh-simplicity/length.git
cd length
npm test
```

* 使用 IDE 或命令行先运行第一遍测试
* 按照自己的理解，先重构一遍题目
* 如果 30 分钟后还没有完成，也停止

## 思考

体验的过程中，我希望你同时思考一下这些问题，它们是重构过程的一些重要关节：

* 这份代码有哪些坏味道？
* 你会先从哪里开始入手重构？
* 你的重构是否能够随时停下，仍然保持代码可工作？
* 运行测试的频率高吗？
* 你觉得重构最重要的是什么？


---

# 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/3-first-impression.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.
