Using AI at work

Review AI-Written Code with a Practical Checklist

AI-generated code can look complete while still making assumptions about inputs, edge cases, dependencies, and failures. This guide uses a tiny synthetic CSV example to review the code systematically before relying on it.

Show contents

Who this is forFor people who use an AI chat assistant to draft small Python scripts and want a repeatable way to check them before using real data.

What you need
  • Python 3.12
  • Basic familiarity with running a Python script and reading CSV files

01Why AI-written code still needs review

An AI chat assistant can produce syntactically plausible code very quickly, but plausibility is not the same as correctness. A script may assume that every input row is complete, silently interpret a column incorrectly, overwrite an existing result, depend on a package you do not have, or fail only when it sees an unusual value. The useful habit is therefore not merely asking whether the code looks reasonable. Review its assumptions one by one.

A compact review can be organized around five questions: What inputs does the code expect? What happens at the edges? How are errors handled? What dependencies are required? What tests would reveal a wrong result? Output behavior should also be checked because a correct calculation can still be unsafe if it overwrites files.

02Use a tiny synthetic example first

Suppose you have a synthetic file named orders.csv. It has three columns: order_id, quantity, and unit_price. The three data rows are A001,2,10.00; A002,3,5.50; and A003,1,8.00. The expected revenue is easy to calculate by hand: 2 × 10.00 = 20.00, 3 × 5.50 = 16.50, and 1 × 8.00 = 8.00. The total should therefore be 44.50 across 6 units.

Keeping the first example this small matters. If an AI-produced script reports 44.50, you can independently verify the number. If you begin with a 50,000-row work file, a plausible-looking result may hide a parsing or calculation mistake.

order_idquantityunit_priceexpected revenue
A001210.0020.00
A00235.5016.50
A00318.008.00
Total644.50

03Ask for explicit input and output rules

A useful prompt specifies the file format, calculation, failure behavior, and output policy instead of asking only for a script that summarizes sales.

  1. Say that orders.csv must contain order_id, quantity, and unit_price.
  2. Ask the script to reject missing or invalid numeric values instead of guessing.
  3. Ask it to calculate total units and total revenue.
  4. Require only the Python 3.12 standard library.
  5. Require results to be written under outputs and require the script to stop if the intended output file already exists.

04Inspect a typical AI answer

A reasonable answer might use csv.DictReader, Decimal for money, pathlib for paths, and explicit validation. Even when the code looks clean, review each branch instead of assuming the implementation matches the request.

python
from csv import DictReader
from decimal import Decimal, InvalidOperation
from pathlib import Path

INPUT_FILE = Path("orders.csv")
OUTPUT_DIR = Path("outputs")
OUTPUT_FILE = OUTPUT_DIR / "order_summary.txt"
REQUIRED_COLUMNS = {"order_id", "quantity", "unit_price"}


def main():
    if not INPUT_FILE.is_file():
        raise SystemExit(f"Input file not found: {INPUT_FILE}")

    if OUTPUT_FILE.exists():
        raise SystemExit(f"Output already exists: {OUTPUT_FILE}")

    total_quantity = 0
    total_revenue = Decimal("0")

    with INPUT_FILE.open("r", encoding="utf-8", newline="") as file:
        reader = DictReader(file)

        if reader.fieldnames is None:
            raise SystemExit("Input file has no header row.")

        missing_columns = REQUIRED_COLUMNS - set(reader.fieldnames)
        if missing_columns:
            names = ", ".join(sorted(missing_columns))
            raise SystemExit(f"Missing required columns: {names}")

        for line_number, row in enumerate(reader, start=2):
            order_id = row["order_id"].strip()
            quantity_text = row["quantity"].strip()
            price_text = row["unit_price"].strip()

            if not order_id or not quantity_text or not price_text:
                raise SystemExit(f"Missing value on line {line_number}.")

            try:
                quantity = int(quantity_text)
                unit_price = Decimal(price_text)
            except (ValueError, InvalidOperation):
                raise SystemExit(f"Invalid numeric value on line {line_number}.")

            if quantity < 0 or unit_price < 0:
                raise SystemExit(f"Negative value on line {line_number}.")

            total_quantity += quantity
            total_revenue += quantity * unit_price

    OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
    result = (
        f"total_quantity={total_quantity}\n"
        f"total_revenue={total_revenue:.2f}\n"
    )
    OUTPUT_FILE.write_text(result, encoding="utf-8")
    print(f"Wrote: {OUTPUT_FILE}")


if __name__ == "__main__":
    main()

05Review inputs, edges, errors, and dependencies

CheckQuestion to askWhat this example does
InputsAre file names, columns, encodings, and data types explicit?Requires orders.csv and three named columns; opens UTF-8 CSV.
Edge casesWhat happens with missing headers, blanks, zero values, or negative values?Rejects missing fields and negative values; zero is accepted.
ErrorsDoes invalid input fail clearly or continue with a bad result?Stops with a line-specific message for missing or invalid values.
DependenciesDoes the script require packages or versions not mentioned?Uses only Python 3.12 standard-library modules.
Output safetyCan an existing result be overwritten?Stops if outputs/order_summary.txt already exists.
TestsCan you predict results for a tiny example?The synthetic example should produce 6 units and 44.50 revenue.

Also review assumptions that are not obvious from syntax. For example, this script permits quantity 0 and unit_price 0 because they are not negative. Whether zero values are valid is a business rule, not a Python question. The reviewer must decide whether that rule matches the real dataset.

06Design tests before using real files

Do not test only the normal case. A short set of deliberately different inputs reveals much more about the script's behavior.

  • Normal case: the three synthetic rows should produce total_quantity=6 and total_revenue=44.50.
  • Missing column: rename unit_price to price and confirm the script stops before calculating.
  • Blank value: leave one quantity empty and confirm it reports the relevant line.
  • Invalid number: use five instead of 5 and confirm it is rejected.
  • Negative value: use -1 for quantity or unit_price and confirm the script stops.
  • Zero value: use quantity 0 and decide whether accepting it matches your intended rule.
  • Existing output: create outputs/order_summary.txt first and confirm the script is designed to stop rather than overwrite it.
  • Empty data file with only the header: determine whether totals of 0 and 0.00 are acceptable or whether an empty dataset should be rejected.

07Know what this checklist does not prove

This checklist can expose many ordinary implementation mistakes, but it does not prove that the underlying requirement is correct. The script cannot decide whether refunds should be negative, whether quantity may be zero, whether prices include tax, or whether duplicate order IDs are permitted unless those rules are specified.

For larger or higher-risk scripts, extend the review to security, permissions, privacy, concurrency, numerical precision, performance, logging, and automated tests. Also inspect code that handles paths, shell commands, network requests, credentials, or destructive file operations more carefully than a small local CSV calculation.

The practical sequence is simple: define the input contract, calculate a tiny example independently, inspect each failure path, identify dependencies, protect outputs, and test edge cases. The AI can help draft the implementation, but the reviewer remains responsible for deciding whether the code matches the actual task.

Execution and verification record

2026-09-21 · hand-checked example · Python 3.12

  • The synthetic arithmetic was checked by hand: 2 × 10.00 = 20.00, 3 × 5.50 = 16.50, and 1 × 8.00 = 8.00.
  • The expected quantity was checked by hand: 2 + 3 + 1 = 6.
  • The expected revenue was checked by hand: 20.00 + 16.50 + 8.00 = 44.50.
  • The example code was reviewed manually against the stated input, validation, dependency, and no-overwrite requirements.
  • The code uses only Python standard-library modules: csv, decimal, and pathlib.
Verification limits
  • The Python code was not executed by the author of this article; behavior described here is based on manual code review.
  • The example does not define business rules for duplicate order IDs, zero quantities, refunds, taxes, currency conversion, or unusually large files.
  • Passing these checks does not prove that a script is secure or correct for a different dataset or production environment.

Site-wide writing and verification principles

References

The explanations and examples were written for this site. See the official sources below for the related behavior and concepts.