[Reprint] How to learn the source code of large projects

The source code of Unreal Engine has about several million lines (no exact statistics, you can refer to the screenshot of the pure code folder below), which can be traced back to the 3D game independently developed by Epic in 1998 - Unreal. For a game engine that provides such a complete set of features, it can be imagined that its code is quite complex. Therefore, at the beginning of your study, you must make it clear that your goal should not be to read all his source codes from beginning to end, but to sort out the content of a specific module in an orderly manner after determining the learning goals .

Write picture description here

Here is a simplified version of the summary first, and then I will further elaborate on each item.

Preparation: It is recommended to prepare large and continuous time (fine time is easy to interrupt the sorting out of class relationships), an IDE or tool that is easier to find (VS, Notepad++, etc.), class diagram tools (staruml, Edraw, etc.)

Learning steps (simplified version):

  • 1. Decide on the modules to learn, search for official documents and relevant summary articles, and sort out the general learning content and goals
  • 2. Run the program and observe the performance
  • 3. Run the source code, breakpoint debugging, follow the execution process of the source code from the beginning, pay attention to the function stack
  • 4. Draw class diagrams and flowcharts, first record the important classes encountered, and indicate the relationship between each class
  • 5. Record the problem, record the class or content that you don't understand in the form of a problem
  • 6. Write articles and notes, and try to solve the remaining problems one by one

2-6 may require continuous repetition


Learning steps (detailed version):

  1. Find official documents and related summary articles.
    For example, if I want to study the network module, I first go through all the content related to the network in the official documents, forums, and wiki. At this time, I try to solve the problems that I don’t understand. Write down the problem, I think it is very necessary to go to the official document first, because the article here is the most authoritative, and the error rate is very low. Then, go to Google and Baidu to search for related articles and posts, and at the same time, you can join some technical qq groups (if there are some water groups, just quit decisively, and keep some high-quality communication groups), and go through these articles and materials. At present, the better technical websites that can be seen are generally the official websites (forums) corresponding to each technology, StackOverflow, Zhihu, Blog Garden, Jianshu, CSDN, some personal websites, etc. Of course, some websites have serious copy and paste problems, and you need to filter. It is recommended that if you can find the link to the original text, try to read it in the original text, because you may see more excellent articles from the original author.

  2. When running the program, we need to adjust various parameters to execute different situations, and then observe its performance to verify our conjectures and conclusions.
    For example, whether the properties of an actor in a dormant state can be synchronized normally, whether the callback function will be executed if the properties of the client are the same as those of the server, etc. Executing the program can quickly draw conclusions, and then we can analyze more quickly and accurately based on the conclusions. In order to improve efficiency, it is best to set different configurations, GM, etc. at the beginning to dynamically change the running content when the project is running, because large-scale projects are generally compiled languages, and we may need to frequently modify the code to compile and then re-run.

  3. Debugging can be said to be the most critical step. 80% of the details need you to understand when the function is executed here (function breakpoint) during debugging? What is the value of each attribute at each step? When does the property value change (conditional breakpoint)? What does a complete execution flow look like (function stack)? These problems require you to track and debug bit by bit, and finally solve them.

  4. Many people may not have the habit of drawing pictures, but I personally think this is an essential link, because it can greatly improve your understanding of the framework. For any complex project, each module will involve a large number of classes ( Excluding pure C projects), the relationship between classes is intricate, and various design patterns may be used to reduce coupling, which greatly increases the difficulty of reading the code. It is very likely that you will be completely confused after reading 3 or 4 classes What do they all do. For example, when I was looking at the UE4 attribute synchronization module, I was completely confused by the relationship between the various classes, so when I saw a class, I drew its class diagram, and when I saw related classes, I recorded their relationship , and finally got a simplified class diagram like the one below. After combing, I can summarize the relationship between them in a few words. Of course, in addition to class diagrams, there are also flowcharts, sequence diagrams, and even "module relationship diagrams" that you invented for convenience. At this time, the types and specifications of diagrams are not that important, as long as they can help you understand.Write picture description here


  5. There are two types of questions recorded: questions given to you by others and questions given to you by yourself To a myriad of questions. At this time, don't worry, solve them in order of importance, and search the Internet for simple problems. If it still doesn't work, just write down the problem and continue to study. When you go deep to a certain level, your problem may be solved by itself.
    Questions you give yourself: When you solve the questions others give you, you should have understood more than half of it. However, it is impossible for any article written by anyone to cover everything. You need to dig out more and deeper questions yourself, and then answer them yourself, so that you can understand more than others and better reflect your own value. .

  6. There is a difference between writing an article and writing notes, but they are both meaningful.
    I don’t think writing an article is necessary compared to the above steps. This is suitable for those who pursue perfection and squeeze out time. What you write is for others to read, so your purpose is to make it clear to others. When I write an article, I will consider whether what I said is really correct, whether I am sure, and whether I have considered it comprehensively. Under this kind of self-torture, I will try my best to improve my knowledge system and modify the content in the article that seems inaccurate. If you can make it clear to the readers in the end, then you really will.
    Writing notes is much easier than writing articles. I just record what I have summarized and what I have learned, and I don’t need to think too much about it. Although it is not as perfect as writing articles, we may also think a lot more during this process, and quickly recall our learning experience in the subsequent learning process.

Remarks: It is quite long and difficult to execute this process for the first time. If you don’t have enough knowledge at the beginning like me, you will need a lot of learning and searching for almost every step. But as long as you stick to it, you will find that you have gained a lot in the end, and reading the source code of other modules will become much easier.
The above is just a way for me to learn the source code, it may not be suitable for everyone, and there are still many places that can be further improved.

Finally, I would like to emphasize that if you just want to use a tool well, you don't need to understand everything thoroughly, because your time is limited. If you really study the details of the source code with a learning attitude, then please be prepared to spend a lot of time and stick to it.

Guess you like

Origin blog.csdn.net/hechao3225/article/details/113752436