How to read the source code? Read the source code.
Author: Liu Xin Public No.: Code agricultural turning over link: http://mp.weixin.qq.com/s/PDhEKM2XG_qzOmBjWb-M7Q
"Novice programmers who have no experience in poor technology can read the project source code? "
"Has anyone read the source code of mybatis? Just look at the initialization process and you will see that you are dizzy. Let's take the initiative! "
"How should I read the source code? I tried to read some source code, such as sqlparser in druid of alibaba, spring-mvc, but I found it hard. debug is the best way to read it, I often encounter errors when debugging ...... It seems that I am not paying much attention to the details. "
......
It is estimated that many people have such doubts.
I can understand the pain of my friends very much, because I have come through with such pain.
The benefits of reading excellent source code must be known to everyone. Learning Other People's excellent designs, reasonable abstractions, and concise code... in short, there are many benefits.
However, if you really put the huge code in front of you, it is like a huge maze. You need to turn the Middle East to the West to find a path and clarify the entire structure of the maze, it is hard to understand the core idea.
When you read code written by an object-oriented language such as Java, you will find that interfaces and specific implementations often do not match, it is not clear how a function can be found in which implementation class. Unlike the C language, the function calls the function, which is relatively better.
If it is a dynamic language such as Ruby, Python, the type of a variable is not even easy to know, the difficulty of reading is greatly increased.
Another important reason is that the source code we see has basically gone through several years of development and has been constantly improved by many people. There are so many branches and the devil is in the details. It is easy to get stuck during reading. After reading dozens of layer-7 function calls, it will completely crash and give up: render manager, you blow the source code to the sky, and I will never read it again.
After a lot of painful struggles, I have some successful experiences. Today, I use the analogy of three levels of study to share with you:
Last night, the west wind withered green trees and tall buildings on its own.
To understand and understand the source code, you must first gain a High View of the path, clarify the goal and direction, and understand the overview of the source code.
So some preparations must be done.
1. Before reading the source code, you must have a certain amount of technical reserves.
For example, the design pattern is almost a standard in many Java source codes, especially the following: Template Method, Singleton, observer, factory method, proxy, policy, and modifier.
For example, to read the Spring source code, you must first understand how IoC is going, the implementation method of AOP, CGLib, and Java Dynamic proxy, and write related code by yourself, master these knowledge points.
2. You must use this framework/class library. It is best to be proficient in all kinds of usage.
As I mentioned above, the devil is in the details. If you do not know some usage, you may understand what the code means, but you may not know why it is written.
3. Go to the book, find the information, and learn about the overall design of the software.
Which modules are available? How are modules associated? How is it associated?
You may not be able to understand it all at once, but you need to build a whole concept, just like a map, to prevent your confusion.
When reading the source code, you can check where you are from time to time.
4. Build a system and run the source code!
Believe me, Debug is a very important means. It is impossible for you to figure out the system without running it!
When the clothes are getting wider, I don't regret it.
5. Based on your understanding of the system, design several major test cases and define input and output.
Run the system, debug it slowly, and step by step. This is a hard task and there is no way to bypass it.
Debugging once is definitely not feasible, and debugging is required many times.
For the first time, discard the details as much as possible and grasp the main process. For example, some seemingly unimportant methods won't be taken into consideration.
The second and third times... let's look at the details.
A very important job is to take notes (and write again !), Draw a system class diagram (do not rely on IDE to generate it for you) and record major function calls for later viewing.
Document work is extremely important because the code is too complex and the human brain capacity is limited. The document helps you remember the key points. You can think back to them and read them quickly.
Otherwise, you may have forgotten about what you saw today.
Let's take a look at some of my notes. The format is not important and you can easily understand it.
6. I have understood the main test cases, enriched the test cases, and considered some branch procedures.
Continue Debug ......
In short, static viewing of code + dynamic debugging (from the business perspective) will slowly unveil this dark forest.
This step will take a lot of time, but after you finish it, your understanding of the system is definitely a qualitative leap.
The crowd looked for him thousands of Baidu, suddenly looked back, the man was in the dark.
Without the support of Baidu, there will be no instant epiphany and understanding. I sincerely wish all the friends who have read the source code can reach this realm.
The last and most critical point is to stick to it.
I am not a smart person, but I am stupid: I can't keep repeating everything, I can't understand it once, I can't understand it again, I can't understand it twice, third time ......
Someone may ask: how can you stick to this problem?
The answer is curiosity: How did this stuff work ?!
(End)