When AI Writes Code You Do Not Understand
Three questions that decide whether unfamiliar AI-written code is safe to ship, a read order that works when you are not fluent, and the parts you never get to skip.
Understanding every line is not the bar. What matters is understanding what happens when the code goes wrong. That is a much smaller problem, and it is the one that decides whether the code can ship.
Full comprehension is the standard people reach for because it sounds responsible. In practice nobody applies it. You already run code you have not read, in every dependency you installed this year. The question was never whether you understand it. The question is what it can reach.
The three questions
Ask these before anything else. They take two minutes and they decide how much of the rest you need.
1. What can it touch? Does it write to the database, call a paid API, send email, move money, or touch files outside its own directory? Code that only reads and returns a value has a small blast radius. Code that deletes rows has a large one.
2. What happens if it is wrong? Wrong in the sense of silently returning bad data, not crashing. A crash you will find. A function that returns the wrong total and keeps going is the expensive case.
3. Can I tell that it worked? Is there a test, a log line, or a visible output that would differ if the code were broken? If the answer is no, that is the thing to fix first, before you worry about reading it.
If a piece of code reads from one table, returns a value, is covered by a test and has an obvious output, you can ship it without following every line. If it writes to three tables with no test, you read all of it, however long that takes. Most code sits in between and the questions tell you where.
A read order that works
Reading top to bottom is the slowest way to understand unfamiliar code, because the top of a file is usually imports and setup. Read the boundaries first.
The signature and the return. What goes in, what comes out. Often this alone tells you whether the code is doing what you asked.
The side effects. Search the block for anything that writes: database calls, file operations, network requests, anything mutating a variable that outlived the function. This is question one, answered concretely.
The error paths. Find every
catch,except,if err != nil. An empty catch block is the single most common serious defect in generated code, because it turns a failure into a silent wrong answer.The edge cases. What happens with empty input, a null, a list of one, a duplicate. Generated code is typically correct on the happy path and vague at the boundaries.
The middle. Only now, and often you no longer need to.
Four out of five times you will have what you need by step three. The middle of a function is usually the part a model gets right, because it is the part that resembles the most code it has seen.
Make it explain, then check the explanation
Asking the model to explain its own code is useful and slightly circular, so structure it to produce something checkable.
Weak: "explain this function." You will get fluent prose that matches the code's intent rather than its behaviour.
Better: "Walk through this function with the input []. What does each line do and what does it return?" Now you have a specific claim you can test. Run it with [] and see.
Better still, ask for the thing it did not do: "What inputs would make this function return a wrong result without raising an error?" A model will often tell you honestly, and the answer is the test you should write next. Getting an agent to explain a stack trace uses the same principle: ask for a trace, not a summary.
The rule underneath: never accept an explanation you cannot verify. If you can run it, run it. If you cannot run it, the explanation is a hypothesis.
The parts you never skip
Some code does not get the blast-radius discount, no matter how tired you are:
Anything touching authentication or authorisation. A subtle mistake here is invisible until it is on the internet.
Anything handling money. Rounding, currency, tax, refunds. The failure mode is quiet and the consequences are legal.
Anything deleting data. Read every line, and check the where clause twice.
Anything that runs on a schedule. A bug in a nightly job runs unattended for a week before anyone looks.
Anything that writes to a file path built from input. Path construction from user input is how directories get escaped, and it sits squarely in the OWASP Top Ten for a reason.
For these, comprehension is not optional and neither is a test. Files your coding agent should never write covers the related question of where to not let it go in the first place.
When you still cannot follow it
Sometimes the code is simply beyond where you are, and that is a normal place to be. Three moves, in order:
Ask for the simpler version. "Rewrite this using only loops and if statements, no comprehensions or clever one-liners." Generated code is often needlessly dense. The simple version is usually just as fast and you can read it.
Ask what it would look like with the abstraction removed. If there is a decorator, a metaclass or a generic wrapper you do not follow, ask for the inlined version for your specific case. You are reading one path, not the general machinery.
Decide not to ship it. A legitimate and underused option. If you cannot understand it, cannot test it, and it touches something that matters, then the risk is not that the code is wrong. The risk is that you will not be able to fix it at 11pm when it breaks. Ship the boring version you understand.
That last point is the real cost of code you do not understand. It is not today's bug. It is that every future change to that file becomes a negotiation with a stranger. Keeping an AI-built app maintainable is mostly about not accumulating these. If something is already broken, debugging AI-generated code is the more immediate read, and the AI coding tools guide covers picking tools that make review easier in the first place.
Questions people ask
Is it bad practice to ship code I do not fully understand?
It depends entirely on what it can reach. Shipping an unread pure function with a test is routine. Shipping unread code that deletes rows is not.
How do I get better at reading generated code?
Read the boundaries first, signature, side effects and error paths, before the body. That order builds the habit of looking for what the code touches rather than what it says.
Should I ask the AI to add comments so I understand it?
Comments are the easiest thing to generate and the easiest to get wrong, because they describe intent rather than behaviour. Ask for a trace of a specific input instead.
What if it works and I cannot explain why?
Write a test that pins the behaviour you observed, then simplify until you can explain it. Working code you cannot explain is a maintenance problem waiting for a deadline.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


