Hi everyone, I'm programmer Fishskin. Last Tuesday night I went live to take you through a nice open source chat room project on GitHubMallChat
, people said they learned a lot, so I cut out a special installment of the project guide video to share with you:/video/BV1Q142147yk
In the process of explaining the project, I found that many students are not clear about how to quickly learn the project, and how to quickly read the source code. Today's article, I'll take you to understand these skills, and then later self-study project will be much easier~
How can I learn the program quickly?
1、Preliminary understanding of the project
Open an open source project on GitHub, the first thing to do is to look at the project's introductory documents, you can take a quick look to see if there is to help you learn the content, such as technology selection, functionality, how to quickly start the project, architectural design, considerations and so on.
Take the MallChat chat room as an example:/zongzibinbin/MallChat The project introduction document contains an introduction to the core features, so you can see if there are any features you are interested in:
If it's an internal project, or a mature open source project, there will be Wiki documentation, which tends to introduce the project in more detail, but you don't need to read the whole thing for the time being, just pay attention to the "Quick Start Project" or "Introduction to the Project Structure" sections. But you don't need to read the whole thing for now.
If I'm determined to learn a new project, in addition to the official GitHub documentation, I'll also collect some relevant information online and organize it into my own documentation, so that I can quickly find it when I'm learning later.
2. Understanding the project structure
After reading the project introduction document, it is recommended that you first understand the structure of the project as a whole from God's perspective, such as what modules the project is divided into, what files are roughly in each module, how the file naming rules are, and so on, but you don't need to further understand the internal organization and implementation of the modules (or directories). Especially for complex microservice projects, this can quickly help you locate the focus of learning.
In most cases, you should be downloading the project code locally to learn it. But if you're just looking for a quick overview of the project and don't intend to learn it in depth, there are actually more efficient ways to do it.
For example, from the home page of a GitHub repository, press。
The period key gives you quick access to the web version of the code editor, which is almost identical to the experience of browsing the project locally:
For developers with some experience, they generally look for key files first, such as front-end projects looking for the(), Java project to find
(Maven) or
(Gradle), the Python project finder
maybe
etc. These files usually contain project dependencies and configuration information. If the project is relatively standardized, you can usually quickly understand the project structure through the dependency and configuration information.
For example, if you see an aop dependency, you will know that the project uses facets; if you see a freemarker dependency, you will know that there will be an FTL template file in the project's resource directory; if you see Redisson, you will know that there will be a configuration class to initialize the Redisson client, and these are the importance of gaining experience.
3. Running the project
If you want to formally learn the project, do not rush to read the source code first, but first run the project successfully locally to facilitate the subsequent debugging while learning.
The steps to run the project are simple, use thegit clone
command to copy the project to your computer, and then follow the instructions in the README file or other documentation to install the necessary dependencies for the project, modify the local runtime configuration, and finally run the project.
Although the steps are simple, the reality is often that people pull the project locally and then a variety of error messages, so run the project to pay extra attention to the following points:
-
Ensure that your system environment is consistent with the requirements of the project, such as noting the front-end and back-end JDK version requirements.
-
Make sure that the versions that the project depends on are consistent with the project requirements, such as the MySQL and Redis versions that the backend depends on. For such mainstream technologies, generally speaking, as long as you don't use a version that is too new, it will not affect the operation of the project.
-
Modify the local runtime configuration to your own, such as database accounts, passwords, etc., and be extra careful not to read the wrong configuration file, otherwise you may change half a day and still wonder "how come the configuration didn't take effect"?
Encountering errors is very normal, because not all open source projects have done "easy to use and easy to get started", encountered errors, we just need to locate the key error message, and then follow the trail to find the corresponding file to modify. If you can't get it right, there are search engines and AIs.
Here is a common technique to run a project quickly. Sometimes a project uses a lot of dependencies (e.g. MinIO, RocketMQ, etc. in MallChat), but we don't have these dependencies installed locally, so the project can't start. As you can see in the image below, because I don't have RocketMQ installed, I get an error connecting to RocketMQ at startup:
The traditional way to get a project off the ground is to install all the dependencies yourself, but if the project only uses a dependency in one obscure place, it's not really cost-effective to take the time to install it. In this case, we can use a simpler method to disable the loading of beans that use these dependencies.
For example, in the startup class addexclude
If you want to disable the automatic loading of certain dependencies, you can do so quickly with the following example code:
However, after disabling the initialization of a RocketMQ bean, if any code uses the bean, it will report an error due to a missing dependent bean. In this case, you can use the@Lazy
annotation on the Bean for lazy loading, used only when it will be loaded, start the project will not report errors ~ sample code is as follows:
public class MQProducer {
4, understanding of business processes and library table design
Before reading the code to learn, it is best to get an overall understanding of the project's business process and library design, which will help clarify the order and learning direction when reading the source code subsequently.
The core business process can be understood by reading the documentation, or by experiencing the system yourself. For example, for a chat room system, the core business process is: user login => user on-line => add friends => create room => join room => send message => message review => other users receive message => reply message.
Understanding the library table design of a project is also tricky. Rather than looking at the SQL file directly, I will usually initialize the database locally first and then view it through visual diagrams.
As shown in the figure, the relationship between tables is clear at a glance! For example, rooms are subdivided into single and group chats, with multiple sessions and messages in a room:
5. Reading the code
When you first start reading the code, don't rush through the code line by line in order, but first understand each directory's overallRoles and file organization within directories , but it's not necessary to delve into understanding the specifics of how it's implemented for now.
For example, for the MallChat program, you want to be able to clarify the information in the red text first:
There is a little trick when looking at the organization of files inside a directory. You can right-click on a package directly in IDEA to view the UML structure diagram:
The structure diagram allows us to take a quick look at the relationships between classes. For example, in the following diagram, the factory creation strategy, and the two concrete strategies inherit from the abstract strategy class:
After that, you can find the core functional modules of the project you are interested in to learn, and share a few learning tips:
1) If you want to quickly learn the implementation of a feature, it is recommended that you do so through documentation, for example.Understand the business process first If you want to learn the source code, you should use Debug mode to start the project. When learning the source code, it is recommended to use Debug mode to start the project, send requests through the interface documentation (or front-end), and completely analyze the processing flow of a request.
You can use IDEA's Endpoints feature to quickly view the interfaces in your project and locate the source code:
2) If you want to get a quick overview of the core structure of a single file (e.g. methods and properties), you can use IDEA'sStructure
function, as shown in Fig:
3) You can use the Call Hierarchy function to view the hierarchy and relationship of methods, classes or variables being called in the program. For example, in the following figure, I viewed thedoMark
The caller view of a method allows you to see which methods call it, making it easy to quickly understand the interaction between the code without having to click through layer by layer to see it for yourself.
4) You can use the Find Usages feature to quickly see where a method or class is used in a project:
(5) Of course, for complex features, the code alone may not be able to understand, then you need to match the documentation and code comments. What? No documentation? No comments? Then directly through the Git version control tool to find the original author to ask.
6, understanding of the project development process and specifications
After you've familiarized yourself with the program, it's time to try your hand at development. You can start by going through theRead the project's contribution guidelines or development documentation To understand the project's development process, development specifications, etc., with the other developers of the project to maintain consistency. If you have doubts, you can look at what others do in the code, just follow the example; if you are really not sure, it is best to ask in advance, do not submit a bunch of code that does not meet the specifications, and then be beaten back by others, wasting each other's time.
7. Participation in projects
Finally, the best way to learn about open source projects is to get involved in the development of the project, which is something I always recommend, not only to improve your skills, but also to add points to your resume.
In fact, it is not that difficult to participate in open source projects. You can first check the bugs in the Issues section of the project and help to fix them, so as to familiarize yourself with the whole process of participating in open source projects. After that, you can alsoCommunicate with the author And try to add new features, make optimizations, etc.
If you want your contributed code to be accepted by authors faster, you must communicate well! Because I have a lot of open source projects myself, and I also have partners who have contributed code to me, but it's not possible to accept all the code. Because everyone's idea about the project is different, in the absence of communication with the author, you may do the function does not help the project, beyond the planning of the project, but will make the project more complex. Team development is the same, we should work together to review the requirements, to ensure that the requirements are valuable, and the project positioning is a match, rather than who want to add features to add features.
OK, the above is this period to share, the original is not easy, if you help, please praise support oh!