Sub topic 11
Maintainability and fit with your codebase
Code that passes its tests can still be a poor change. This chapter covers the part of review that tests rarely catch: whether the code belongs in your project. It assumes you can read a Git diff, write unit tests, and have used an AI coding assistant. It also assumes you have already checked the change against the requirement and read the diff for correctness.
Why fit matters
An AI assistant sees a limited slice of your repository. It usually sees the files you pointed it to, plus whatever it searched for. It does not know your team's habits, your past decisions, or the helper someone wrote last year in another directory. So it often writes code that is correct in isolation and foreign in context.
Maintainability costs show up later. Someone has to read this code, change it, and debug it at a bad hour. A reviewer's question here is: if a teammate had written this, would I approve it into this codebase?
Common fit problems in AI-generated changes
| Problem | What it looks like | What to check |
|---|---|---|
| Duplicated logic | A new helper that does what an existing one already does | Search the repo for the behavior, not only the name |
| Convention drift | `camelCase` in a `snake_case` project, different error handling, different logging | Read two or three neighboring files |
| Needless abstraction | A factory, base class, or config layer for a single use | Ask what second use case justifies it |
| Scope creep | Renames, reformatting, or "cleanups" outside the task | Look at `git diff --stat` for unexpected files |
| Narrating comments | `# increment i by one` | Delete comments that restate the code |
| Dead or speculative code | Unused parameters, unreachable branches, "future-proof" options | Search for callers |
| New dependency | A package added for something the standard library or an existing dependency does | Check the manifest diff |
| Wrong layer | Database calls in a view, business rules in a controller | Compare with where similar code lives |
Duplicated logic diverges
Duplication is the most common fit problem, and it is more dangerous than it looks. Two near-copies of a function do not stay identical. Here is a project that already has a slugify helper, and an assistant that wrote its own instead.
import re
# Already in the project (app/text.py)
def slugify(title: str) -> str:
return "-".join(title.lower().split())
# Added by the assistant (app/blog.py)
def make_slug(title: str) -> str:
s = title.lower().strip()
s = re.sub(r"[^a-z0-9]+", "-", s)
return s.strip("-")
if __name__ == "__main__":
title = "Hello, World!"
print(slugify(title)) # hello,-world!
print(make_slug(title)) # hello-worldEach function is reasonable on its own. Together they produce different URLs for the same title, depending on which code path created the slug. Links break, and the bug report will not mention slugs.
You have three options, and the choice is a judgment call:
- Reject the duplicate and call
slugify, if its behavior is acceptable. - Improve
slugifyso it handles punctuation, add tests, and use it everywhere. This is right if the existing helper is wrong. - Keep both only if the behaviors must differ, and name them so the difference is obvious.
Do not accept the duplicate "for now". Cleanup you defer rarely happens.
Finding what the assistant missed
You need to search for what the assistant did not find. Search for behavior as well as names, because the existing helper may be called something unexpected.
# Look for existing helpers by likely names and by behavior
git grep -n -i -E "slug|normalize.*title|kebab"
# Look at how neighboring code handles errors and logging
git grep -n -E "logger\.(error|exception)" -- app/ | head -20
# See which files the change touched
git diff --stat
# Check whether the dependency manifest changed
git diff -- requirements.txt pyproject.toml package.jsonThen open two or three files near the change and read them as a style sample. You are looking for how they name things, how they return errors (exceptions, result objects, None), where they log, and how they are tested.
Convention drift
Drift is small inconsistency repeated over many changes. One file that returns None on failure while its neighbors raise exceptions looks harmless. After twenty such files, a caller cannot predict how any function fails.
Check these against your project's norms:
- Error handling. Does the code raise, return error values, or swallow exceptions? A bare
except Exception: passis a red flag in any style. - Naming. Do names match the domain vocabulary your team uses? An assistant may call a "customer" a "user" because that is common in training data.
- Structure. Does the code live in the module where similar code lives?
- Logging and configuration. Does it use the project's logger and settings mechanism, or does it call
printand read environment variables directly? - Tests. Do new tests follow the existing layout, fixtures, and naming?
If your project has a linter and formatter configuration, mechanical style is already covered. The remaining drift is structural, and only a human reading neighboring code will notice it.
Over-engineering
Assistants tend to produce more structure than the task needs. Asked for a function that sends one kind of notification, one may return an abstract NotificationStrategy, a registry, and a config class.
# Over-built for a single use
class NotificationStrategy:
def send(self, user, message):
raise NotImplementedError
class EmailStrategy(NotificationStrategy):
def send(self, user, message):
print(f"emailing {user}: {message}")
STRATEGIES = {"email": EmailStrategy()}
def notify(kind, user, message):
STRATEGIES[kind].send(user, message)
# What the requirement needed
def send_email(user, message):
print(f"emailing {user}: {message}")The first version is not wrong. It adds three names, a lookup table, and a new failure mode (KeyError on an unknown kind) in exchange for flexibility nobody asked for. Ask for the concrete second use case. If there is none, request the simpler version. Adding the abstraction later, when a real second case arrives, is cheap.
The opposite failure also exists. A 90-line function with nested conditions and no structure is hard to change. Judge by whether you can explain what each piece is for.
Scope creep inside the diff
Look at the list of changed files before reading any code. An assistant asked to fix one bug may also rename variables, reorder imports, or reformat a file. Each extra line is something you must review and something that makes the history harder to read.
Stage only what belongs to the task:
git add -pReject hunks that are not part of the change. If the refactor is worth doing, do it in a separate commit with its own review. This also keeps git blame useful.
Comments, names, and readability
Read the code as the next maintainer would, without the prompt that produced it.
- Comments should explain why, such as a constraint or a surprising decision. Remove comments that restate what the line does.
- A comment that contradicts the code is worse than none. Assistants sometimes edit code and leave the old comment.
- Names should say what a value is.
data,result,temp, andhandle_stuffhide intent. - Look for leftovers: unused imports, unused parameters, commented-out blocks, and debug prints.
- Check that a function does one describable thing. If you need "and" to summarize it, consider splitting it.
A decision flow for fit
Pushing back effectively
When fit is the problem, tell the assistant what to change and give it context it lacked. A useful revision request names the existing code:
Do not add make_slug. Use slugify from app/text.py. If it mishandles punctuation,
fix it there and add a test in tests/test_text.py. Keep the diff to those two files.Rejecting a change and restarting with better context is often faster than patching a poor structure line by line.
What is fine to let go
Not every difference is worth a round trip. Accept small style differences that a formatter will fix, and accept names that are slightly off but clear. Spend your review effort on duplication, wrong layering, new dependencies, and hidden behavior differences, because those cost the most to fix later.
Exercise
An assistant adds parse_date(s) to utils/dates.py in a project that already has parse_iso_date(s) in core/time.py. The new function also accepts "12/05/2025", and its tests pass. Name two things to check before accepting, and give one reasonable outcome.
Show answer
Check (1) whether parse_iso_date already covers the requirement, and how the two functions differ on the same inputs (for example, whether "12/05/2025" is day-first or month-first, and what each does on bad input). Check (2) whether other code or the requirement relies on the extra formats, and whether the new function follows the project's error handling. A reasonable outcome is to reject parse_date and reuse parse_iso_date. If the extra formats are really required, extend parse_iso_date once, add tests for the ambiguous cases, and keep a single date parser.