How can programmers efficiently read code?

Source: Internet
Author: User
Recently I am reading the source code of Twitter's oau2's Library (simplegeopython-oauth2-GitHub), can't see it... the whole database has a self-built system and looks closely related, so it is inconvenient to start from where it is. If you look at the code from the top down, it will certainly be dizzy by various functions. From the beginning of main (), it will also be dizzy by calling multiple layers. Because I was not familiar with the Python Http request library (such as httplib2, requests), I often need to read the source code of the oau2's library recently on Twitter (simplegeo/python-oauth2-GitHub ), You can't see it... the whole database seems to have a self-built system. It seems very closely linked, so it is inconvenient to see where it starts. If you look at the code from the top down, it will certainly be dizzy by various functions. From the beginning of main (), it will also be dizzy by calling multiple layers. Because I was not familiar with the Python Http request library (for example, httplib2 and requests), I often need to check some content of other libraries...

At present, I always feel that I understand the general process, but there are always a lot of functions that I don't understand. I just want to understand it and then make some improvements to this library (because the Oauth process of some apis is different from that of other APIs, I want to improve the Oauth process ). But the degree of understanding is far from reaching this requirement... (a new library has been found in the past two days to meet my requirements, but I still want to figure out how to read the code better ...)

What should I do for senior programmers? reply: read the document first, and then read the unit test. Code Reading (Douban) (The auxiliary question is The Open Source Perspective, which is suitable for The subject's needs)
Code Reading (Douban) (Chinese translation) It was slow to read the code before. Later, the speed of writing more code to read these open-source projects was significantly faster.
Slow code reading means that you are not familiar with common functions and libraries, and you are not familiar with common programming skills in python. When you read code, you are too distracted, I still need to check the syntax frequently on the Internet. I can't focus on the overall structure of the Code. Even if every line of code is clear, I still don't know the logic of the entire program, and it consumes a lot of energy, soon I got tired and very inefficient.
After familiar with common library and programming skills, many functions are similar when reading code. You can see at a glance that many functions know at a glance, the idea will not be interrupted by the syntax or programming skills that you don't understand, and will stay on the logic of the program. It will be much easier to read the code, and it will soon be, basically, you can understand the idea of a small project if you flip it over. You don't have to grasp all the big projects, but you just need to familiarize yourself with the part you want to see.
People who read code quickly are not always faster than you. When they encounter something they have never seen before, they also need to query. They need to write some examples to familiarize themselves, and it will take some time, getting familiar with a single knowledge point is not faster than you.
Documentation and test cases are of little use in the current phase of the building lord. These things can only be icing on the cake after you read the code faster, most of the time, you can take a look at the document and test routine meetings to speed up understanding the library and compare it with the code.
If you want to improve the programming level by familiarizing yourself with this library, write more of the third-party library and programming skills used in it. In this way, you can find out why the author wrote this, A lot of code looks complicated. In fact, the idea is very simple. At first, I thought I had no idea how to do it. I wanted to buy a very famous book that taught me to read code, but I didn't buy it later, without knowing it, I found it easier to read the code.
Do you think the call stack jumps and jumps? Do you think it is too complicated to provide too many module functions? The real reason is that your mind is not enough!
At first, I read a 3000 or 4000-line code and made thousands of notes for the last two weeks. Then I didn't need to read the code. After reading the code, the function call stack was in my mind, you can draw a maximum of grass icons to show the dependencies of each module. Later, I spoke to a newbie about the module. With SI, I clicked between the functions to speed up. Finally, I found that I had gone through dozens of functions.
When the computer runs a program, too many call Stacks will exhaust the memory and crash. When people read code, too many call stacks of the brain will exhaust the memory and then dizzy. The solution is to read more code, develop the potential of your brain to support deeper call stack memory. When it breaks through the critical point, you will naturally understand how to read it. Scold the author first, and then find a small example for debugging. Looking at the system with hundreds of thousands of lines, and then looking at other systems, the system will naturally be flat.

Original article address: Aaron Liu-Welcome to my blog! • Several steps to read source code

Skill required: program debugging capability.

1. Run the program.

2. Run the core functions of the program and learn the running mode from the outside.

3. Read the official documents related to core functions.

4. Read the source code (the core module) and unit test corresponding to the core function, and find out the connection between the core modules from the inside.

5. Put forward a question about the source code (for example, how the state machine works, filters is divided into several layers) and try to solve it.

6. Browse the code and tests related to the problem, and then debug and solve the problem. (do not start debugging when the code is very unfamiliar, too long program stacks consume too much time ).

7. Continue to raise questions until the core module is familiar with the problem.

8. (optional) Communicate with colleagues and learn about this module, or write a blog to share it with netizens to deepen your memory and obtain feedback. this time, you may find vulnerabilities or errors you may understand.

9. Read the core module and repeat steps 3-7.

10. If it is still insufficient, develop plug-ins or libraries for the program.

11. Understand the source code.
For more information, see my PPT: how to read source code .pptx _ free high-speed download

Question-oriented methods for reading code are divided into two parts: external and internal.

External reading: Let's take A look at the problem. I propose A common application scenario for the program and test some main logic on the program. For example, if A is added to the background, does the foreground generate B? If the server modifies C, will the client see D or something.

Internal reading: Let's look at it with a question and propose a scenario of How to Write Program expansion. (What do you do if you want to expand this logic in the future? Is there any built-in ready-to-use API available for calling? In short, do not take the document with ideas, or take the ideas with the question.

Thank you for making more bricks or adding more.

There are several special books and articles in this regard, and some friends provide specific and systematic skills, so they will not repeat them.
It is recommended from the personal perspective, not comprehensive, mainly because of two points:
  1. Starting from figuring out the target, understanding the design idea of the target system layer by layer and how to implement the specific details at the end;
  2. If you have the ability or even empathy: instead of reading other people's code, you should design and implement it again-Do you also start by figuring out the goal and then propose a solution, the solution is further obtained and then broken down into several modules ...... layer-by-layer decomposition? Is it hard to do this?
The main tips are as follows:
  1. Top-down: from top-down to top-down reading from the features implemented by the target code, the problems (requirements) solved, layer-by-layer decomposition of the problem/Solution to the Problem
  2. Outline: Find out the framework and avoid staying in a specific detail. If you have any questions about a specific detail, record it and go back to it.
  3. Breadth First: Read the data at one level each time (for example, the relationship between the main modules and them is grasped for the first time, and the second is the main class, the third time may be at the method/function level) to ensure that the breadth is preferred rather than the depth is preferred.
  4. Clear purpose and separate responsibility: clarify the purpose/responsibility of each module, class, and method at different levels, rather than how to achieve this purpose/responsibility. Because any Algorithm serves a specific role and purpose, figuring out the purpose rather than How to implement it, and figuring out Why rather than How

Behind the ability to read code efficiently:
  1. The brain runs like the CPU. When you need to follow up to the next layer (depth), you need to write down the current breakpoint and press the relevant information into the "stack" for the moment, when you enter the next layer, return to the previous breakpoint working state. Think about the brain's stack and accurate memory capabilities are obviously not as good as the CPU, so try to avoid traversing back and forth at different levels, or even too deep. If necessary, use some computer software to help you read, such as the class diagram and sequence diagram of UML tools.
  2. What are the goals/responsibilities, and how to achieve the Goals/responsibilities: All systems exist to solve specific problems. How to solve the problem is reflected in the solution, and the code is just to implement the solution. The solution is embodied in the combination of functional modules, the combination of specific functions/methods, and the implementation of specific algorithms (internal methods) at different levels.
    Therefore, we need to break down the problem from top to bottom and solve the problem. We need to go down from top to bottom to understand the global context of this layer (breadth first ).
  3. In addition: The most common problem for developers is to pay too much attention to implementation problems such as "how to". Common scenarios include: seeing what systems, software, and functions have been made by others, the first response is "this is simple, I will" and "hey, how is this implemented? I don't seem to be "-whether you do, please restrain your technical impulse. The first question is: Why -- Why is this implementation necessary? It must serve a certain goal and responsibility. When you read the code that has been written by others, the Code must serve the goals and responsibilities of each one, big and small. So first, ignore How to and understand each of their goals/responsibilities, ignore details temporarily. After all, let's take a look at the areas you are interested in and analyze them in depth. There are time and opportunities.
    -- Please restrain the technical impulse and achieve the impulse, which is very helpful for your career. If you have already passed a similar stage, you may wish to review your experiences. The more you are not confident in your technical implementation capabilities, the more likely you will encounter similar problems.
    In fact, with the improvement of experience and ability in any industry, more and more proficient in abstract aspects of thinking, good at using the "top-down" decomposition ability, there is also a strong sense of hierarchy and logic to ensure that the expansion at any layer is systematic and comprehensive (in fact, it is not easy to omit the expansion based on the previous Layer ). For example, I used my resume to list my proficient, mastered, and familiar with various technologies and platforms, as well as various experience cases. I then changed it to a sentence --He is good at solving problems efficiently and is willing to solve complex and new problems. He is especially fond of solving problems through innovative methods.The job orientation on your resume is no longer limited to software-related. Of course, 1. This is not so much about improving your personal abilities. It is better to find the direction of your own efforts and enjoyment. At least in terms of thinking, this stage is beyond the technical background constraints related to software design and development and has the courage to face any problems in any industry. Of course, 2, do not compare industries and jobs that require too much professionalism. 3. Do practitioners in these industries have the same stage problems?
Thinking and discussing such issues can speed up personal growth.

I think there are several basic capabilities:
Computing power (the brain's stack capacity) determines the size and complexity of the problems that can be handled (with physiological ceilings), and the thinking model (methodology) the results and efficiency of handling large and complex problems (expanded physiological upper limit) and self-restraint (understanding and even controlling physiological instinct) determine the ability to eliminate interference, then there is experience accumulation (reusable module: experience at the mode level and specific implementation level ).
-- Like a good player.

My mother said that when I was a child, I like to play with a line group and organize buttons. I can sit in a place and wait for an afternoon until I undo a line group.
As I grew up, I was no longer satisfied with unlocking various online groups, or even unlocking more and more complex online groups. Instead, I tried and pursued another method: observing and reasoning from various angles, after figuring out the clues, find a line head and shake it up. Wow, the whole line is elegantly unlocked!

A real leader can observe in the crowd, and do not immediately respond to external stimuli (self-restraint), be the one who understands the goal, team, and himself clearly ), find the solution and select the most appropriate way to influence and drive people around you. It is not necessarily a leader under the spotlight, but a real leader. Without power, the entire organization can be driven to effectively solve the problem.
A friend who has read "game of Android" and recommended his sister-in-law "" shadow of Android ", the protagonist xiaopai is an excellent example (to avoid the spoiler hitting here ).

And then... Don't think about how to express it. Stop it first. If you have any friends, I will share my further experiences.

Finally, I had to wait here. 1. Establish layers and structures
Reading the code is not a novel. Just read it down linearly. The code is used to understand the code and build a ing between the layers and structures behind the source code in your mind. To this end, at the beginning of the analysis project, it is necessary to clarify the sub-projects, packages (namespaces), Class layers and structures of the Code, which are mainly composed of packages, and what each package is used. What are the main classes and their respective general responsibilities. What are the main methods in the main classes.

2. Grasp the trunk
In any project, there is a considerable amount of code used for trivial and transactional tasks. To efficiently understand and grasp the code, we need to grasp the backbone. For some main methods or procedures in the code, you can sort out their main steps, and ignore the secondary things, and pay attention to them when necessary.

3. document creation
As said by the previous respondents, the human brain is not good at remembering the inbound, outbound, and other things between methods. While analyzing these things, we use an effective method, recording the analysis results not only saves the work results, but more importantly, helps us analyze the results more easily and know where we are now when exploring the details, when you roll back, you can roll back again to avoid losing your way. Class diagrams and sequence diagrams are both useful documents and can be customized with more flexible charts. A good memory is not as good as a bad pen, so it is easy to read the code.

4. Make Rational Use of various available means
Static analysis is of course the basic method for analyzing Code. However, in addition to static analysis, if your code can also be run, run it and observe its call stack, LOG, or use debugging tools for tracking. For the main methods and procedures, it is necessary to use dynamic tracking to figure out the process. This will also be more direct than static analysis, telling you more quickly what paths it has taken and what methods it has implemented.

For my personal habits, I first read the documentation to learn about the functions and then make a demo. After understanding the basic functions, I will start from this familiar point.


I have also met a cool guy who has a hard Code. For him, this method is simple and rude, and the speed is fast and the effect is good.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.