After thinking about these two questions for a long time, I also went to Zhihu to find some questions and answers on relevant topics, but there was no standard answer. Therefore, I am just recording some of my views on this, which may change as RTFSC experiences more and more, I will update the record at github.com/mzlogin/rtf… .
meaning
In my opinion, the point of reading source code is to learn good “routines”.
“Routines” can range from architectural design to desirable naming styles to design patterns, data structures and algorithms used to implement certain types of functionality. The so-called master is actually a person who can master the routine earlier and faster than most people and skillfully use it.
While you can progress and grow by working in your own world, there will always be situations where you can’t come up with a good design for a long time. There will always be times when the struggle to name a variable makes you stop pounding your fingers. These are the kinds of situations that the ancients have probably encountered and come up with elegant solutions to. When looking at good source code, keep a scenario like this in your pocket with a corresponding solution, or just keep an image in your mind, so that you always have a hand to weigh in your Arsenal when you need it.
Some methods
It shouldn’t be like this
You should not randomly pick up a source code and try to read it through. On the one hand, it’s forgettable, and on the other hand, it’s so boring that you go from starting to quitting quickly. There are many ways to learn, and reading source code is not necessarily the best one for your current situation.
Should be like this
-
Carefully select the source projects to read.
It’s best if it’s related to your programming language, what you do, and what your interests are, so you can feel the benefits of reading source code and be more motivated to continue.
-
If you want to learn something that is officially documented, read the documentation first and then the source code.
Directly from the source code, to figure out the principle is good, but the source code may be difficult to chew, first familiar with the official provided for all people to see the document, can be relatively smooth to this knowledge first have a general understanding, and then combined with the source code to go deep.
-
Ask specific questions, and then go to the source code with the questions to find the answers.
For example, in the process of using Toast, you might think of some questions: toast.maketext (…) What happened at.show()? Can Toast be called from a non-UI thread? Can I customize the Toast layout? And so on. By finding the answers you want in the source code, you have achieved your goal.
-
Start with some commonalities.
Things that are used in most applications, such as the threading model, UI organization structure, task scheduling, etc. It is much more efficient to understand one aspect rather than aimlessly.
-
Preferably compile and run.
If you’re looking at a piece of code and you can’t run it, you’re probably going to read somewhere and you’re going to have to guess what the data values and jump structures are going to look like, and you’re probably going to be wrong. But if you can compile and run it, then you can modify it as needed, log it, etc., to better observe and validate your ideas and get the right understanding.
-
Take some notes.
On the one hand, it is to keep your learning achievements for easy access at any time. After all, it is not reliable to remember only by brain. On the other hand, in the process of learning, it can also help understanding.
Raw link: Read the meaning and method of the source code
If you are interested in my articles, you can follow my wechat official account isProgrammer at any time to read more content.