跪拜 Guibai
← Back to the summary

AI Code That Runs Is Still Probably Wrong

Insert image description here

When using AI to write code, we often encounter a situation:

But when placed into a real project, problems arise.

This is "looks right, but is actually wrong."

The problem is not necessarily that AI can't write code, but that the code given by AI is usually based on speculation from existing information. If the requirements, project context, or verification process are incomplete, the code may be inconsistent with the real goal.

This article focuses on solving one problem:

After AI generates code, how should we judge whether it can actually be used?

1. Running Doesn't Mean Correct

The correctness of code includes at least four levels:

Check Level Questions to Confirm
Syntax Can the code be parsed correctly
Runtime Will it throw errors during execution
Logic Does the result conform to business rules
Engineering Is it safe, maintainable, and won't affect other functions

Many people only check the first two items: the code has no syntax errors and can run normally.

But the real problems often lie in the latter two.

For example, a function that calculates a discount, even if it can return a number normally, does not mean the discount calculation is necessarily correct.

2. Why AI Generates "Looks Right" Code

1. Lack of Business Context

The same field can have different meanings in different projects.

For example, discount might represent:

If you only tell AI "write a function to calculate a discount," it cannot determine the real meaning of this field and can only guess based on common writing styles.

2. Using Seemingly Reasonable APIs

AI might generate a function with a reasonable name, or use an already outdated API.

The code looks like it conforms to a certain framework's style, but the current project version might not support it.

Therefore, when seeing AI use unfamiliar methods, you should confirm through project documentation, type definitions, or official documentation.

3. Ignoring Boundary Conditions

AI-generated code usually satisfies normal inputs first.

But real projects also need to consider:

If these requirements are not explicitly raised, AI may not actively handle them completely.

4. Mistaking Example Code for Production Code

To illustrate an idea, AI sometimes omits:

Example code is suitable for helping us understand the direction, but cannot be directly equated with code that can go live.

3. A Typical Error Case

Suppose the business requirements are:

We ask AI to generate a function:

function calculateFinalPrice(price, discount) {
  return price * (1 - discount);
}

This code looks very concise, but executing the following code:

console.log(calculateFinalPrice(100, 20));

The result is:

-1900

The reason is that the code treated 20 as 20.0, not 20%.

The correct calculation method should first convert the percentage to a decimal:

function calculateFinalPrice(price, discount) {
  if (!Number.isFinite(price) || price < 0) {
    throw new Error('price must be a number greater than or equal to 0');
  }

  if (!Number.isFinite(discount) || discount < 0 || discount > 100) {
    throw new Error('discount must be a number between 0 and 100');
  }

  const finalPrice = price * (1 - discount / 100);

  return Number(finalPrice.toFixed(2));
}

This code still needs to be confirmed according to the project's actual rules, but at least it handles several key issues:

This example illustrates:

AI didn't necessarily write the syntax wrong, but it may have misunderstood the field's meaning.

4. What to Check First After Code Generation

Step 1: Re-read the Requirements

Don't run the code immediately after receiving it. First, check against the requirements:

Pay special attention to fields prone to ambiguity, such as amounts, time, ratios, status values, and IDs.

Step 2: Identify the Assumptions AI Made

You can directly ask AI:

Please list all the assumptions you made when generating this code.

Focus on checking:
1. The type and unit of each parameter
2. How null and outlier values are handled
3. Calculation rules for time, amounts, and ratios
4. Dependent libraries and versions
5. Permission and security premises

Do not modify the code, only list assumptions and issues that may need confirmation.

This step is very useful because hidden assumptions are often the source of errors.

Step 3: Prepare a Minimal Test Set

Prepare at least four types of inputs:

Test Type Example Purpose
Normal Value Price 100, Discount 20 Check the main flow
Boundary Value Discount 0, 100 Check the minimum and maximum range
Abnormal Value Null, string, negative number Check parameter validation
Extreme Value Very large price and decimals Check precision and stability

Corresponding to the previous function, you can first write tests:

console.log(calculateFinalPrice(100, 20));
console.log(calculateFinalPrice(100, 0));
console.log(calculateFinalPrice(100, 100));

try {
  calculateFinalPrice(100, 120);
} catch (error) {
  console.log(error.message);
}

The expected results should be:

80
100
0
discount must be a number between 0 and 100

Testing is not to prove AI is definitely correct, but to expose errors as quickly as possible.

Step 4: Check the Project Environment

After moving code from a standalone example into the project, also confirm:

If AI uses functions or dependencies that do not exist in the project, you cannot casually install new packages just to make the code run. First confirm whether the project already has similar capabilities.

5. Four Common Errors to Focus on Preventing

1. Logic Correct, Business Wrong

The code runs according to some logic, but does not conform to product rules.

For example:

This type of problem can only be confirmed through requirements, interface documentation, and business examples, not just by looking at syntax.

2. Normal Data Correct, Abnormal Data Wrong

For example, a search function works normally when entering keywords, but throws an exception when entering an empty string, special characters, or an overly long string.

Need to actively test:

3. Works Locally, Not in the Project

Common reasons include:

Therefore, after generating code, it must be run back in the real project, not just tested in isolated code snippets.

4. Functionally Correct, but Security Issues Exist

Code that implements a function does not mean it can be used safely.

Need to focus on checking:

Security checks should be done separately; do not default to "safe if functional tests pass."

6. Let AI Help You Verify, Not Just Generate

You can use this Prompt:

Please do not directly modify the code below; first help me verify if it meets the requirements.

Requirements:
[Paste complete requirements and business rules]

Code:
[Paste code]

Please output in the following order:
1. What the code currently implements
2. What unconfirmed assumptions the code has made
3. Places inconsistent with the requirements
4. Normal, boundary, and abnormal scenarios that need testing
5. Possible security risks
6. Issues that still cannot be determined

Please mark each issue: High, Medium, or Low priority.
Only after I confirm the issues, then provide a modification plan.

7. Don't Just Look at AI's Explanation

AI's explanation of code can be very fluent, but fluency does not equal accuracy.

When verifying answers, it is recommended to combine the following methods:

For code related to amounts, permissions, payments, data deletion, and privacy, verification standards should be stricter.

8. AI-Generated Code Acceptance Checklist

Before submitting code, you can use this checklist:

If the last item cannot be confirmed, it means this code is not yet suitable for direct submission.

Summary

Common reasons why AI-generated code "looks right, but is actually wrong" include:

When using AI for programming, don't just ask "Can the code run?", but also continue to confirm:

Does it meet the requirements? Does it cover boundaries? Is it suitable for the current project? Is it safe?

You can remember a simple process:

Read requirements first
  ↓
Confirm assumptions
  ↓
Run minimal examples
  ↓
Test boundaries and exceptions
  ↓
Check project environment and security
  ↓
Then submit code

AI is responsible for increasing coding speed, developers are responsible for verifying whether the results are truly reliable.

The next article will introduce:

"Building Your AI Programming Workflow from Scratch"


✍Insist on originality, seeking follows, likes, and favorites

Comments

Top 1 from juejin.cn, machine-translated. The original thread is authoritative.

咬代码的兽

I feel this deeply. The biggest pitfall of AI code is that its 'language intuition' is too good but it lacks execution feedback — it looks logically closed, but when you run it, all the boundary conditions collapse. My approach is to always pair two checks after generation: boundary-value unit tests + having the AI retell the key logic itself. If the retelling is vague, there's almost certainly a problem. Also, distilling the pitfalls you've stepped on into a checklist is very worthwhile. I usually browse the AI programming category on AI345 and can find plenty of similar practical tools — searching by scenario is much more efficient than blindly searching on my own.