1、代码评审开发者指南

Introduction(介绍)

A code review is a process where someone other than the author(s) of a piece of code examines that code.
所谓的代码审查(code review)就是指让别人来审查自己的代码的过程。

At Google, we use code review to maintain the quality of our code and products.
在Google内部,我们会通过代码审查来保证代码和产品的质量。

This documentation is the canonical description of Google’s code review processes and policies.
本文档详述了Google是如何进行代码审查的以及所采用的策略。

This page is an overview of our code review process. There are two other large documents that are a part of this guide:

  • How To Do A Code Review: A detailed guide for code reviewers.
  • The CL Author’s Guide: A detailed guide for developers whose CLs are going through review.

本页内容大体介绍了下代码审查的过程是怎样的。代码审查指南包含了两大部分:

  • 如何进行代码审查 详述了代码评审人应该如何进行代码审查。
  • 变更列表作者指南: 详述了提交待审核变更列表的开发人员应该如何做。

What Do Code Reviewers Look For?(代码审查应该关注哪些内容?)

Code reviews should look at:

  • Design: Is the code well-designed and appropriate for your system?
  • Functionality: Does the code behave as the author likely intended? Is the way the code behaves good for its users?
  • Complexity: Could the code be made simpler? Would another developer be able to easily understand and use this code when they come across it in the future?
  • Tests: Does the code have correct and well-designed automated tests?
  • Naming: Did the developer choose clear names for variables, classes, methods, etc.?
  • Comments: Are the comments clear and useful?
  • Style: Does the code follow our style guides?
  • Documentation: Did the developer also update relevant documentation?

代码审查内容应该关注以下几点:

  • 设计: 代码是否设计良好且对于系统而言是否适合?
  • 功能: 代码实现的行为是否达到了作者想要的效果?代码实现的行为对于用户是否友好?
  • 复杂性: 代码能否写得更简单点吗?是否能让别的开发人员易于理解而且易于使用?
  • 测试: 代码是否包含了正确的,设计良好的自动化测试?
  • 命名: 开发人员是否为变量、类、方法等选取了清晰的名字?
  • 注释: 注释是否含义清晰并且有用?
  • 代码规范: 代码是否遵循了归约规范
  • 文档: 开发人员是否同步更新了相关文档?

See How To Do A Code Review for more information.
具体内容详见《如何进行代码审查》

Picking the Best Reviewers(挑选最好的评审人)

In general, you want to find the best reviewers you can who are capable of responding to your review within a reasonable period of time.
通常情况下,你会倾向于寻找你所能找到的最好的评审人,以保证你的代码能尽快得到审查。

The best reviewer is the person who will be able to give you the most thorough and correct review for the piece of code you are writing. This usually means the owner(s) of the code, who may or may not be the people in the OWNERS file. Sometimes this means asking different people to review different parts of the CL.
好的评审人能够给予你编写的代码较为彻底和正确的审查。这意味着,相关代码的所有者(owner)可能在所有者文件里也可能不在所有者文件里。有时可能需要让不同的人来去审查变更列表的不同部分。

If you find an ideal reviewer but they are not available, you should at least CC them on your change.
如果你找到了理想的评审人但他们却因为某些原因无法为你审查时,你至少需要将你的变更内容抄送给他们。

In-Person Reviews (and Pair Programming)(面对面审查(以及结对编程))

If you pair-programmed a piece of code with somebody who was qualified to do a good code review on it, then that code is considered reviewed.
如果某部分代码你是与小伙伴结对编程写出来的,同时这个小伙伴被认为能进行合格的代码审查,那么这部分代码也可以被认为是进行过了代码审查。

You can also do in-person code reviews where the reviewer asks questions and the developer of the change speaks only when spoken to.
你也可以采用面对面的代码审查方式,在这种方式下,评审人提出问题,被问的开发人员进行描述答复。