keepcalculator

06 Numbers and Powers

AI Coding Assistants: Why They Fail and How to Spot It

Card No. 06·29125 · Updated · Standard formula, shown below

AI coding assistants make mistakes because they generate likely-looking code from patterns rather than reliably understanding a project, its requirements, or the effects of every change. You can spot errors by checking the code against the task, running tests and tools, and reviewing assumptions, edge cases, and security-sensitive behavior before relying on it.

How AI coding assistants produce code

Most coding assistants use large language models trained on substantial collections of text and code. Given a prompt and some surrounding context, a model predicts a sequence of tokens—pieces of text that may form comments, code, or punctuation. The result can be fluent and syntactically plausible without being correct.

Many assistants also use tools: they may search files, inspect errors, suggest edits, or run commands. This can make them more useful than a model that only receives a prompt, but it does not guarantee that they have found the relevant files, understood the full project, or interpreted a test result correctly. Their capabilities and access depend on the product and its configuration.

A useful mental model is a fast collaborator whose suggestions need verification, not a compiler or an authority. A compiler can check certain rules; tests can check specified behavior. Neither proves that software is correct in every situation, but both provide evidence that a generated suggestion alone cannot.

Why plausible code can be wrong

  • Incomplete context: The assistant may not see a related module, configuration file, database constraint, or unwritten requirement. If it changes one function without knowing how callers use it, it can break those callers.
  • Ambiguous requests: “Make this faster” does not say which behavior must stay the same, what inputs are common, or how performance should be measured. The assistant may optimize the wrong thing.
  • Pattern matching instead of verification: Familiar-looking code may use a nonexistent method, an incorrect library option, or an outdated convention. A confident explanation does not establish that an API exists or works as described.
  • Hidden edge cases: Code that works for a normal input can fail on an empty list, missing value, unusual character, very large number, timezone boundary, or network timeout.
  • Multi-step reasoning limits: A change can have consequences across several files and layers. The assistant may miss an interaction, such as a function returning a different shape than its caller expects.
  • Unclear or changing dependencies: Libraries, frameworks, and project versions differ. An answer based on a different version may be internally coherent but unsuitable for the codebase at hand.

These are not unique to AI. Human-written software also has defects. The difference is that generated code can arrive quickly and in convincing prose, so readers may accept it before establishing what it does.

Common mistakes to look for

Start by comparing the proposed change with the actual requirement. For example, a request to reject invalid email addresses might produce a complicated regular expression that rejects legitimate addresses or accepts unwanted ones. The key question is not whether the expression looks sophisticated; it is whether its behavior matches a clearly stated policy and tested examples.

Look for mismatches between names, types, and behavior: a function labelled “sort” that mutates the original list, a date treated as local time when the system expects UTC, or a number represented as text and compared alphabetically. Check error handling too. A broad catch that silently returns an empty result may hide a failure instead of handling it.

Pay particular attention when code handles permissions, user input, files, network requests, or data changes. Possible warning signs include trusting input without validation, building a database query by joining raw text, exposing secrets in logs, or removing authorization checks to make a test pass. These are prompts for careful review, not proof by themselves that a vulnerability exists.

A practical review process

  1. State expected behavior. Write down normal cases, invalid inputs, and important constraints before judging the code. If the request could mean several things, clarify it or define the assumption.
  2. Inspect the whole change. Read the diff, not just the assistant’s summary. Check which files changed, whether unrelated edits appeared, and whether the change fits existing naming, error-handling, and architectural patterns.
  3. Check claims against the project. Confirm that imports and methods exist in the installed dependency version. Consult the project’s documentation, type definitions, or existing examples where needed.
  4. Run focused checks. Use the project’s relevant tests, then its broader test suite when practical. Run formatting, linting, type checking, or a build if the project uses them. A passing check only covers what that check actually tests.
  5. Test boundary cases. Try empty, missing, invalid, unusually large, or otherwise important inputs. For changes involving time, ordering, or state, test the boundaries and transitions that matter.
  6. Review side effects and failures. Check whether the code changes stored data, makes external calls, modifies files, or behaves safely when a dependency is unavailable. Avoid running unfamiliar commands blindly, especially if they can delete or overwrite data.

For a small change, this may take only a few minutes. For a broad refactor or sensitive feature, tests and a second human review may be appropriate. The level of scrutiny should match the possible impact of a defect.

Tests help, but they are not proof

A test can show that code meets the examples and conditions it checks. It cannot establish behavior for cases nobody considered. An assistant may also generate tests that repeat the same mistaken assumption as its implementation: both may treat an empty input as valid even though the intended behavior is to reject it.

Improve confidence by writing or reviewing expected outcomes independently of the generated code. Include tests for the requirement’s boundaries, and use integration tests when behavior depends on several components working together. For changes involving transformations, invariants—facts that should remain true, such as preserving the number of records—can reveal problems that example-based tests miss.

When a test fails, read the failure and investigate the cause rather than asking the assistant to make it pass at any cost. A change that weakens an assertion or skips a failing test may conceal the defect instead of fixing it.

Prompts that make errors easier to catch

Clear instructions reduce avoidable misunderstandings, though they cannot eliminate model errors. Include the relevant files or interfaces, the project’s language and dependency constraints, expected behavior, and examples of valid and invalid inputs. Ask for a small, reviewable change rather than a sweeping rewrite.

You can also ask the assistant to state assumptions, identify files it plans to change, explain edge cases, and suggest tests. Treat those responses as a review aid: an explanation can itself be wrong. If a requirement is unclear, ask a focused question before accepting an implementation.

Frequently Asked Questions

Can AI coding assistants write correct code?
Yes, they can produce useful and correct code, especially for well-scoped tasks with clear context. Correctness still needs to be assessed against the project’s requirements and checks.

Why does generated code look right but fail?
It may follow familiar coding patterns while using the wrong API, overlooking project-specific context, or mishandling an edge case. Readability and confidence are not evidence that it was executed or verified.

Are AI-generated tests reliable?
They can help identify cases and expand coverage, but they may share the implementation’s mistaken assumptions. Review whether each test checks the intended behavior, including failure and boundary cases.

Should I accept code if all tests pass?
Passing tests is useful evidence, not a guarantee. Check what the tests cover, review the diff, and use additional checks or review when the change has wider effects.

What is the quickest way to spot a coding assistant’s mistake?
Compare the change with the stated requirement, inspect the diff, and run the most relevant tests. Then check edge cases and any assumptions about dependencies, inputs, or side effects.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →