Reviewing Code Isn't Hunting for Typos
Many people, reviewing code for the first time, think it's about finding where it's written wrong or whether the indentation is right. Real code review answers three entirely different questions: can this code run correctly, will it be easy to change later, and does it follow the team's rules.
These three layers, from bottom to top, get more abstract and harder to judge with each step.
Layer One: Can It Run Correctly
Correctness is the foundation: is the logic right, are edge cases handled, will an exception crash the program. Get this layer wrong and no matter how pretty the code is, it's useless.
def average(nums):
return sum(nums) / len(nums)
# 传进来一个空列表会怎样?
average([]) # ZeroDivisionError!Layers Two and Three: Easy to Maintain, Compliant with the Rules
Maintainability cares about you and your colleagues six months from now: are the variable names understandable, are functions too long, is there duplicated code. Code that runs now but can't be read or changed is debt you've taken on.
Team conventions are the topmost layer: how comments are written, how files are named, whether to use a certain library—these have no absolute right or wrong, only "this is how this team decided."
自测 · 学完检查一下
想真正动手做题、记进度、攒连胜?到互动课里练。
What's the correct order of code review's three layers, from bottom to top?
答案:Correctness → maintainability → team conventions
The foundation is "can it run correctly," then "is it easy to maintain," and only at the top "does it follow team conventions."
"This function doesn't handle being passed an empty list"—which layer does this problem belong to?
答案:Correctness
An empty list is a classic edge case; mishandling it makes the program error out—it belongs to the "can it run correctly" correctness layer.
"Variable names like `a`, `b`, `tmp2` everywhere, so no one can read it"—which layer is this?
答案:Maintainability
The code runs, but bad naming makes future edits painful—it belongs to the "is it easy to maintain" layer.
Judge: "the team requires a one-line doc comment on every public function, and this code has none"—this belongs to the team-conventions layer.
答案:Correct
Comment-format rules of the "the team decided it this way" kind have no absolute right or wrong—they belong to the team-conventions layer.
Judge: compared with "does the program run correctly," "is this name clear enough" is harder to fully hand to a machine to judge.
答案:Correct
Whether it runs correctly can be auto-verified by tests, while whether a name is clear often needs human contextual judgment—the higher the layer, the harder to automate.
Among the three layers of code review, the foundation that must be guaranteed first is ____ness.
答案:correct
Correctness is the foundation; if the code can't even run right, maintainability and team conventions are moot.