Sub topic 3
Review AI-Generated Code Before You Accept It
Assume you can read a Git diff, run unit tests, and use an AI coding assistant. Automated checks are evidence, not proof. They find many cheap errors, but they cannot confirm that the code meets the requirement.
Let Linters and Type Checkers Do the Cheap Work
Run the project’s formatter, linter, and type checker before inspecting every detail. A linter catches patterns such as unused imports, unreachable code, and unsafe calls. A type checker catches mismatched values, missing attributes, and invalid function arguments.
ruff check .
mypy src/
pytestUse the commands your project already configures. Do not weaken a rule merely to make generated code pass.
Maintainability and Fit
Check whether the change follows local conventions. Look for the project’s naming style, error-handling pattern, module boundaries, and test structure. AI-generated code often introduces a new helper, library, or abstraction when a small existing utility would fit better.
A change is easier to maintain when another developer can explain why it exists and modify it without understanding unrelated machinery.
Performance and Resource Use
Check loops, database queries, network calls, and memory use. A function that scans the same list repeatedly may pass tests but become slow with real data. Code that loads an entire file into memory may fail for large files.
Ask what happens at the expected maximum input size. Confirm that files, connections, and response bodies are closed or bounded.
Use AI as a Second Reviewer
A second AI pass can suggest missed edge cases or explain unfamiliar code. Give it the requirement and the diff, then verify every claim yourself. The second model may repeat the first model’s mistake, invent an issue, or approve insecure code.
Reusable Review Checklist
| Check | Evidence |
|---|---|
| Style and types | Formatter, linter, and type-checker output |
| Behavior | Tests for normal and boundary cases |
| Security | Input validation and safe API usage |
| Fit | Consistent project patterns |
| Resources | Expected time, memory, and cleanup |
Decide What to Do
Accept the change only when the requirement is satisfied and the evidence is sufficient. Revise it when the approach is sound but has fixable defects, missing tests, or unclear boundaries. Reject it when the design violates security requirements, hides behavior, or would require extensive correction.
Exercise
A generated function passes the linter but loads an unbounded upload into memory. What should you do?
Show answer
Revise or reject it after checking the upload limit and resource requirements. A clean lint result does not prove safe memory use.