Yesterday and part of the online exchange, there are many users mentioned in the exchange to read the source code is more difficult, do not know how to get started. Fat brother shared some personal experience, here is a summary.

Reading source code is actually Debug source code

In fact, the so-called reading source code is not pure reading, but debugging source code. Nothing happens if you don’t do it. I usually debug source code from these aspects.

Sample and unit tests

A lot of the source code is the Samples sample project and unit tests, and you can start with executable code. Examples include unit tests and samples in Spring Authorization Server.

Both the sample and the unit tests are easy enough to use as a starter project.

Attaches great importance to the log

Every programmer should be aware of the importance of logging as a record of understanding the execution of logic. Online troubleshooting, most rely on logs to locate and track, so when looking at the source code should look at the log. Learn to read the log, pay attention to the log not only look at the source of help, daily development is also very helpful. A good developer is a good journal reader, so make it a priority to read your journal.

Specific goals

Be sure to read with a purpose in mind, for example, to understand the initialization process of a class, to understand a mechanic, etc. Details and concepts that are not part of this goal can be put aside for now, such as the following snippet:

I just want to understand the flow of the filter, so I’m going to make the flow clear, and I’m not going to bother with the details of each line. There are details within details, and you’re stuck in an infinite number of details. After you understand the overall process, look at the details of key steps, such as step 2 inside. Step 2 is the goal to understand the mechanism of AuthenticationConverter. Then I just look at AuthenticationConverter and I don’t care about anything else either. By doing the same thing, you understand the steps and then string them together to figure out the details of the process.

Look at the abstraction first and then the implementation

Flexible Java frameworks are oriented to interface programming. It is important to understand what this interface abstracts. Interfaces generally have one or more methods.

If I don’t know what it does now, and I want to figure it out, how do I get it? Here’s how I did it:

  • First look at the comments, see the author’s design intent, this is more important than debugging, good source code has detailed comments, including from the naming, some words can reflect what this thing is doing.
  • Look at the input and return values for each method to learn more about the return values.
  • See who references the abstract interface.
  • Finally, let’s look at what implementations it has. Based on some of the information learned in the previous steps, break some points to observe the analysis.

Learn to disassemble targets

A lot of people are going to study the Spring Bean lifecycle, the Spring Boot startup process, which is too big to be mastered in a short time. You need to break this big goal into a lot of smaller goals. For example, how are Spring beans registered? What did you do before registration? What did you do after you registered? Take them one by one, divide and conquer them, and then string them together. This not only reduces the difficulty, but also increases the sense of accomplishment. When you see someone write a paper and get it done, it’s a paper, but it takes more than a day to analyze, and a lot of it is a summary of several goals.

Disassembling goals is very useful in any job, and it pays off when you learn them.

To summarize

Taking notes is a must. I never met a bull who didn’t take notes. Programming is not about rote memorization. New frameworks are popping up and new concepts are popping up. Can you keep track? I can’t remember anything, including Spring Security. But I can take notes. It’s not a closed-book exam, so I can flip through it and come back. Markdown is recommended for its simple syntax and well-organized layout. All of my work is markdown.

Some debugging tips

Other I temporarily can not think of, here to share some IDEA debugging skills.

Learn to look at call frames

Learn to look at this Frames:

This is going to be viewed from the bottom up, and this is a record frame of some code that you’ve executed, and you can click on it and see how each frame is executed.

It’s kind of like a video clip, finding key frames to work with something.

Increase the Watches

Select a variable, Add to Watches, you can observe the change of this variable in the whole process, life cycle. It is used to observe some key parameters.

Expression operation

You can also select a variable and press Alt+F8 to manipulate it in the dialog box, such as getting the current property, instead of system.out.println! Like the following:

It also provides methods for changing values to verify your assumptions.

conclusion

Source code analysis is key to your advancement. Every developer will, and the ones who are in control are always the ones who are good at research.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn