应用指南

如何提示 AI 编写更好的代码

当请求指定目标、相关代码上下文、约束和预期行为时,提示人工智能编码助手效果最佳。

听听这个指南AI叙述 · ~3 min

在本页3 分钟阅读
  1. 概述
  2. 深入探讨
  3. 现实世界的实施
  4. 如何向 AI 提示以获得更好代码的未来
  5. 付诸实践
  6. 不断探索
  7. 常见问题

概述

清晰的示例和边界情况有助于暴露歧义,但在依赖生成代码之前,它仍然需要人工审查、测试和安全检查。

深入探讨

A vague coding request leaves the assistant to infer requirements that may be obvious to the developer but absent from the prompt. State the goal first, then add language or framework versions, inputs and outputs, constraints, relevant interfaces, and acceptance conditions. GitHub’s Copilot prompting guidance recommends starting general and becoming specific, giving examples, avoiding ambiguity, and pointing the assistant to relevant code. For complex work, divide the task into smaller steps and provide examples of expected behavior. These techniques improve the information available to the model; they do not guarantee that the code is correct. Context should be relevant and focused. Include the specific function, error, schema, or project convention that matters rather than pasting a large unrelated codebase. For a new function, examples can clarify edge cases such as empty input or malformed rows. Asking for tests alongside implementation can make expected behavior explicit. If the model proposes a design first, review that plan before asking it to implement; this creates a checkpoint for correcting a wrong assumption before it spreads through code. Treat generated code like a proposed change from a contributor. Read it, check that it fits the project and uses real APIs, run applicable tests and static analysis, and consider security properties such as validation and secret handling. GitHub’s responsible-use guidance cautions that code suggestions can contain errors and calls for careful review and testing, especially in security-sensitive work. Do not execute unknown code merely because an assistant produced it. If the result is wrong, narrow the discrepancy and provide a concrete failing case in the next prompt. Iteration is most useful when it responds to evidence rather than asking the model to “try again” without new information.

现实世界的实施

开发人员请求编写一个解析器,指定语言和函数签名,并给出预期结果的有效和无效日期示例。

维护人员分享了相关接口和仓库规范,然后要求进行范围有限的更改,而不是大规模重写。

队友请助手在实现解析器之前,为输入为空、记录格式错误以及编码意外的情况起草测试。

审阅者检查生成的更改,运行项目测试套件和静态检查,并验证新的依赖项和 API 是否存在。

如何向 AI 提示以获得更好代码的未来

编码助手正在获得更多对仓库上下文和开发工具的访问权限,这可以减少手动复制,但也会扩大错误指令的后果。具体要求和相关上下文仍然很重要,因为项目意图并不总是编码在文件中。团队需要将便利性与审查权限、测试以及对生成更改的清晰所有权结合起来。由于工具可以进行更大的编辑,维护人员应保持更改的可审查性,并在合并更改之前运行仓库的测试和检查。

付诸实践

为什么这很重要

  • 构建选择. 应用级设计决定了人工智能是否能改善实际结果。
  • 团队与工作流程. 良好的工作流程集成可以创造用户值得信赖的生产力收益。
  • 风险与安全. 范围明确的用例可以减少变更疲劳和实施风险。

留意

  • 将损坏的流程自动化可能会加剧现有问题。
  • 团队可能会过度自动化并消除所需的人工判断。
  • 如果不持续评估输出,质量可能会出现偏差。

后续步骤

  1. 绘制当前工作流程并确定摩擦最大的步骤。
  2. 在完全自动化之前定义人工检查点。
  3. 对用户进行提示、升级路径和质量标准方面的培训。
  4. 跟踪任务级结果以确认持续价值。

不断探索

免费通讯

获取每日AI简报

每个工作日早晨提供三条经过验证的AI故事,使用简明英文撰写。永久免费,无广告。

每个工作日发送一封邮件。一键退订。我们永不出售或分享您的地址。

测试一下自己

参加“如何向 AI 提示以获得更好代码”测验

每个答案即时反馈,课程通过后可获得可验证ID的可分享证书。

开始测验

支持免费AI教育。AI Understanding是一家501(c)(3)非营利组织——无广告,无付费墙,永不收费。 捐款

常见问题

什么是如何向 AI 提示以获得更好代码?

当请求说出目标、相关的代码上下文、约束和预期行为时,提示 AI 编码助手效果最好。清晰的示例和边界情况有助于暴露歧义,但在依赖生成代码之前,它仍然需要人工审查、测试和安全检查。

除了总体目标之外,清晰的代码生成请求还应该说明什么?

该指南建议陈述目标,然后提供上下文、约束、接口和接受条件。

为什么具体的输入输出示例在编码提示中很有用?

示例使预期行为变得明确,并且可以尽早揭示误解。

要重现特定的失败测试,开发人员应在相关代码中包含哪些上下文?

具体的失败案例和相关的代码上下文使行为可重现,并为调试提供辅助证据。

随着工作的进展,哪个顺序使得多部分实施请求最容易验证?

该指南建议分解复杂的请求并检查中间结果,以便在集成之前更容易发现假设和失败。

在实施的同时要求进行测试有助于澄清什么?

测试需要明确的预期行为,并且可能会暴露隐藏的歧义。