A software developer who has worked for a certain period of time (usually around 3 or 4 years) will encounter bottlenecks if they have not yet learned to read source code. Because at this point in development, he should not only be able to do CURD business logic, but should also be able to write frameworks based on the actual situation of the company. And basically no one can write a framework from scratch like a genius. Many people start writing frameworks by imitating. And if you want to imitate, you first need to understand the framework source code. So reading the source code is so important.
When I worked for one or two years, I tried to read the source code of JDK. At that time, it felt like reading a heavenly book, and even reading a little bit felt painful. Until about three years of work, because my colleagues were all looking at source code, I also started looking at some source code. The source code of the first framework I saw was Dubbo, although I didn't fully understand it. But by reading the Dubbo source code, I understood the SPI mechanism of JDK and knew the role of SPI in framework development. It wasn't until recently when I read a gateway framework from the company that I gradually gained some experience and tricks in reading source code. Seize the opportunity and write down these experiences. I hope you can also embark on the path of source code reading soon.
Search for online information
The most basic operation to read the source code of a framework is definitely to scan various analysis articles online. Through this operation, you can have a general understanding of this framework. Standing on the shoulders of giants, taking fewer detours. But if the project you are looking at is an internal framework of the company, then you can only look for internal documents of the company, and even worse, some do not even have documents. So you can skip this step.
Scan the source code once
When you receive the source code of the framework, you can roughly scan each package of the source code and the files under each package. Scanning does not require you to understand the meaning of every line of code, only to let you know the purpose of each part of the source code.
If an open-source framework is standardized enough, then its naming is very semantic. So when we scan and read, we can determine the purpose of this package by its package name and file name. For example, if the utility package is a utility class, we can skip it directly. The VO package stores physical models and can also be skipped. The protocol package stores protocol related information, etc. Through this step, you will have a basic impression of the entire project, knowing what the project is about and which ones are relatively important.
Find the entrance
To read the source code of any framework, the first step is to find the entry point of the framework. By scanning the source code above, you should be able to find some signs of entry. For example, for Dubbo, you will find a submodule called dubbo demo, so we will definitely focus on it. Further exploring the requirements, you will find that its entry points are the Provider class and Consumer class in dubbo demo. We can directly run the main method of these two classes and track the execution of the code step by step.
Read through the source code thoroughly
After finding the entrance, the next step is to read through all the source code, that is, to read every file and every line of the source code.At this stage, it is not necessary to fully understand the detailed business logic, but to form a general framework, know how the framework is designed, what are the general modules, and how these modules are designed.
The stage of reading through the source code is the most tedious and easy to give up. On the one hand, because there are too many source codes, and on the other hand, because there is no clear goal, it is easy to give up and feel uncertain. I was the same at first, but later I came up with a better way to keep myself informed of my reading progress through digitization. This way, I won't feel uncertain and don't know how long it will take to finish watching it.
My method is to install a code statistics plugin for my IDE: Statistics. This plugin can count the number of source code lines in a project. For example, the following is a screenshot of my statistics on the source code of the Dubbo project, which lists the number of lines in each Java file's source code and the total number of lines.

From the screenshot above, we can see that in the Dubbo project, there are 110000 lines of source code. In this way, I also have a rough expectation in my mind. Next, I will copy these data and put them in an Excel spreadsheet, like the following:

In an Excel spreadsheet, I only store the name of each source code file, the number of source code lines, and the proportion of source code lines. Next, I will follow the entrance and look at the source files one by one. I will review each method and write a comment for the reviewed methods, such as csy mark, along with my own comments. After I have reviewed a file, I will write the percentage of the corresponding file in the rightmost column of the Excel spreadsheet. Finally, I will have a line below to count the percentage of files I have read.

After reading each method, I write a csy done to encourage myself. Every time I finish reading a file, I mark it as completed in Excel, and the read percentage at the bottom keeps increasing.In this way, I made the boring source code reading a bit interesting and gained some goals.
Reading through the source code is the most tedious and easily directionless. With digital records, you can know your current progress and how much source code you have read. Many times, after watching for a long time, we find that there is no progress and we don't want to watch anymore. At this point, you can set a goal for yourself, such as taking a break after reading 5% of the source code in a day. When you want to slack off, take a look at the read percentage below Excel. It's not even 5% yet, keep reading.
This method still has a certain effect on me. But for those who don't know if it works, you can give it a try. If it's useful, just leave a comment and let me know.
Sort out the framework
During the process of reading through the source code, you will have a lot of new understanding of the framework, knowing roughly which parts the framework is divided into, what the role of each part is, what design concepts the module uses, and so on.
If the previous stage was to read through the source code, then this stage is to organize the gains you have gained from reading through the source code. In the process of organizing, you will definitely have more questions, and you will constantly refine and read carefully.
critical thinking
Through the above stages, you will find that you have a comprehensive understanding of this framework and a deep understanding of the implementation details of each module. At this point, you can think about why it is doing this, what are the benefits of doing this, and can it be done better in another way?
summary
The above stages are some of my experiences after reading several framework source codes, and I believe it will be a good source code reading guide. If you have better experience or methods for reading source code, please feel free to leave a message and communicate with me.


