2011年7月31日星期日

Re: Things Everyone Should Do: Code Review

翻译得不好



谷歌是如何做代码审查的

本文是从 Things Everyone Should Do: Code Review 这篇文章翻译而来。

Mark CC and SS

本文的作者 Mark CC


在上一篇文章中提到过,我已经不在Google工作了。我还没有想清楚应该去哪里—有两三个非常好的工作机会摆在我面前。因为在这段做决定时间里,我不再受雇于任何人,我想可以写一些专业性的东西,一些很有趣,但也会在同事和管理工作中导致关系紧张的东西。

Google是一个非常优秀的公司。他们做出了很多令人称赞的东西—既是公司外部,人们可以看到的东西,也是公司内部。有一些在公司内部并不属于保密的事情,在外部并没有给予足够广泛的讨论。这就是我今天要说的。

让Google的程序如此优秀的一个最重要的事情看起来是非常的简单:代码审查。并不是只有Google做这个事情—代码审查已经被广泛的认可为一种非常好的做法,很多人都在这样做。但我还没有看到第二家这样大的公司能把这种事情运用的如此普遍。在Google,没有程序,任何产品、任何项目的程序代码,可以在没有经过有效的代码审查前提交到代码库里的。

所有人都要经过代码审查。并且很正规的:这种事情应该成为任何重要的软件开发工作中一个基本制度。并不单指产品程序——所有东西。它不需要很多的工作,但它的效果是巨大的。

从代码审查里能得到什么?

很显然:在代码提交前,用第二群眼睛检查一遍,防止bug混入。这是对其最常见的理解,是对代码审查的好处的最广泛的认识。但是,依我的经验来看,这反倒是它最不重要的一点。人们确实在代码审查中找到了bug。可是,这些在代码审查中能发现的绝大部分bug,很显然,都是微不足道的bug,程序的作者花几分钟的时间就能发现它们。真正需要花时间去发现的bug不是在代码审查里能找到的。

代码审查的最大的功用是纯社会性的。如果你在编程,而且知道将会有同事检查你的代码,你编程态度就完全不一样了。你写出的代码将更加整洁,有更好的注释,更好的程序结构——因为你知道,那个你很在意的人将会查看你的程序。没有代码审查,你知道人们最终还是会看你的程序。但这种事情不是立即发生的事,它不会给你带来同等的紧迫感,它不会给你相同的个人评判的那种感受。

还有一个非常重要的好处。代码审查能传播知识。在很多的开发团队里,经常每一个人负责一个核心模块,每个人都只关注他自己的那个模块。除非是同事的模块影响了自己的程序,他们从不相互交流。这种情况的后果是,每个模块只有一个人熟悉里面的代码。如果这个人休假或——但愿不是——辞职了,其他人则束手无策。通过代码审查,至少会有两个人熟悉这些程序——作者,以及审查者。审查者并不能像程序的作者一样对程序十分了解——但他会熟悉程序的设计和架构,这是极其重要的。

当然,没有什么事情能简单的做下来的。依我的经验,在你能正确的进行代码审查前,你需要花时间锻炼学习。我发现人们在代码审查时经常会犯一些错误,导致不少麻烦——尤其在一些缺乏经验的审查者中经常的出现,他们给了人们一个很遭的代码审查的体验,成为了人们接受代码审查制度的一个障碍。

最重要的一个原则:代码审查用意是在代码提交前找到其中的问题——你要发现是它的正确。在代码审查中最常犯的错误——几乎每个新手都会犯的错误——是,审查者根据自己的编程习惯来评判别人的代码。

对于一个问题,通常我们能找出十几种方法去解决。对于一种解决方案,我们能有百万种编码方案来实现它。作为一个审查者,你的任务不是来确保被审查的代码都采用的是你的编码风格——因为它不可能跟你写的一样。作为一段代码的审查者的任务是确保由作者自己写出的代码是正确的。一旦这个原则被打破,你最终将会倍感折磨,深受挫折——这可不是我们想要的结果。

问题在于,这种错误是如此的普遍而易犯。如果你是个程序员,当你遇到一个问题,你能想到一种解决方案——你就把你想到的方案作为标准答案。但事情不是这样的——作为一个好的审查者,你需要明白这个道理。

代码审查的第二个易犯的毛病是,人们觉得有压力,感觉非要说点什么才好。你知道作者用了大量的时间和精力来实现这些程序——不该说点什么吗?

不,你不需要。

只说一句“哇,不错呀”,任何时候都不会不合适。如果你总是力图找出一点什么东西来批评,你这样做的结果只会损害自己的威望。当你不厌其烦的找出一些东西来,只是为了说些什么,被审查人就会知道,你说这些话只是为了填补寂静。你的评论将不再被人重视。

第三是速度。你不能匆匆忙忙的进行一次代码审查——但你也要能迅速的完成。你的同伴在等你。如果你和你的同事并不想花太多时间进行代码复查,你们很快的完成,那被审查者会觉得很沮丧,这种代码审查带来的只有失望的感觉。就好象是打搅了大家,使大家放下手头的工作来进行审查。事情不该是这样。你并不需要推掉手头上的任何事情来做代码审查。但如果中途耽误了几个小时,你中间还要休息一会,喝杯茶,冲个澡,或谈会儿闲话。当你回到审查现场,你可以继续下去,把事情做完。如果你真是这样,我想没有愿意在那干等着你。


On Mon, Aug 1, 2011 at 1:41 PM, WanZheng <wanzheng@gmail.com> wrote:

Things Everyone Should Do: Code Review

Jul 06 2011 Published by  under Programming

As I alluded to in my last post (which I will be correcting shortly), I no longer work for Google. I still haven't decided quite where I'm going to wind up - I've got a couple of excellent offers to choose between. But in the interim, since I'm not technically employed by anyone, I thought I'd do a bit of writing about some professional things that are interesting, but that might have caused tension with coworkers or management.

Google is a really cool company. And they've done some really amazing things - both outside the company, where users can see it, and inside the company. There are a couple of things about the inside that aren't confidential, but which also haven't been discussed all that widely on the outside. That's what I want to talk about.

The biggest thing that makes Google's code so good is simple: code review. That's not specific to Google - it's widely recognized as a good idea, and a lot of people do it. But I've never seen another large company where it was such a universal. At Google, no code, for any product, for any project, gets checked in until it gets a positive review.

Everyone should do this. And I don't just mean informally: this should really be a universal rule of serious software development. Not just product code - everything. It's not that much work, and it makes a huge difference.

What do you get out of code review?

There's the obvious: having a second set of eyes look over code before it gets checked in catches bugs. This is the most widely cited, widely recognized benefit of code review. But in my experience, it's the least valuable one. People do find bugs in code review. But the overwhelming majority of bugs that are caught in code review are, frankly, trivial bugs which would have taken the author a couple of minutes to find. The bugs that actually take time to find don't get caught in review.

The biggest advantage of code review is purely social. If you're programming and youknow that your coworkers are going to look at your code, you program differently. You'll write code that's neater, better documented, and better organized -- because you'llknow that people who's opinions you care about will be looking at your code. Without review, you know that people will look at code eventually. But because it's not immediate, it doesn't have the same sense of urgency, and it doesn't have the same feeling of personal judgement.

There's one more big benefit. Code reviews spread knowledge. In a lot of development groups, each person has a core component that they're responsible for, and each person is very focused on their own component. As long as their coworkers components don'tbreak their code, they don't look at it. The effect of this is that for each component, only one person has any familiarity with the code. If that person takes time off or - god forbid - leaves the company, no one knows anything about it. With code review, you have at least two people who are familiar with code - the author, and the reviewer. The reviewer doesn't know as much about the code as the author - but they're familiar with the design and the structure of it, which is incredibly valuable.

Of course, nothing is every completely simple. From my experience, it takes some time before you get good at reviewing code. There are some pitfalls that I've seen that cause a lot of trouble - and since they come up particularly frequently among inexperienced reviewers, they give people trying code reviews a bad experience, and so become a major barrier to adopting code review as a practice.

The biggest rule is that the point of code review is to find problems in code before it gets committed - what you're looking for is correctness. The most common mistake in code review - the mistake that everyone makes when they're new to it - is judging code by whether it's what the reviewer would have written.

Given a problem, there are usually a dozen different ways to solve it. Andgiven a solution, there's a million ways to render it as code. As a reviewer, your job isn't to make sure that the code is what you would have written - because it won't be. Your job as a reviewer of a piece of code is to make sure that the code as written by its author is correct. When this rule gets broken, you end up with hard feelings and frustration all around - which isn't a good thing.

The thing is, this is such a thoroughly natural mistake to make. If you're a programmer, when you look at a problem, you can see a solution - and you think of what you've seen as the solution. But it isn't - and to be a good reviewer, you need to get that.

The second major pitfall of review is that people feel obligated to say something. You know that the author spent a lot of time and effort working on the code - shouldn't you say something?

No, you shouldn't.

There is never anything wrong with just saying "Yup, looks good". If you constantly go hunting to try to find something to criticize, then all that you accomplish is to wreck your own credibility. When you repeatedly make things to criticize just to find something to say, then the people who's code you review will learn that when you say something, that you're just saying it to fill the silence. Your comments won't be taken seriously.

Third is speed. You shouldn't rush through a code review - but also, you need to do itpromptly. Your coworkers are waiting for you. If you and your coworkers aren't willing to take the time to get reviews done, and done quickly, then people are going to get frustrated, and code review is just going to cause frustration. It may seem like it's an interruption to drop things to do a review. It shouldn't be. You don't need to drop everything the moment someone asks you to do a review. But within a couple of hours, you will take a break from what you're doing - to get a drink, to go to the bathroom, to talk a walk. When you get back from that, you can do the review and get it done. If you do, then no one will every be left hanging for a long time waiting on you.



Things Everyone Should Do: Code Review

Things Everyone Should Do: Code Review

Jul 06 2011 Published by  under Programming

As I alluded to in my last post (which I will be correcting shortly), I no longer work for Google. I still haven't decided quite where I'm going to wind up - I've got a couple of excellent offers to choose between. But in the interim, since I'm not technically employed by anyone, I thought I'd do a bit of writing about some professional things that are interesting, but that might have caused tension with coworkers or management.

Google is a really cool company. And they've done some really amazing things - both outside the company, where users can see it, and inside the company. There are a couple of things about the inside that aren't confidential, but which also haven't been discussed all that widely on the outside. That's what I want to talk about.

The biggest thing that makes Google's code so good is simple: code review. That's not specific to Google - it's widely recognized as a good idea, and a lot of people do it. But I've never seen another large company where it was such a universal. At Google, no code, for any product, for any project, gets checked in until it gets a positive review.

Everyone should do this. And I don't just mean informally: this should really be a universal rule of serious software development. Not just product code - everything. It's not that much work, and it makes a huge difference.

What do you get out of code review?

There's the obvious: having a second set of eyes look over code before it gets checked in catches bugs. This is the most widely cited, widely recognized benefit of code review. But in my experience, it's the least valuable one. People do find bugs in code review. But the overwhelming majority of bugs that are caught in code review are, frankly, trivial bugs which would have taken the author a couple of minutes to find. The bugs that actually take time to find don't get caught in review.

The biggest advantage of code review is purely social. If you're programming and youknow that your coworkers are going to look at your code, you program differently. You'll write code that's neater, better documented, and better organized -- because you'llknow that people who's opinions you care about will be looking at your code. Without review, you know that people will look at code eventually. But because it's not immediate, it doesn't have the same sense of urgency, and it doesn't have the same feeling of personal judgement.

There's one more big benefit. Code reviews spread knowledge. In a lot of development groups, each person has a core component that they're responsible for, and each person is very focused on their own component. As long as their coworkers components don'tbreak their code, they don't look at it. The effect of this is that for each component, only one person has any familiarity with the code. If that person takes time off or - god forbid - leaves the company, no one knows anything about it. With code review, you have at least two people who are familiar with code - the author, and the reviewer. The reviewer doesn't know as much about the code as the author - but they're familiar with the design and the structure of it, which is incredibly valuable.

Of course, nothing is every completely simple. From my experience, it takes some time before you get good at reviewing code. There are some pitfalls that I've seen that cause a lot of trouble - and since they come up particularly frequently among inexperienced reviewers, they give people trying code reviews a bad experience, and so become a major barrier to adopting code review as a practice.

The biggest rule is that the point of code review is to find problems in code before it gets committed - what you're looking for is correctness. The most common mistake in code review - the mistake that everyone makes when they're new to it - is judging code by whether it's what the reviewer would have written.

Given a problem, there are usually a dozen different ways to solve it. Andgiven a solution, there's a million ways to render it as code. As a reviewer, your job isn't to make sure that the code is what you would have written - because it won't be. Your job as a reviewer of a piece of code is to make sure that the code as written by its author is correct. When this rule gets broken, you end up with hard feelings and frustration all around - which isn't a good thing.

The thing is, this is such a thoroughly natural mistake to make. If you're a programmer, when you look at a problem, you can see a solution - and you think of what you've seen as the solution. But it isn't - and to be a good reviewer, you need to get that.

The second major pitfall of review is that people feel obligated to say something. You know that the author spent a lot of time and effort working on the code - shouldn't you say something?

No, you shouldn't.

There is never anything wrong with just saying "Yup, looks good". If you constantly go hunting to try to find something to criticize, then all that you accomplish is to wreck your own credibility. When you repeatedly make things to criticize just to find something to say, then the people who's code you review will learn that when you say something, that you're just saying it to fill the silence. Your comments won't be taken seriously.

Third is speed. You shouldn't rush through a code review - but also, you need to do itpromptly. Your coworkers are waiting for you. If you and your coworkers aren't willing to take the time to get reviews done, and done quickly, then people are going to get frustrated, and code review is just going to cause frustration. It may seem like it's an interruption to drop things to do a review. It shouldn't be. You don't need to drop everything the moment someone asks you to do a review. But within a couple of hours, you will take a break from what you're doing - to get a drink, to go to the bathroom, to talk a walk. When you get back from that, you can do the review and get it done. If you do, then no one will every be left hanging for a long time waiting on you.


父亲是终身制

父亲是终身制


#郑在对话#22  郑渊洁和郑洪升对话录

 

父亲是终身制
郑渊洁: 老爸你好。现在是两位父亲的对话。又正值父亲节。我要感谢你将我带到这个世界上来。谢谢。(2011-06-13 05:42:35)
郑洪升:我记得你说过,其实父亲也要感谢孩子,是孩子使爸爸成为父亲的。你现在也是父亲,只不过咱俩的父龄不一样,我的资格老。 (2011-06-13 05:45:40)
父亲是终身制
郑渊洁:你的父龄是56年。我的父龄是28年。其他职业都有退休年龄,父亲这个“职业”没有退休年限,你今年7月2日就八十岁了,依然当着父亲。 (2011-06-13 05:50:42)
郑洪升: 父亲该是终身制。“终身制”现在是个负面词汇,但是用在父亲身上,是褒义词。 (2011-06-13 05:53:46)
父亲是终身制
郑渊洁: 怎么讲? (2011-06-13 05:54:14)
郑洪升:一个男人有了孩子,就成为父亲了。只要孩子活着,他就一直是父亲,直到自己离开这个世界。所以我说,父亲应该是终身制“职业”。或者说好的父亲是终身制,你要尽职尽责,确保你的孩子平安成长和生活,不能让白发人送黑发人的杯具出现。如果那样,这个父亲就退休了。 (2011-06-13 06:01:12)
父亲是终身制
郑渊洁: 是悲剧吧? (2011-06-13 06:01:54)
郑洪升:自从你写博客、微博以及现在的z星球白客后,我也成了“控”,接受很多网络词汇。例如将“悲剧”写成“杯具”。八十岁的人说“杯具”,其实是“洗具”,说明你脑子没有僵化,思想还年轻。 (2011-06-13 06:06:37)
父亲是终身制
郑渊洁:我明白你说的“父亲该是终身制”的含义了。合格的父亲应该重视孩子的道德品质教育、养成良好的健康生活习惯、遵纪守法例如交通安全。使得孩子幸福平安一生。如果孩子发生药家鑫那样的事,,使得父亲提前退休,其实是父亲的失职导致的。 (2011-06-13 06:14:32)
郑洪升:是的。但是有一种情况例外,虽然孩子离开了,等于没走,永远活着,父亲也依然是终身制,那就是孩子为了救别人而献身,这样的孩子,在他献身那一刻,实际上获得了永生,比如消防队员。 (2011-06-13 06:20:04)
父亲是终身制
郑渊洁: 我今天能成为作家并自食其力,你功不可没。在咱们相处的56年中,有三件事给我印象最深。 (2011-06-13 06:24:27)
郑洪升:我现在去书店时,经常能看到大部头的阐述家庭教育的书籍。我成为父亲时,不懂家庭教育,只知道做好自己的工作。那时条件差,全家只有一间屋子,我当时是石家庄高级步兵学校的教员,每天晚上要看书备课。(2011-06-13 06:33:52)
父亲是终身制
郑渊洁:这就是我印象最深刻的第一件事。从我出生起,几乎天天看见你趴在桌子上看书写字,甚至抱着我看书。而父亲的一举一动,是对孩子效果最强烈的教育。所以我从小对看书和写字产生了崇拜心理。如果你官职挺高,家里有两间屋子,咱俩分别住,又有保姆看我,我今天成为作家的可能性很小。你是抱着我看完《资本论》的。我还记得你总是拿笔往书上画道,我也要求画。后来你把那本《资本论》送给了我,我现在还收藏着,书上还有我一岁时做的“眉批”。 (2011-06-13 06:39:56)
 
郑洪升:我在父亲节时送给你一件礼物。你上小学认字后,老是缠着我要往纸上写字,我没办法了,就让你抄《人民日报》。你抄了很多。当时我收藏了一张,今天送给你。你抄的是1966年12月27日的《人民日报》,那篇文章的题目是《谈谈解放军的连队行军政治工作》。 (2011-06-13 06:44:02)
父亲是终身制
郑渊洁:能看到被父亲珍藏了45年的字迹,这样的孩子,真是幸福和幸运。从出生起就看到你看书和写字,是你这个父亲给我这个儿子留下深刻印象的第一件事。第二件事是我读小学二年级时。我的一篇作文被刊登在校刊上,你虽然很高兴,但是你从来没说过诸如“咱家要出作家了”“你要当作家”之类的给我施加压力的话,也没有给我制定当作家的培训计划。如果是那样,我肯定逆反,从此将写作视为痛苦。 (2011-06-13 06:54:48)
郑洪升:我那时可能因为忙,没时间规划你的人生,只能规划自己的人生。同时我天天写工作需要的文字,可能也写累了,不想让自己的孩子也这么写。 (2011-06-13 06:57:47)
父亲是终身制
郑渊洁:我觉得作为父亲,这就是最好的对孩子的家庭教育:只规划自己的人生,不规划孩子的人生。父亲在奋斗自己的人生时,孩子都看在眼里了。我小时候主动要求抄写《人民日报》,内容我已经忘了,但是我从抄写的过程中获得快乐,这种快乐,源于父亲能做的事,我也能做了。 (2011-06-13 07:00:47)
郑洪升:最近有个武汉的孩子被冠以“五道杠”,“罪名”之一是看《人民日报》。其实,报上的内容他不一定记得住,但是阅读这种习惯会让他终生受益。开卷有益就是这个意思。你郑渊洁小时候抄写《人民日报》,你长大了并没有成为官场里的人。 (2011-06-13 07:04:23)
父亲是终身制
郑渊洁:你给我留下深刻印象的第三件事,导致我一个人写一本期刊《童话大王》写了26年,还在继续写。我写作的初期是使用灌水钢笔。经常会写到一半时钢笔没水了,停止写作给钢笔灌墨水。1981年春节,我住在家里写中篇作品《皮皮鲁外传》,写了三万字居然每给钢笔灌过水。后来我才知道,只要我离开写字台,你就悄悄给我的钢笔灌水。从这以后,只要我写累了《童话大王》不想再写了,我就会想起你偷偷给我的钢笔灌水这件事,我就一个人把一本期刊写了26年。这就是父亲的威力。 (2011-06-13 07:10:39)
郑洪升:渊洁,作为父亲,我要谢谢你这个儿子,你让我的晚年极其幸福,不管我到哪儿,别人都说“这是郑渊洁的爸爸”。虽然我没有自己的名字了,但我非常幸福。 (2011-06-13 07:13:06)

 

父亲是终身制
郑渊洁: 7月2日就是你80岁生日了,你的80岁生日和父亲节几乎是零距离。我要对你说:谢谢老爸。不懂教育的你,教育出了一个还行的儿子。不望子成龙,是最好的望子成龙。 (2011-06-13 07:15:59)
  • 郑洪升: 你也是父亲,我也祝你父亲节快乐。我还希望,明年的现在,我能祝我的孙子郑亚旗父亲节快乐。 (2011-06-13 07:16:54)
 
 
 

和郑渊洁对话的路径:WWW.PPL419.COM 

 

 1958年郑渊洁和父亲郑洪升:

 父亲是终身制

 


2010年郑渊洁和父亲郑洪升:
父亲是终身制 

1966年12月27日郑渊洁遵父嘱抄《人民日报》:

 父亲是终身制

 

 

《北京晚报》刊登《郑渊洁和父亲郑洪升对话录》:

父亲是终身制
父亲是终身制