Learn to read code before you trust it (including code written by AI)
6 min read
In the first article, I wrote that even with AI helping us write code, the person who has to judge whether that code is right or wrong, well designed or not, and safe or not, is still us. This article goes into detail about how to make that judgment in practice.
Why reading comes first
At school, most of our practice is writing code. Once we start working, though, most of a software engineer's time goes into reading. We read our teammates' code, old code whose author left long ago, the code of the libraries we use, and our own code from six months back, which by now is hardly different from someone else's.
AI shifts this balance even further. Code is produced faster than we can read it, and code written by AI usually looks tidy, with good variable names and thorough comments. That makes it easy to trust by accident, even though a tidy surface tells us nothing about whether the code actually works.
If we can't read code, we become people who just click "accept" without knowing what we are letting into the system.
What to look at, and in what order
Reading code well doesn't mean going line by line from top to bottom. It means asking the right questions in the right order. I suggest these four, ordered from the most damaging problems to the least.
One: does the code do what was asked, and only that? Start by comparing the code with the task, not with your feeling that the code looks good. Code often works, but does something different from what was needed, or quietly does extra things nobody asked for, such as editing other files, changing defaults or adding a new library.
Two: what happens in unusual cases? Think about empty input, unusually long input, negative values, zero, a network failure, or a user who presses the button twice. Most code works for the cases its author thought about. The problems are usually in the cases nobody thought about.
Three: can it corrupt data or open a security hole? Who can call this code? Where does user input end up? Does it delete or overwrite data with no way back? Mistakes like these may show no symptoms during testing, then do serious damage in production.
Four: will the next person understand it? Do the names say what they mean? Is the function too long? Is there anywhere you have to guess what the author intended? This matters, but it should come last, because code that is easy to read but wrong is more dangerous than code that is hard to read but right.
An example: code that looks right
Suppose we ask AI to write a function that checks whether a user may delete a post. The rule is that administrators (admin) and editors (editor) can delete any post, while regular users can delete only their own. We get this code:
def can_delete_post(user, post):
"""Admins and editors can delete any post; authors can delete their own."""
if user.role == "admin" or "editor":
return True
return post.author_id == user.id
The code is short and easy to read, and its docstring matches the requirement word for word. Test it with an admin or with the post's author and it gives the right answer every time. Many people would click "accept" here.
Now apply question two, "what happens in unusual cases?", by asking: if a regular user tries to delete someone else's post, what does this function return?
The answer is True, every time. In Python, the condition user.role == "admin" or "editor" doesn't mean "the role is admin or editor". It means (user.role == "admin") or ("editor"), and a non-empty string like "editor" is always true. The result is that anyone can delete anyone's post.
This is a security hole that easily passes shallow testing, and nothing about how the code looks gives it away. What catches it is deliberate reading with the right questions. The correct code looks like this:
def can_delete_post(user, post):
if user.role in ("admin", "editor"):
return True
return post.author_id == user.id
Once you find a mistake like this, also write a test for the case that went wrong, namely that a regular user must not be able to delete someone else's post, so the same problem can't come back.
Notice too that question one helps here. If, while reading, you wonder "should editors really be able to delete other people's posts?", that is a question about the requirement, not about the code, and you should ask whoever set the requirement rather than guess.
From my own experience
On one job, I had to change the data type of a table's primary key column from INTEGER to BIGINT (in PostgreSQL), and every stored procedure related to that main table had to be updated to match. I trusted the AI a little too much. It made the changes, but somehow it didn't copy the queries over exactly as they were, which introduced a logic error. Callers kept working normally, because the function signatures were unchanged and so was the structure of what they returned. By the time we realized something was wrong, several days had passed. Luckily, it didn't do much damage to the business. If I had double-checked that the new stored procedures changed only the BIGINT column, this problem would never have reached production.
Side notes
- Later, I'll explain why my team chose stored procedures even though we could have written the queries in the application.
- If we don't trust AI at all, we end up working harder than before. Later, I'll share the principles and methods that let us trust AI to write code for us about 95% of the way.
Make it a habit
Reading code is a skill you can practise, and the more you practise, the faster you get. A few short tips to get started:
- Read other people's code regularly, for example by reviewing your teammates' work or reading the code of libraries you use every day.
- Before you read, write the task down as a short sentence, then check whether the code does that.
- When you hit a line you don't understand, don't skip it. Run it, or write a small test to prove your understanding.
- Treat code written by AI like code a new teammate sends you for review: with respect, but without trusting it right away.
The checklist
- Does the code do what was asked, and only that?
- Are empty input, odd values, failures and repeats handled?
- Is there any way for data to be corrupted, or for someone without permission to do what they shouldn't?
- Will the next person understand it without guessing?
- Is there a test for every case that went wrong before?
AI helps us get code faster, but responsibility for the code that reaches production is always ours. Knowing how to read it is what lets us actually carry that responsibility.