Yesterday, I had an online communication with some netizens. Many netizens mentioned that it is difficult to read the source code, and they do not know how to get started. The fat brother shared some personal experiences, which are summarized here.
Reading the source code is actually the Debug source code
In fact, the so-called reading source code is not simply reading, but debugging the source code. If you don't see it, you can't do anything. Debugging source code I usually start from these aspects.
Samples and unit tests
A lot of the source code is samples sample projects and unit tests, you can start with these executable code. For example unit tests and samples in Spring Authorization Server .
The samples and unit tests are very easy to get started with.
Pay attention to the log
Log as a record to understand the execution of logic, every programmer should know the importance of it. Most online troubleshooting relies on logs to locate and track, so you should look at the logs when looking at the source code. Learning to read logs and paying attention to logs is not only helpful for looking at source code, but also helpful for daily development. A good developer must be a good log reader, so you should pay attention to reading the log.
with a clear purpose
When reading, you must read with a goal. For example, today's reading is to understand the initialization process of a certain class, or to understand a certain mechanism, and so on. Details and concepts that are not in this goal can be put aside for now, such as the following snippet:
I just want to simply understand the filtering process of this filter, and I will mark its process clearly, and I will not bother with the details of each line. There are details within details, so you get caught up in infinite details. After understanding the overall process, look at the details of the key steps, such as the step ② inside. Take to figure out step ② as the goal to understand the mechanism of AuthenticationConverter
. Then I just look at AuthenticationConverter
and don't care about the rest. Follow the same method to understand several steps, and then string them together is equivalent to clarifying the details of the entire process.
First look at the abstraction and then look at the implementation
The flexible Java framework is all interface-oriented programming. It must be clear what the interface abstracts. Generally, the interface has one to several methods. For example, AuthenticationProvider
has only two methods:
If I don't know what it does now, I want to figure it out, how should I start? Here's what I did:
- Look at the comments first, and look at the author's design intentions. This is more important than debugging. Good source code has detailed comments, including naming, some words can reflect what this thing is for.
- Look at the input parameters and return values of each method for details on these return values.
- See who references this abstract interface.
- Finally, let's see what implementations it has.
According to some information learned in the above steps, hit some breakpoints to observe and analyze.
Learn to dismantle goals
Many people come up to study the life cycle of Spring Bean and the startup process of Spring Boot . This is too grand and cannot be mastered in a short time. You need to break this big goal into many smaller goals. For example, how is Spring Bean registered? What did you do before registering? What did you do after registering? One by one, divide and conquer, and finally string them together. This not only reduces the difficulty, but also improves the sense of achievement. You see someone wrote an article to get XXXX, the article is an article, but his analysis time can be more than one day, many of them are a summary of the results of several goals.
Dismantling goals is very useful in any job, and once you learn it, the benefits are endless.
must summarize
Notes are a must, and I haven't seen anyone who doesn't take notes. Programming is not about rote memorization. New frameworks are emerging one after another, and new concepts are changing with each passing day. Do you remember? Anyway, I can't remember anything, including Spring Security . But I will take notes. It is not a closed-book exam. I will look it over when I need it, and come back again. It is recommended to use markdown here, with simple syntax and organized layout. Lets say all my creations are markdown .
Some debugging tips
I can't think of others for the time being, so here are some IDEA debugging skills.
Learn to read call frames
Learn to look at the following Frames :
This should be viewed from the bottom up. This is the recording frame of some code you executed. You can select it with the mouse to view the execution of each frame.
It's a bit like video editing, finding keyframes to process something.
Increase Watches
Select a variable, Add to Watches , you can observe the changes and life cycle of this variable throughout the process. It is mostly used to observe some key parameters.
expression manipulation
You can also select a variable, press Alt+F8 , and then you can manipulate this variable in the dialog box, such as getting the current property, don't just System.out.println
! Like the following:
You can also change some values through the methods it provides to verify some of your inferences.
Summarize
Source code analysis ability is the key to your advancement. Every development will be adjusted, and those who can control it are always those who are good at research.
Follow the official account: Felordcn for more information
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。