I Spent 10x Longer Debugging AI Code Than Writing It — Here's What Changed

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

I Spent 10x Longer Debugging AI Code Than Writing It — Here’s What Changed

每个人都在谈论人工智能如何加速编程,却没人谈论调试人工智能生成的代码。我记得自己达到崩溃临界点的那个瞬间。那是一个周二的下午,我盯着一段本应解析杂乱 CSV 数据的 Python 脚本。AI 助手在大约 12 秒内就写完了它。代码看起来很完美——变量命名清晰、文档字符串规范,甚至还有一个捕获明显边界情况的 try/except 代码块。然后我运行了它。修复第一个错误花了我 45 分钟。第二个错误又花了一个小时。到那天结束时,我花了大约 6 个小时来调试这些代码,而如果我自己从头写,可能只需要 3 个小时——而且我会理解其中的每一行。那一刻我意识到,我使用 AI 的方式存在严重的问题。

The Illusion of Speed Here’s the thing nobody tells you about AI pair programming: the code looks perfect. The syntax is flawless. The indentation is consistent. The function names follow best practices. It passes the “eye test” every single time. But the eye test is exactly where the trap lies. The problem isn’t that AI generates bad code. The problem is that AI generates confident code. It never second-guesses itself. It never adds a comment saying “I’m not sure about this part.” It just produces output with the same unwavering certainty whether it’s solving a FizzBuzz problem or implementing a complex state machine. So you trust it. You skip the code review because it looks so clean. You deploy it because the tests pass — the tests you wrote, not the tests that actually cover the edge cases.

速度的幻觉 关于 AI 结对编程,没人告诉你的一点是:代码看起来太完美了。语法无懈可击,缩进整齐划一,函数命名遵循最佳实践。它每次都能通过“目测”。但陷阱恰恰就在目测中。问题不在于 AI 生成了糟糕的代码,而在于 AI 生成了“自信”的代码。它从不质疑自己,也从不添加类似“我不确定这部分”的注释。无论是在解决 FizzBuzz 问题还是实现复杂的有限状态机,它总是以同样坚定不移的语气输出结果。于是你信任了它。你跳过了代码审查,因为它看起来太整洁了。你部署了它,因为它通过了测试——那些你写的测试,而不是真正覆盖了边界情况的测试。

I started keeping a log of my debugging sessions. Here’s what the data showed over a two-week period: Code written by AI: 2.5 hours total Time debugging that same code: 19 hours Time rewriting AI code from scratch: 11 hours The math is brutal. I was spending nearly 8x longer fixing problems than I would have spent just writing the damn thing myself.

我开始记录我的调试过程。以下是两周内的数据显示: AI 编写代码总耗时:2.5 小时 调试相同代码耗时:19 小时 从头重写 AI 代码耗时:11 小时 这笔账算下来非常残酷。我花在修复问题上的时间,几乎是我自己动手写代码所需时间的 8 倍。

Why AI Code Breaks (In Ways You Don’t Expect) After dissecting about 30 different failures, I found a pattern. AI-generated code breaks in three specific ways:

  1. The “Confident Hallucination” Bug This is the worst one. The AI invents an API function that doesn’t exist, or assumes a library behaves a certain way when it doesn’t. The code looks perfectly reasonable — until you actually read the docs. I had a case where the AI used pandas.DataFrame.append() to combine two dataframes. That function was deprecated in pandas 2.0 — removed entirely. The AI was trained on data that included the old API, so it confidently generated code that would crash on any modern installation. The worst part? The error message said “AttributeError: ‘DataFrame’ object has no attribute ‘append’” — which looks like a typo in my code. So I spent 20 minutes looking for a typo that didn’t exist.

为什么 AI 代码会(以你意想不到的方式)崩溃 在剖析了大约 30 种不同的故障后,我发现了一个模式。AI 生成的代码通常会在三个方面出错:

  1. “自信的幻觉”错误 这是最糟糕的一种。AI 发明了一个不存在的 API 函数,或者假设某个库的行为方式与实际不符。代码看起来非常合理——直到你真正去阅读文档。我曾遇到过一个案例,AI 使用了 pandas.DataFrame.append() 来合并两个数据框。该函数在 pandas 2.0 中已被弃用并完全移除。AI 是在包含旧 API 的数据上训练的,因此它自信地生成了在任何现代安装环境下都会崩溃的代码。最糟糕的是什么?错误信息显示为 AttributeError: 'DataFrame' object has no attribute 'append'——这看起来像是我代码里的拼写错误。于是我花了 20 分钟去寻找一个根本不存在的拼写错误。

  2. The “Silent Logic” Bug This one is sneaky. The code runs. It doesn’t crash. It just produces wrong results about 5% of the time. I had an AI-generated function that was supposed to calculate compound interest. It worked for 95% of test cases. But for certain edge cases — like when the interest rate was exactly 0% — it returned None instead of the principal amount. No error. No warning. Just a None that propagated through the entire pipeline and corrupted the final output. These bugs are the worst because they don’t show up in tests. They show up in production, three weeks later, when a customer sends you an angry email.

  3. “静默逻辑”错误 这种错误非常隐蔽。代码可以运行,不会崩溃,但大约有 5% 的情况下会产生错误的结果。我有一个 AI 生成的函数本应计算复利。它在 95% 的测试用例中运行良好。但在某些边界情况下——比如利率恰好为 0% 时——它返回了 None 而不是本金金额。没有错误,没有警告。只有一个 None 在整个流程中传播,并破坏了最终输出。这些错误最糟糕的地方在于它们不会出现在测试中。它们会在三周后的生产环境中显现,那时客户会给你发来愤怒的邮件。

  4. The “Over-Engineered” Bug AI loves to be thorough. It adds type hints, validation checks, and utility functions that were never part of the original requirements. This sounds great in theory, but in practice it means: More code to debug More places for subtle bugs to hide More complexity than the problem actually requires I had an AI generate a 200-line function for what should have been a 15-line script. It included a custom exception hierarchy, a logging setup, and a configuration system. All for a one-off data migration script.

  5. “过度设计”错误 AI 喜欢面面俱到。它会添加类型提示、验证检查和工具函数,而这些从未在原始需求中出现过。理论上这听起来不错,但实际上意味着: 需要调试的代码更多了 隐藏细微错误的地方更多了 复杂程度远超问题本身的需求 我曾让 AI 为一个本应只有 15 行的脚本生成了一个 200 行的函数。它包含了自定义异常层次结构、日志记录设置和配置系统。而这一切仅仅是为了一个一次性的数据迁移脚本。

What Actually Changed After that brutal two-week period, I completely changed my workflow. Here’s what worked:

  1. Write the Tests First This is not a new idea, but it becomes critical when working with AI. You can’t trust the code, so you need a safety net before you even look at the implementation.

Before: “Let AI write the function, then write tests”

After: “Write tests first, then prompt AI to satisfy them”

… Now when I prompt the AI, I include the test file in the context. The AI generates code that has to pass these tests, which forces it to actually think about the edge cases instead of just pattern-matching. This single change reduced my debugging time by about 60%.

真正改变了什么 在那段残酷的两周之后,我彻底改变了我的工作流程。以下是有效的方法:

  1. 先写测试 这不是什么新点子,但在与 AI 合作时变得至关重要。你不能信任代码,所以你需要一个安全网,甚至在查看实现之前就得有。

之前:“让 AI 写函数,然后写测试”

之后:“先写测试,然后提示 AI 去满足这些测试”

…… 现在,当我提示 AI 时,我会将测试文件包含在上下文中。AI 生成的代码必须通过这些测试,这迫使它真正去考虑边界情况,而不是仅仅进行模式匹配。这一项改变将我的调试时间减少了约 60%。

  1. Code Review With a Skeptic’s Eye I stopped reading AI code looking for bugs. Instead, I read it looking for assumptions. Every time I saw an implicit assumption — like “this list is never empty” or “this API never returns None” — I added a comment and a guard clause. The key insight: AI doesn’t know your data. It doesn’t know that your CSV files sometimes have encoding errors. It doesn’t know that the third-party API goes down on Thursdays. It just writes clean, abstract code that works in a perfect world.

  2. 用怀疑的眼光进行代码审查 我不再为了找 Bug 而阅读 AI 代码。相反,我阅读它是为了寻找其中的假设。每当我看到一个隐含的假设——比如“这个列表永远不会为空”或“这个 API 永远不会返回 None”——我就会添加注释和保护子句。关键的洞察是:AI 不了解你的数据。它不知道你的 CSV 文件有时会有编码错误,也不知道第三方 API 会在周四宕机。它只是编写了在完美世界中运行的、干净且抽象的代码。

  3. The 10-Minute Rule If I can’t understand a block of AI-generated code within 10 minutes, I delete it and rewrite it myself. This sounds wasteful, but it’s actually the most efficient strategy. Here’s the math: AI generates code in 30 seconds. Debugging it takes 2-3 hours. Rewriting it myself takes 45 minutes. Even if I delete and rewrite 4 times…

  4. 10 分钟规则 如果我不能在 10 分钟内理解一段 AI 生成的代码,我就把它删掉并自己重写。这听起来很浪费,但实际上这是最高效的策略。算一下:AI 在 30 秒内生成代码。调试它需要 2-3 小时。我自己重写需要 45 分钟。即使我删掉并重写了 4 次……