"Super practical" to face the boring source code, how can we persist in watching?

Source: Internet
Author: User

A software developer, working to a certain length of life (generally 3, 4 years or so), if he has not learned to read the source, then he will encounter bottlenecks. Because at this time of development, he should not only do those curd business logic, but should be based on the actual situation of the company to write the framework. And basically no one can write a framework from scratch like a genius, and many people write frameworks that start with imitation. And you have to imitate, then you have to understand the framework of the source code. So it's important to read the source code.

I have been working for a year or two when I tried to see the source of the JDK, then it felt like reading the heavenly book, it felt pain. has been to work about three years, because colleagues are looking at the source code, I also began to look at some source code. I see the first frame of the source code is Dubbo, although said not fully understand it. But by reading the Dubbo source code, I understand the SPI mechanism of the JDK and know the role of SPI in framework development. Until recently read a company's gateway framework, I slowly have a bit of reading the source of experience and routines. Just hit the strike, write down the experience, I hope you can also early on the source of reading the road.

Search online Information

Read a framework of the source code, the most basic operation is definitely to sweep over the online analysis of various articles. With this operation, you can have a general understanding of the framework. Stand on the shoulders of giants, and walk a lot less detours. But if you look at the project is within the framework of the company, then you can only find the company's internal documents, and even more, some of the documents are not. Then you can skip this step.

Sweep through the source code

When you get the framework of the source code, you can roughly put the source of each package, as well as the file under each package to read it again. Scanning does not require you to understand the meaning of each line of code, just let you know the role of each part of the source.

If an open source framework is standard enough, then his naming is very semantic. So when we sweep, we can tell by the package name and file name what the package is for. For example, util package is a tool class, then we can skip directly. VO packages are stored in the entity model and can be skipped as well. Protocol packages are storage protocol related and so on. With such a step, you will have a basic impression of the whole project, knowing what the project is about and what is relatively important.

Find the Entrance

To read the source code of any one frame, the first is to find the portal 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 that it has a sub-module called Dubbo-demo, then we must focus on it. Explore further requirements you will find that its entrance is the Provider class, the Consumer class in Dubbo-demo. We can go directly to the main method of running these two classes, and follow the execution of the code step by step.

Read through the source

After finding the entrance, the next step is to read through all the source code, that is, each file of the source code to see each line. at this stage, it is not necessary to fully understand the detailed business logic, but to form a general framework, to know how the framework is designed, what the approximate modules are, and how the modules are designed.

Read through the source code this stage is the most boring, but also the most easy to give up. On the one hand because the source is too much, on the other hand because there is no goal, so look at the heart without a bottom, so easy to give up. I did the same thing at first, but then I came up with a better way to make myself aware of the progress of reading in a digital way. So I do not feel the bottom of the heart, do not know how long it will take to read.

My way is to install a code statistics plugin for my IDE: Statistics. This plugin can count the number of source lines in the project. For example, here is my statistics Dubbo project source code, it will list each Java file source line number, as well as the total number of rows.

From the above we can see the Dubbo project, the source line number of lines is 110,000. In this way, the psychological also has a general expectation. Next, I'll copy the data and put it in an Excel table, like this:

In the Excel table, I will only store the name of each source file, its source lines, the number of source lines accounted for. Next, I will press the entrance, one by one to see the source file, each method to see once, see the method I will write a note, for example: Csy mark, and write my comments. After I've seen a file, I'll write the percentage of the file in the Excel table in the rightmost column. In the end I'll have a line below that counts the percentage of files I've read.

Every way I read it, I write a csy done to encourage myself. Every time I finish reading a file, I mark it as complete in Excel, and the bottom of the read percentage is also rising. In this way, I let the boring source read, some interesting, with some goals.

Read through the source code is the most boring, the most easily without direction and goals. With the digital record, you can know your current progress is how to see how much of the source code. Most of the time we watched for a long time, found that there is no progress of the appearance, do not want to see. At this time you can set a goal, for example: a day to read 5% of the source code can rest. When you want to be lazy, look at the read percentage below Excel, not yet 5%, continue to see it.

This way for me, still has a certain effect. But for those of you who do not know if there is any effect, you can try. If it works, just let me know.

Carding framework

In the process of reading through the source code, you will have a lot of new understanding of the framework, you will know that the framework is broadly divided into which parts, what is the role of each part, the module with what design concepts and so on.

If the last stage is to read through the source code, then this stage is to be read through the source of your harvest in the process of sorting out. In the process of finishing, you will certainly have more questions, you will continue to refine, constantly intensive reading.

Critical Thinking

Through the above several stages, you will find that you have a general understanding of the framework, and the implementation of the details of each module has a relatively deep understanding. At this point, you can think about why it is doing this and what is the benefit of doing it, can you do it better in another way?

Summarize

The above several stages is I read a few framework source after some experience, I believe it will be a good source code reading guide. If you have a better source of reading experience or method, welcome message to communicate with me.

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.