在代码中发现并修复漏洞可能令人沮丧,尤其是对于新手开发者而言。幸运的是,像 GitHub Copilot 这样的工具可以快速识别并消除漏洞,让您能够专注于更具创造性和有趣的工作。
先决条件
本文中的示例假设您在 Visual Studio Code(VS Code)中使用 GitHub Copilot 调试 Python 项目。要跟随示例,您需要
- 完成 设置 Visual Studio Code 与 Copilot(在 Visual Studio Code 文档中)。
- 下载 Python.
- 安装 Visual Studio Code 的 Python 扩展。
通过示例学习调试
当您尝试运行有缺陷的代码时,会遇到两种主要情况
- 代码在运行结束前退出,并出现错误信息。
- 代码运行未报错,但输出与预期不同。
幸好,Copilot 可以在这两种情况下帮助调试代码。要了解如何操作,请完成以下示例。
使用 GitHub Copilot 调试错误
当您运行有错误的代码时,通常会收到错误信息。该信息会告诉您错误发生的文件和行号,并简要描述出错原因。然而,错误信息有时会让人困惑。要完整理解并修复该错误,我们可以向 Copilot 求助。
让我们使用示例仓库进行尝试:new2code/debug-with-copilot。
克隆示例仓库
首先,我们需要创建仓库的本地副本
- 开始克隆 new2code/debug-with-copilot 仓库 在 VS Code 中。
- 选择在计算机上保存仓库的位置,然后点击 选择为仓库目标位置。
- 出现提示时,打开仓库。
运行有缺陷的文件
现在,让我们运行 bugged_dice_battle.py 文件。此程序模拟两个玩家之间的掷骰子对决。
-
在 VS Code 中,打开并查看
bugged_dice_battle.py文件。 -
通过按下 Ctrl+Shift+P(Windows/Linux)或 Cmd+Shift+P(Mac)打开命令面板。
-
输入
Terminal: Create New Terminal并按 Enter。 -
在终端标签页中,粘贴以下命令。
Windows
Shell py bugged_dice_battle.py
py bugged_dice_battle.pyMac 或 Linux
Shell python bugged_dice_battle.py
python bugged_dice_battle.py -
按 Enter 运行程序。
不幸的是,我们在终端中得到一些错误文本,结尾如下信息
TypeError: can only concatenate str (not "int") to str
调试文件
要了解此错误的含义,在 VS Code 中打开 Copilot Chat,然后粘贴并发送以下提示
Explain in depth why my code produces the following error and how I can fix it: TypeError: can only concatenate str (not "int") to str
Explain in depth why my code produces the following error and how I can fix it:
TypeError: can only concatenate str (not "int") to str
Copilot 将回复说错误是因为我们尝试将整数 die_1 和 die_2 与字符串连接,而只能将字符串与字符串连接。
它还会提供一个 更新后的代码版本,通过使用 str() 函数在连接之前将整数转换为字符串来修复该错误。通过将 Copilot 的建议应用到文件中,练习调试的最后一步。
使用 GitHub Copilot 调试不正确的输出
有时,有缺陷的代码运行时没有抛出任何错误,但输出显然不正确。在这种情况下,调试会更困难,因为 VS Code 无法告诉您错误的位置或描述。
针对这些“隐形”错误,Copilot 尤其有用。让我们通过示例仓库中的另一个文件获得实践经验:bugged_factorial_finder.py。这是一个应该计算阶乘的 Python 程序。
运行有缺陷的文件
首先,让我们运行程序以查看错误的输出
-
打开并检查
bugged_factorial_finder.py文件。 -
在之前创建的终端中,粘贴以下命令。Windows
Shell py bugged_factorial_finder.py
py bugged_factorial_finder.pyMac 或 Linux
Shell python bugged_factorial_finder.py
python bugged_factorial_finder.py -
按 Enter 运行程序。
不幸的是,代码未按预期工作。我们期望它返回 720,即 6 的阶乘的正确值,但输出远高于此。
调试文件
要了解出错原因,打开 Copilot Chat 并发送以下提示
Why is the output of this code so much higher than expected? Please explain in depth and suggest a solution.
Why is the output of this code so much higher than expected? Please explain in depth and suggest a solution.
Copilot 将指出,由于我们使用了 *= 运算符,实际上是在将 factorial 同时乘以 i 和 factorial。换句话说,在循环的每一次迭代中,我们都多乘了一个 factorial。
为了解决此错误,Copilot 会建议删除公式中多余的 factorial,或将 *= 运算符改为 =。现在进行该更改!
调试您自己的项目
现在您已经使用 Copilot 练习了调试一些简单程序,可以使用相同的方法在自己的工作中寻找并修复隐藏的错误。
例如,要调试代码生成的错误信息,请向 Copilot 发送以下提示
Explain in depth why my code produces the following error and how I can fix it: YOUR-ERROR-MESSAGE
Explain in depth why my code produces the following error and how I can fix it:
YOUR-ERROR-MESSAGE
或者,如果您在调试不正确的输出,请询问 Copilot 为什么输出不正确以及如何修复。为获得最佳效果,请尽可能提供输出与您期望之间差异的详细背景。
有了这些技巧,您已经具备了在项目中消除错误的能力!
后续步骤
在持续编码过程中,您可能会遇到特定的情景和难以调试的错误。有关潜在问题列表以及用于修复它们的 Copilot Chat 示例提示,请参阅 调试错误。