# 查看 AI 生成的代码

了解验证和确认 AI 生成代码的技术 副驾驶聊天 ，以及如何提供帮助。

查看 AI 工具（如 GitHub CopilotChatGPT 或其他代理）生成的代码正成为新式开发人员工作流的重要组成部分。 本指南提供实用技术，强调人工监督和测试的重要性，并包括展示 AI 如何协助审查过程的示例提示。

对于旧代码库和较大的拉取请求来说，彻底的评审过程尤其重要。 将人类专业知识与自动化工具相结合，可以确保 AI 生成的代码符合质量标准，符合项目目标，并遵循最佳做法。

借助 Copilot，你可以简化评审过程，并增强识别 AI 生成的代码中的潜在问题的能力。

## 1.从功能检查开始

始终首先运行自动测试和静态分析工具。

* 请确保代码编译，并且所有测试都通过。 检查是否有任何新的警告或错误。
* 使用类似 [CodeQL](https://codeql.github.com/) 和 [Dependabot](/zh/code-security/supply-chain-security/keeping-your-dependencies-updated-automatically) 捕获漏洞和依赖项问题的工具。
* 有关使用 Copilot 验证代码的示例，请参阅 [生成单元测试](/zh/copilot/tutorials/copilot-chat-cookbook/testing-code/generate-unit-tests) 和 [为网页创建端到端测试](/zh/copilot/tutorials/copilot-chat-cookbook/testing-code/create-end-to-end-tests)。

### 示例提示

* `What functional tests to validate this code change do not exist or are missing?`
* `What possible vulnerabilities or security issues could this code introduce?`

## 2.验证上下文和意向

检查 AI 生成的代码是否符合项目的用途和体系结构。

* 查看 AI 输出，以符合你的要求和设计模式。
* 问自己：“此代码是否解决了正确的问题？ 它是否遵循我们的约定？
* 使用你的自述文件、文档和最近的拉取请求作为为 AI 提供上下文的起点。 指示 AI 信任哪些来源、不使用哪些来源，并提供优良示例供参考。
* 尝试 [合成研究](/zh/copilot/tutorials/copilot-chat-cookbook/communicate-effectively/synthesizing-research) 来了解 Copilot 如何使用文档和研究来指导代码生成。
* 当要求 AI 执行研究和规划任务时，请考虑将 AI 输出提取到结构化项目中，然后成为未来 AI 任务（如代码生成）的上下文。

### 示例提示

* `How does this refactored code section align with our project architecture?`
* `What similar features or established design patterns did you identify and model your code after?`
* `When examining this code, what assumptions about business logic, design preferences, or user behaviors have been made?`
* `What are the potential issues or limitations with this approach?`

## 3. 评估代码质量

人类标准仍然很重要。

* 查找可读性、可维护性和明确的命名。
* 避免接受难以遵循的代码，或者重构所需的时间比重写更长。
* 首选记录良好且包含清晰注释的代码。
* 检查 [提高代码可读性和可维护性](/zh/copilot/tutorials/copilot-chat-cookbook/refactor-code/improve-code-readability) 以获取有关审查和重构生成代码的提示和建议。

### 示例提示

* `What are some readability and maintainability issues in this code?`
* `How can this code be improved for clarity and simplicity? Suggest an alternative structure or variable names to enhance clarity.`
* `How could this code be broken down into smaller, testable units?`

## 4. 审查依赖项

警惕新软件包和库。

* 检查建议的依赖项是否存在并被主动维护。 考虑新依赖项的来源和参与者，以确保它们来自信誉良好的非竞争源。
* 查看授权许可。 避免引入与项目许可证不兼容的代码或依赖项（例如 MIT 许可项目中的 AGPL-3.0，或者没有声明的许可证的依赖项）。
* 注意留意幻觉或可疑的包（例如实际不存在的包），或“slopsquatting”（一种使用虚假或恶意包对大型语言模型进行的理论攻击）。
* [创建模板](/zh/copilot/tutorials/copilot-chat-cookbook/communicate-effectively/creating-templates) 演示了如何 Copilot 协助依赖项设置，但最好始终自行验证建议的包。
* 使用 [GitHub Copilot代码引用](/zh/copilot/concepts/completions/code-referencing) 查看与公开可用的代码的匹配项。

### 示例提示

* `Analyze the attached package.json file and list all dependencies with their respective licenses.`
* `Are each of the dependencies listed in this package.json file actively maintained (that is, not archived and have recent maintainer activity)?`

## 5. 发现特定于 AI 的陷阱

AI 工具可能会犯独特的错误。

* 查找虚构的 API、被忽视的约束或存在错误的逻辑。
* 请关注那些已删除或跳过的测试，而非已修复的测试。
* 对那些看似正确但与您意图不符的代码持怀疑态度。
* 请参阅[调试无效的 JSON](/zh/copilot/tutorials/copilot-chat-cookbook/debug-errors/debug-invalid-json)，了解如何通过Copilot捕获细微错误和调试的示例。

### 示例提示

* `What was the reasoning behind the code change to delete the failing test? Suggest some alternatives that would fix the test instead of deleting it.`
* `What potential complexities, edge cases, or scenarios are there that this code might not handle correctly?`
* `What specific technical questions does this code raise that require human judgment or domain expertise to evaluate properly?`

## 6. 使用协作评审

配对和团队协作有助于捕获微妙的问题。

* 要求团队成员查看复杂或敏感的更改。
* 使用清单可确保涵盖所有关键评审点（功能、安全性、可维护性）。
* 在团队中共享 AI 使用的成功提示和模式。
* 请参阅[有效沟通](/zh/copilot/tutorials/copilot-chat-cookbook/communicate-effectively)以获取有关如何与Copilot协作处理和记录结果的示例。

## 7. 尽可能实现自动化

让工具处理重复性工作。

* 设置 CI 检查样式、linting 和安全性。
* 使用 Dependabot 进行依赖项更新和警报。
* 应用 CodeQL 或类似的扫描程序进行静态分析。
* [查找与 GitHub Copilot 建议匹配的公共代码](/zh/copilot/how-tos/get-code-suggestions/find-matching-code) 演示如何 Copilot 帮助跟踪代码模式并自动执行搜索任务。
* 考虑是否具有推理功能的 AI 代理可以帮助自动执行评审过程的一部分。 例如，构建一个自我评审代理，该代理根据标准评估草稿拉取请求，在请求人工评审 *之前* 检查准确性、适当的语气和业务逻辑。

## 8. 不断改进工作流

采用新的 AI 工具和技术可以使工作流更加有效。

* 记录查看 AI 生成的代码的最佳做法。
* 鼓励团队中的“AI 冠军”共享提示和工作流。
* 更新载入和贡献指南，以包括 AI 评审技术和资源。 使用`CONTRIBUTING.md`文件在存储库中记录您对 AI 生成的源代码和内容的期望，参见 [制定仓库贡献者指南](/zh/communities/setting-up-your-project-for-healthy-contributions/setting-guidelines-for-repository-contributors)。
* 参考 [GitHub Copilot Chat 指南](/zh/copilot/tutorials/copilot-chat-cookbook) 获取灵感，并在团队文档中分享有用的食谱。

## 延伸阅读

* [现代代码评审中的人工监督](https://resources.github.com/enterprise/human-oversight-modern-code-review/) 在 GitHub 资源中