How to quickly and effectively read other people’s code
如何快速,有效的阅读其他人的代码
by Alex Coleman | Learning, Web Development
Just the other day, a fellow STCer (Self-Taught Coder) asked me the following question:
“How do you go about understanding someone else’s code? I am starting to feel more comfortable with my own code, but whenever I try to look at something someone else wrote I feel totally lost. I know some of this is unavoidable, especially if the code is poorly (or not at all) documented, but right now I have no strategy at all. Any tips would be greatly appreciated!”
有一天,一个自学编程的人问了我以下的问题:
“
你是如何看待其他人的代码的?我更加喜欢看我自己的代码,当我尝试阅读其他人代码的时候,我彻底迷失了。我知道这是不可避免的,特别是在文档不足的情况下。我现在针对上面的情况没有一点办法,你有什么好的建议吗?
”
I love this question for a few reasons:
- The method I’ll recommend for reading and understanding someone else’s code will also help you: 1) better understand your own code; and 2) help you increase the speed and ease with which you understand all new pieces of code you approach.
- It sheds light on one of the most important aspects of learning a new skill, like programming: exposure to high quantity, high quality examples of expertise.
我推荐下面方法给你是基于下面两个原因:
- 我下面介绍的,帮助你阅读和理解他人代码的技巧,也能帮助你理解你自己编写的代码,同时也能帮助你更加容易和快速的理解你接触到的代码。
- 它表明了当你接触一个新知识时(比如说编程)很重要的一个角度:接触大量高质量的专业知识实例。
There are a lot of wins here. Let’s start at the beginning.
What’s the best way to read and understand someone else’s code?
阅读和理解其他人代码的最好办法
The best way I’ve ever discovered to read and understand someone else’s code is to:
下面介绍我发现的最好阅读和理解他人代码的方式:
- Find one thing you know the code does, and trace those actions backward, starting at the end.
1 确定当前代码的作用,从最后开始倒推其实现步骤
Say, for example, you know that the code you’re viewing ultimately creates a file with a list of movie titles. Figure out where in the code — the specific, few lines — it generates that file.
Then, move one step backward and figure out how it places the info in the file.
Then, move another step backward and figure out where the info came from.
And so on…
比如,你知道这段代码是最终生成一个电影标题的文件。首先,找出最终生成文件的那几行。
接着,往后倒退,找出确定其生成内容的代码。
接着,再往后退,找出上面生成的内容来自哪里。
以此类推。
Let’s call those connected pieces of code a “chain of actions.”
让我们将这些相关联的各段代码叫做“行动链”
Inevitably, using this approach will lead you through a bunch of different areas of the code. And that will probably give you a good deal of insight into things such as:
- how the body of code is organized (where variables are defined, where different types of functions are located, etc.)
- the person’s style of coding
- how the person who wrote the code thinks and problem solves (this is harder to describe, but it comes intuitively the more examples you see)
当然不可避免的,使用这种方法会让你阅读一堆各个地方的代码。你可能还会有下面的问题:
- 整个代码是如何组织的(比如,变量在哪里定义,函数,类在哪里被引用等)
- 当前人员的编码风格
- 编写代码的人是如何思考解决该问题的(这很难说明,但是当你看了更多例子后,你会渐渐明白)
And by doing that, you’ll gradually begin to understand more and more of that full body of code. So where you started with:
[ a big file of code that doesn’t really mean much at all to you ]
you’ll now be looking at:
[ still a big file of code, but where you now understand a few specific sections ]
It’s almost as if you were originally standing in a room that was pitch-black, and, one at a time, different lights throughout the room were turned on to gradually reveal more details of the room’s appearance.
当你开始这么做了,你会逐渐开始了解代码的整个架构。所以当你面对:
[当你面对一个很大的文件,并且你并不了解它时]
你就可以尝试上面的方法了。
[文件并不会减少,但是你会逐渐明白一些关键的点]
这就好像你在一个黑暗的房间里,逐渐打开一盏又一盏灯,你会逐渐看清房间的全貌。
当使用“行动链”查看一段之前从未看过的代码时
- Rinse and repeat.
2 多次反复
Repeat that process multiple times, and you’ll rapidly increase your understanding of more and more pieces of the overall codebase.
Just as parts of the pitch-black room are gradually illuminated, parts of the code gradually “light up” for you, as you understand how they function.
The reason that works well is that, in all cases, a body of code is designed to tackle one (or more) complex problems. So you’ll always have those “chains of actions” throughout.
And the more you can gain an understanding of how different parts of the code are connected, the more you’ll develop an understanding of the entire codebase, as a whole.
And, over time, the more (good) code you see, the easier it becomes to read and understand *all* code, and the faster you can do so.
…which leads directly into the second reason I love this question: it highlights the importance of exposure to high quantity, high quality examples of expertise.
In programming, “high quality examples of expertise” = good code that other programmers wrote.
重复上面的提到的方法多次,你会快速掌握该代码越来越多的部分。
就像黑暗的房间最终会变亮,你也会最终掌握你所阅读的代码,并理解其是如何工作的。
上面的方法能奏效的原因是,在所有的情况下,一段代码被设计出来是为了应对一个(或者多个)复杂的问题的。所以你总是可以使用“行动链”来学习他们。
当你越来越理解各个部分代码是如何组织在一起的,你越能将这些代码当作一个整体运行。
随着时间推移,随着你阅读越来越多优秀的代码,你会越来越熟练阅读和理解这些代码。
上面的内容直接引出我的第二个回答:它表明了接触大量高质量的专业知识实例的重要性。
在编程中,“高专业质量的专业知识实例=其他人写的好的源码”。
The importance of exposing yourself to high quantity, high quality examples of expertise
接触高质量的专业知识实例的重要性
In her incredibly poignant new book, Badass: Making Users Awesome, Kathy Sierra states that exposure to high quantity, high quality examples of expertise is one of the two main factors that dictate how quickly and effectively people learn new skills. (The other is deliberate practice.)
“The more you watch (or listen) to expert examples, the better you can become. The less exposure you have to experts or results of expert work, the less likely you are to develop expert skills.”
Let’s take a look at the first example that clearly pops into everyone’s mind: chicken sexing. Kidding. But it is, in fact, a great demonstration of this concept.
在 Kathy Sierra 的新书《Badass:让用户惊叹》中指出,接触高质量的专业知识实例是快速,高效掌握新技能的重要原因之一。(另一个是刻意的练习。)
你看(或听)专家提出的例子,你越有可能成为一个专家。你接触,或者了解专家的工作内容越少,你掌握这项技能的可能性就越低。
让我们举一个辨识小鸡性别的例子。我没有在开玩笑*(翻译人员:作者估计到这里就开始写high了,所以就会有很多语气语句,所以我只是翻译,不是我也翻high了。。。。。。)*。事实上,辨别小鸡性别是解释上面结论的一个很好的例子。
Learning from chicken sexing (yeah, you read that right)
学习如何辨别小鸡性别(是的,接下来是真实的教学内容)
What is chicken sexing? And what does it have to do with exposure to high quantity/quality examples of expertise? Kathy explains:
“Determining the gender of a newborn chick is notoriously tough, but for large commercial chicken farms, the sooner the females are separated from the males, the sooner they can be on the feeding-for-egg-production path. In the early 1900s, the Japanese developed a particular method for chick sexing and a few experts (reliable, accurate chick-sexers) emerged.
Great, we’ll have those experts teach others, right? Just one problem: when questioned, the chick-sexing experts didn’t know exactly how they did it. ‘I just knew.’”
什么是小鸡的性别呢?它和我们上面提到的结论之间有什么关系呢?kathy做了下面的解释:
辨别刚出生的小鸡的性别是困难的,但是对于大型商业性质的养鸡场,母鸡能越早被辨别出来,就越早能执行喂养-产蛋-销售。在1900年的时候,日本人发明了一种特殊的方法来辨识小鸡的性别,从而出现了一些专家(可靠,准确的小鸡性别辨认者)。
这样很好,这些专家可以将他们的技巧交给其他人了,对吗?但是只有一个问题:当这些专家被问及如何准确辨认小鸡性别时,他们也说不上来原因。
So how do they even go about the training? Well, assume you’re one of the new chick-sexer recruits. You’re are placed in front of a bin full of baby chicks. Problem is: they all look exactly the same to you. But you’re told to just pick one up and make a guess as to its sex. As far as you’re concerned, your guesses are completely random. But Sierra continues:
“After each wild, random, totally made-up guess, the master chick-sexer gives you feedback. Yes, no, no, yes. You still have no idea how the expert “knows,” but you just keep doing this, over and over.
And then, eventually, something happens. You begin scoring better than random. You get better. Over time, much better. But you don’t know why. For all you know, you’re still just guessing, but now it’s as if some “mysterious” force is guiding your hand toward the correct bin.
这种情况下,怎么能让他们执行培训过程呢?现在,假设你是一个小鸡性别判断的新手,你站在一大堆刚出生的小鸡前,而且他们在你看来一模一样。你被告知堆一只鸡的性别进行猜测。随着你越来越紧张,你几乎就是在乱猜。Sierra解释到:
在经历每一次胡乱猜测后,上面提到的小鸡性别判断专家会给予你反馈,诸如,对,错,对这样的。你依然不知道这些专家是如何辨别出这些小鸡的性别的,但是你接着猜测下一只鸡的性别,一次又一次。
最后,神奇的事情发生了,你猜中的概率上升了。而且随着时间,你猜中的概率逐渐上升。但是你也说不出原因,你只是在猜测,只是有一股神奇的力量在指引着你说出正确的答案。
How does all this “magic” work? Sierra brings it on home:
“After enough exposure with feedback, your brain [begins] detecting patterns and underlying structures, without your conscious awareness. With more exposure, your brain [fine-tunes] its perception and eventually [figures] out what really [matters]. Your brain [is] making finer distinctions and sorting signal from noise even if you [can’t] explain how.
Perceptual knowledge includes what we think of as expert intuition. The ability to instantly know which chess move to make. Or that this painting is a forgery. Or that this house fire will explode. Or that there’s something wrong with that code, even though you can’t always articulate how you know.”
上面提到的神奇的力量是如何工作的呢?Sierra解释道:
在经历足够多的带有反馈的实例之后,你的大脑在无意中中开始构建模型和基础结构。随着你经历更多实例,你的大脑开始微调并最终做出结论。你的大脑拥有更好的感觉,并从众多信息中选择能最终做出决定的信息,但是你还是不了解这一切是怎么发生的。
我们所讨论的专家的直觉是感性的知识。就像很快决定下一步棋下哪里,或者这幅画是否是赝品,又或者这场火灾是否会引发爆炸,又或者这段代码中是否有问题。以上这些你都能感受到,但是你说不出原因。
专家是如何使用潜意识的感性知识的
How does that play out in programming?
以上建议如何在编程中起作用?
Most importantly, know that the longer you’re programming — and thus the more code samples you see, of all different kinds — the easier it gets to understand other people’s code. And the faster you’re able to do it.
It’s a wonderfully self-perpetuating cycle: you read more code; you gain the ability to understand it quicker and more effectively; so you are able to consume even more code; and so on.
And it doesn’t stop there: you’ll also see huge positive gains in your own coding. How so?
- You’ll be able to more quickly understand code samples and examples you inevitably reference during your own programming (e.g. something from an online course; or a snippet from a StackOverflow post).
- You’ll be able to understand past code you’ve written at a glance. (And, inevitably, down the road, you’ll be working with a lot of different pieces of code all together, so this ability pays off big time.)
Ultimately, that translates to:
- Less pauses
- More progress
which = more fun and more enjoyable. Win!
And you better believe that…
最重要的是,随着你编写程序的时间越久,必然你看到越来越多的各种各样的代码,你会更快,更容易的理解其他人的代码。
这是一个良好的自我循环:你阅读越多的代码,你就越能训练自己更快,更有效的阅读代码,从而你能阅读更多代码,以此循环。
不仅仅在于阅读别人的代码上,你在阅读自己代码上,也会有优势。
- 你会更加容易其他人的代码示例。(比如网课中的代码,或者你从StackOver上复制黏贴来的代码)
- 当你快速阅读自己以前写的代码时,能更快的理解他们。(而且,不可避免地,在将来,您将一起处理许多不同的代码,因此,这种能力可以节省大量时间)
最终,它会产生:
- 更少的停顿
- 更多的进展
等于带给你更好的编程体验和享受,双赢。
你最好这么相信。。。。。。(翻译人员:我相信,因为有心理学上的支持。。。。。。)
下面的内容不是知识介绍的,而是介绍作者的课程的,有兴趣的可以直接去看原文,我就不翻译了。