# 代码重构

重构是指通过修改现有代码来改进代码质量和提高可维护性的过程。重构并不是改变代码的功能，而是旨在仅改善代码的质量。

使用 GitHub Copilot 可以轻松进行代码重构。GitHub Copilot 可以理解代码结构并提供推荐的重构选项。例如，如果有以下代码：

```py
def calculate_sum(numbers):
    total = 0
    for number in numbers:
        total += number
    return total
```

可以使用 GitHub Copilot 对此代码进行重构。以下是 GitHub Copilot 提供的重构示例。 这段代码被重写为使用 sum() 函数来计算列表的总和。参数和返回值的类型也已明确。

```py
# 请在下面写出已重构的 calculate_sum() 函数
def calculate_sum(numbers):
    return sum(numbers)
```

通过显式地编写这些指令，可以使 GitHub Copilot 和其他工具更加互动地进行重构。 这样，GitHub Copilot 可以为改善代码质量提供重构选项。开发人员可以考虑这些选项，并在必要时手动修改代码。通过这种重构，代码的可维护性得到了提高，开发人员可以更有效地开发代码。

在进行重构时编写测试非常重要。 如果编写了测试，可以确认重构后的代码是否与之前的代码执行相同的操作。 特别是在依靠 AI 工具进行重构时，GitHub Copilot 可能会建议您使用平时不使用的代码编写方式。 从一般的编程角度来看，这可能是很好的代码，但对您或您的组织可能不适用。 在进行 AI 重构时，也要考虑采用多大粒度的建议。

另一方面，GitHub Copilot 有助于支持测试驱动开发（TDD）。 TDD 是一种编写代码之前编写测试并编写代码以使测试通过的方法。 使用此方法，开发人员可以编写通过测试的代码，并提高代码质量。


---

# Agent Instructions: 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:

```
GET https://ai-native-development.gitbook.io/archived/chinese/ji-chu/refactoring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
