Reading code is one of the most important skills for programmers. We read code from colleagues or third-party libraries every day. How can we read code efficiently? Share my source code reading method.

My approach to reading source code is divided into five steps:

The first step is to understand the functionality of the code through documentation and test cases

Understand what the code does before you read the source code, either through documentation or test cases, and understand what the code does and what the inputs and outputs are.

Understanding the function is the basis of reading the source code, there will be a sense of direction.

The second step is to think about how the functionality will be implemented

Once you know what the source code does, think about what you would do if you implemented it yourself. Just get a general idea.

If confused, you can take a look at the source code used by the dependency library, what function these dependency libraries have, and then think about how to implement.

It doesn’t matter if you’re still confused, the important thing is to think about the implementation first.

The third step, rough read the source code to clarify the implementation of ideas

You have a general idea of how to implement it, then read the source code to see how it is implemented. Similar to your ideas can be quickly swept away, and the impression is also very deep, and your ideas are not the same place, through reading the code to figure out its implementation ideas.

Don’t worry about the details of this step, just know what a piece of code does, the key is to diff with your ideas, to understand the overall implementation of it.

Step 4, use the Debugger to sort out the implementation details

Rough reading of the source code to clarify the implementation of ideas, for some parts of the specific implementation may not be very clear, at this time you can debug through the debugger breakpoint.

Construct a test case that triggers this functionality, make a breakpoint at the code of interest, and run the code through the Debugger.

At this point, you already know what this part of the code does, and it is easy to understand the function of each statement in single step debugging, so that after understanding each statement, you can easily clarify the implementation details of this part of the code.

Part by part through the debugger to clarify the details of the implementation, you will have a good grasp of the overall code ideas and details of the implementation.

The fifth step, output article to tell about the source code implementation ideas

When you feel you have a good grasp of the implementation of the source code, you can output an article to describe the overall idea of the source code.

Because there may be some parts you did not notice, and in the output process, will be more comprehensive thinking, at this time if found some points not read, you can read the source code through the first few steps, until you can clearly understand the implementation of the source code. That’s how you really read the code.

This is the way I find it more efficient to read the source code.

conclusion

My approach to reading the source code is divided into five steps:

  1. Understand the functionality of the code through documentation and test cases
  2. Think for yourself about how the functionality will be implemented
  3. Rough read the source code to clarify the implementation of ideas
  4. Use the Debugger to sort out implementation details
  5. Output article to tell about the source code implementation ideas

These five steps are indispensable:

  • Missing the first step, do not understand the function began to read the source code, that read the code will have no sense of direction
  • Without the second step, reading the source code without thinking about it, the efficiency of understanding the implementation of the code will be reduced
  • Without the third step, the debugger starts without clarifying the overall idea. It is easy to get bogged down in details and lose track of the overall idea
  • Without the fourth step, the debugger can only sort out the overall idea, so it can’t really sort out the details
  • Without the fifth step, it is impossible to verify whether the overall idea and implementation details are really clarified without the output article

Of course, this is my personal way of reading the source code, for reference only.