Understanding and Reviewing Generated Code
AI can generate code quickly, but running code you don't understand is risky. It might have bugs, security flaws, or simply not do what you expected. Taking time to review generated code builds your skills and catches problems early.
Reading Code Systematically
Start at the top and work down. For a function that fetches stock prices, you might see:
import os
import requests
from datetime import datetime
class StockNotFoundError(Exception):
pass
def get_stock_price(symbol):
api_key = os.environ.get('ALPHA_VANTAGE_API_KEY')
if not api_key:
raise ValueError("API key not found in environment")
url = f"https://www.alphavantage.co/query"
params = {
"function": "GLOBAL_QUOTE",
"symbol": symbol,
"apikey": api_key
}
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
# ... rest of parsing logic
Read each section: imports, class definitions, function signature, then the function body line by line.
Asking AI to Explain
When you hit something unfamiliar, ask. If you don't understand response.raise_for_status(), prompt:
Explain what response.raise_for_status() does in this code.
When would it raise an exception?
AI excels at explaining code. Use this to learn, not just to get answers. Understanding why code works a certain way helps you modify it later.
Your Review Checklist
Before running any generated code, verify these points:
Security: Are API keys loaded from environment variables? Is user input validated before use?
Error handling: What happens if the network fails? If the API returns an error? If the input is invalid?
Correctness: Does the code actually do what you asked? Check that return values match your requirements.
Edge cases: What if someone passes an empty string? A symbol with special characters? Does the code handle these gracefully?
When Something Looks Wrong
If you spot an issue, don't just fix it yourself — ask AI to fix it and explain the change. This teaches you the pattern for next time.
For example: "This code doesn't handle the case where the API returns an empty 'Global Quote' object. Add a check for that and raise StockNotFoundError."
The goal isn't just working code — it's code you understand and can maintain.