Lec 9 Codereviews
Lec 9 Codereviews
Software QA
It is difficult to assure the quality of a software product.
• What are we assuring?
– Building the right system? Building the system right?
– Presence/absense of good/bad properties?
– Identifying errors? Confidence in the absence of errors?
– Robust? Safe? Secure? Available? Reliable?
– Understandable? Modifiable? Cost-effective? Usable? …
4
Mechanics
• who: Original developer and reviewer, sometimes together in
person, sometimes offline.
6
Actual Studies
• Average defect detection rates
– Unit testing: 25%
– Function testing: 35%
– Integration testing:45%
– Design and code inspections: 55% and 60%.
8
Variations
• inspection: A more formalized code review with:
– roles (moderator, author, reviewer, scribe, etc.)
– several reviewers looking at the same piece of code
– a specific checklist of kinds of flaws to look for
• possibly focusing on flaws that have been seen previously
• possibly focusing on high-risk areas such as security
– specific expected outcomes (e.g. report, list of defects)
10
Code Reviews at Yelp
• "At Yelp we use review-board. An engineer works on a branch
and commits the code to their own branch. The reviewer then
goes through the diff, adds inline comments on review board
and sends them back."
• "The reviews are meant to be a dialogue, so typically comment
threads result from the feedback. Once the reviewer's
questions and concerns are all addressed they'll click "Ship It!"
and the author will merge it with the main branch for
deployment the same day.”
11
Code Reviews at WotC
• "At Wizards we use Perforce for SCM. I work with stuff that
manages rules and content, so we try to commit changes at
the granularity of one bug at a time or one card at a time. Our
team is small enough that you can designate one other person
on team as a code reviewer."
• "Usually you look at code sometime that week, but it depends
on priority. It’s impossible to write sufficient test harnesses for
the bulk of our game code, so code reviews are absolutely
critical."
12
Code Reviews at Facebook
• "At Facebook, we have an internally-developed web-based tool to aid the
code review process. Once an engineer has prepared a change, she submits
it to this tool, which will notify the person or people she has asked to
review the change, along with others that may be interested in the change
-- such as people who have worked on a function that got changed. At this
point, the reviewers can make comments, ask questions, request changes,
or accept the changes. If changes are requested, the submitter must submit
a new version of the change to be reviewed. All versions submitted are
retained, so reviewers can compare the change to the original, or just
changes from the last version they reviewed. Once a change has been
submitted, the engineer can merge her change into the main source tree
for deployment to the site during the next weekly push, or earlier if the
change warrants quicker release."
13
Code Reviews in CSE 403
• code reviews required for each code phase (Alpha, Beta, V1)
– Idea: 3 per week, one review per developer
– Not required to review before checkin
• Need to document code review in some fashion
– Email thread, checklist, tools, etc.
• Exact structure up to group: something between walkthrough
and inspection
• Don't review "trivial" code but "trivial" threshold is probably
lower than you think
– example: exception tests added, good
14
Counter-argument 1
• "I don't like doing a code review of every commit. It takes too
long. I want to make lots of commits and it slows me down."
• Responses?
– Maybe you are making too many commits.
– Commit coherent, complete changes to the code base, not
incomplete work or tiny changes.
– If something was quick to write, or a small change, it will also be
quick to code review.
– If you check in buggy code, you'll lose more time than it would
have taken to code review it and (hopefully) find the bug.
15
Counter-argument 2
• "We tried doing code reviews, but it was not useful. We never
find any bugs or errors; the code is always approved."
• Responses?
– Maybe the reviewer should be more thorough. Almost all reviews
of non-trivial code uncover at least some issues to look into.
– Sometimes the point is not to find flaws and fix them, but also to
make sure that another developer is familiar with that code.
– The fact that the original coder knew it would be reviewed
increases the chance that they wrote better code to start with.
16
Code Review Example
• What changes (if any) would you suggest?
public class Account {
double principal,rate; int daysActive,accountType;
public static final int STANDARD = 0, BUDGET=1,
PREMIUM=2, PREMIUM_PLUS = 3;
} ...
18
Improved Code
/** An individual account. Also see CorporateAccount. */
public class Account {
/** The varieties of account our bank offers. */
public enum Type {STANDARD, BUDGET, PREMIUM, PREMIUM_PLUS}
/** The yearly, compounded rate (at 365.25 days per year). */
private double rate;
/** Return the sum of broker fees for all given accounts. */
public static double calculateFee(Account[] accounts) {
double totalFee = 0.0;
for (Account account : accounts) {
if (account.isPremium()) {
totalFee += BROKER_FEE_PERCENT * account.interest();
}
}
return totalFee;
}
}
20
Code Review Checklist
• (See course web site)
22