This article mainly summarizes my source code reading experience from graduation to now, hoping to help you explore a suitable for their own source code.
1 Why read the source code
There’s a lot of sharing online about the benefits of reading source code. Here’s what I think:
Get involved in projects quickly. As a front-line developer, it is inevitable to have a deep understanding of the project when entering a new company and a new project. Besides, by learning to read and debug the code efficiently, I can quickly get familiar with the new project and participate in the development of the new project in a shorter time, thus saving more time to recharge my batteries. As a student engaged in technology, you must be lifelong learning, and your daily learning time is accumulated in little by little.
Hone your programming skills. By reading the source code of open source framework, you can effectively improve your programming skills, learn excellent design patterns, and form your own programming style. For some classic framework design, such as Spring, Mybatis, if you have not read the source code, it is difficult to understand the essence of its design, the interviewer casually in-depth a few questions immediately revealed.
Solve difficult and miscellaneous diseases. Master the ideas and skills of reading and debugging source code efficiently, you can locate and solve problems more quickly, read other open source frameworks across languages, achieve the realm of skills and language independent, thanks to JetBrains to provide us with the IntelliJ series of tools, greatly reduce the cost of cross-language migration.
Reading a good open source framework is like reading a page-turner detective story, as you unravel puzzle after puzzle, link together, and get to the bottom of it. In this article, I will share my experience of reading source code from graduation to now. In the next article, I will share some key ideas and debugging tips for reading source code.
2. My reading experience of source code
Looking back on my graduation from 2014 to now, my reading experience of source code can be roughly divided into three periods: exploration period, growth period and maturity period.
2.1 Exploration Period: 2014 ~ 2016
The exploratory period is about two years from 2014 to 2016. The end of this period is marked by the mastery of shortcut keys and the basic principles of Tomcat.
Part of the time before the exploratory period was spent practicing IDEA shortcut keys. In the first half year after graduation, I spent a lot of time to practice shortcut keys every day. It was really rote memorization at the beginning.
The specific approach at that time was as follows:
- Write each shortcut key on a post-it note with a Deadline. The format of each line should be “Extract method parameters ALT+CMD+P 12.31”. Write five lines for each post-it note.
- Every day before going to work, I will operate each post-it note several times. After a period of time, when the five shortcut keys of the post-it note are familiar, I will tear off the post-it note and paste it on a notepad. In the early stage, I will copy a new post-it note every day, that is, five shortcut keys.
- At most, there should be seven or eight sticky notes, or dozens of shortcuts. After that, every time you tear off a sticky note, copy some new ones and start the cycle.
- With more and more notes on the notepad, we also need to review it every few days. Occasionally, we encounter some ones that are not used at ordinary times. If we can’t recall them, we will copy them again and stick them on the new sticky notes at the station.
- In the process of coding or debugging, if certain shortcut keys have been memorized before, force yourself to use the shortcut keys to complete, if memory lag, even for a second, also need to copy on a new sticky note, the next cycle.
After this period of deliberate practice, the result is that I can now memorize close to 200 key combinations, and it is muscle memory.
After laying a good foundation, I started to try to read the Tomcat source code. To be honest, the first time to do it is very hard, often get caught up in the details, there is no shortcut, this time you have to spend yourself. This process also has a surprise, is the first time to know how to implement an Http server through Socket, that feeling is very comfortable, suddenly inspired me to explore the curiosity of the source world.
In general, this paragraph of time is only on a Tomcat source book to read the source code, see which is which, also didn’t read the source of its train of thought, but through this experience, I learned that in fact not so difficult to understand the open source framework source code, as long as you take the time, even if the other people’s ideas to explore, also can understand the underlying principle.
One of the more meaningful outputs I made during this time was to help 10W + developers by recording a video of my experience with keyboard shortcuts:
If you’re in the first stage, my advice is to spend some time memorizing keyboard shortcuts.
2.2 Growth period: 16-19 years
Growth period is about 16 ~ 19 years, about three years of time, the end of this period of time is marked by Netty source code analysis video course online.
About a year or two after graduation, I started reading the source code for Spring. The first time I read it, I was fascinated by its beautiful design and code style, and I still think Spring is the most beautiful code in the open source world.
When I read the Spring source code, I also referred to a source guide book (so you must read more books). Different from the exploratory period, during this period, debugging skills have been improved to a certain extent, especially the combined debugging skills in some scenarios, and gradually began to form their own methodology. Then I started to share the source code analysis. I remember sharing the core flow of Spring for two or three hours without stopping.
Familiar with the Spring source code, the group was given a strange nickname, “Spring Fairy “, which translates to “Spring fairy” in Chinese, a nickname that survives today, and the Flash is no match for that.
In the middle of the growth period, I received a long connected related project (thanks for yong Ge’s trust), and Netty was used as the core technology. At that time, I realized that this framework was the lowest technology of almost all middleware, so I began to study the implementation of Netty at the bottom while completing the project goals.
And Tomcat, Spring is not the same, at that time Netty related principle analysis related articles almost zero, can only try to chew.
Have the basis of the previous years, oneself read the source code of the key ideas and debugging technique gradually stability, try without reference to any principle under the condition of analysis data to explore, really spent a lot of time, in this process there is suffering, there are excited, has stalled, also have be suddenly enlightened, but, after all the hard stuff is my part.
The gnathing process for others may be relatively boring, but for me it is a lot of fun: almost every day after work, every weekend will open the source code, a debugging is a few hours, constantly “CMD F2, Shift F9, F7…” , debugging skills have been polished, accumulated a lot of debugging skills.
Meanwhile, during that period of time, I spent a lot of time on weekends to write articles related to the underlying principles of Netty, through which I got to know many peers and some of them even became colleagues now.
Later, the MOOCs editor approached me through my article and wanted to collaborate on a course. After much consideration, I decided to create a video course related to Netty source code analysis to break new ground and better help others exploring this road. Video lessons are harder than writing essays because mistakes are expensive and cannot be revised repeatedly.
At that time there was no way, but to brave it, once again into the bottom of the Netty world, constantly gnaw, repeatedly gnaw, each time before recording a new chapter, will spend a lot of time to gnaw details, grasp the context, until this chapter involves all parts of the heart to begin recording.
Finally, the video course “Java Read source Code Netty In-depth Analysis” was launched in MOOC in 18 years, and it took about half a year. Every weekend, I was either recording videos or on the way to record videos.
To summarize the changes during this period: Through reading Spring and Netty source code, as well as sharing articles, blogs and video classes, I have basically formed the key ideas of reading source code and debugging methodology. When solving Spring and Netty related problems in the follow-up, I can quickly locate and solve the problems even if I have not studied related modules before. Then you enter the maturity stage.
2.3 Maturity period: 19 years ~ today
From 2019 to now, in the mature period, I began to read a lot of other open source framework source code, check all kinds of difficult and miscellaneous diseases, and gradually began to explore the realization of the underlying principles of the computer.
During this period, I studied the implementation of some open source frameworks at home during weekends, such as Dubbo, Mybatis and some core frameworks involved in my work. My approach is to start each study by posing a question to myself, such as how Dubbo’s service discovery works, and then spend an afternoon or evening digging through the source code to find the full answer.
Honed during the formative years, by this stage, keyboard shortcuts have developed muscle memory. I may not be able to say a shortcut key for an action quickly, but I can instinctively press it with my fingers. The muscle memory of the shortcut keys has helped me tremendously in reading and debugging the source code. When my hand speed has caught up with the speed of my mind, the continuity of my mind is guaranteed and every time I read the source code, I can enter the state of flow.
In the process of exploring the source code of Dubbo, Mybatis and other open source frameworks, I have a very intuitive psychological feeling based on my past experience and debugging skills: Each analysis of the source code is like a process of reasoning to solve a case, and like reading a mystery novel, in the complex world to grasp the core link, clarify ideas, and finally get to the bottom. There are no secrets in front of the source code, everything is open.
After studying some open source frameworks, I found that the underlying principles of many frameworks are pretty much the same (redis, Nginx, Netty’s application layer memory allocation mechanism and thread model design are almost the same). Frameworks are updated every few years, so there is not enough time to study all frameworks.
At this time, you need to ask yourself a question: every time you encounter a new technology, how can you quickly master it?
I started thinking about this question before the Spring Festival of 2021, and during the Spring Festival, I came to the conclusion that in the rest of my technical career, I need to explore what will not change in the next few years or even more than a decade.
So I turned my research direction to the underlying principles of computers. Part of this is the Linux kernel, which has a lot of treasure worth digging. Some of the design of many application layers is isomorphic to the kernel design, and many technologies directly depend on the implementation of the kernel, such as Docker.
Although the kernel is written in C language and assembly, and I am engaged in Java, but the source analysis ideas and debugging skills are the same, after a period of trying, at the end of 21 began to write Linux kernel analysis related articles, can be regarded as the beginning of the bar.
The kernel is far more complex than I expected, but the more complex it is, the more exciting it is to explore.
Today is the New Year’s Day in 2022, set a small goal for yourself kernel: 2025 New Year’s Day before the entry, 2027 New Year’s Day before the familiar, 2032 New Year’s Day before the master, 10 years later, I hope not to be hit in the face!
3 conclusion
Originally this article would like to focus on writing the key ideas and debugging skills to read the source code, did not expect to write finally became a source memoir, simply put the dry part of the next article, welcome everyone to pay attention to the public number.
Finally, by the way, after New Year’s day, my new book, Learn Netty from the Flash, comes out. The first half of the book is from the Gold Digger booklet: Getting started with Netty through a complete IM project; The second half of the larger length to introduce the underlying principles of Netty, will also be interspersed with some source code reading ideas, I hope to help you.
The full text, everyone happy New Year!