Why do you want to see the source code? How to look at the source code?

Foreword

The main contents of this article is taken from plus how much God "Java Concurrency in beauty," the preface content, talked about how to look at the source code and see how the source code, he said very refined. This section is part of the source code learning methodology, so down and extract individual summary.

Why look at the source code?

We usually encounter the following problems in doing the project:

  1. I do not know how to design. For example, started going to the workplace, to a summary of the design needs to be done, I do not know how to start, I had to see how the system is similar to the current needs of the design, and then modeled to design.
  2. The design, the problem is not considered comprehensive. Compared workplace novice, these people rely on their own experience to a demand has been able to come up with a preliminary design, but design often miss some unusual details, such as the use of multi-threading bounded queue mission, met the machine is down, If the queue inside the task without saving, then the next time you start the machine when these tasks are lost.

For these problems, the final analysis, mainly because of lack of experience, and experience accumulated mainly from the project practice , so recruiters usually limited working time is more than 3 years, because these people are relatively abundant project experience, scene encountered in the project relatively high. Accumulated from years of work experience and practice, but look at the source code can expand our thinking, this is a disguised increase our experience a good method. Although we can not gain experience in a short time by time, but you can gain experience by learning open source framework open source project .

In addition, into the workplace are generally required to be familiar with the existing system, if there are documents Fortunately, no documents, then you have to look through the code for their own research. If you have experienced before reading the source code, then code logic in the study of the new system would not be so strenuous.

Another point is that, when you use a framework or tools to do the development, implementation if you understand it, you can maximize reduce the possibility of failure . For example, concurrent queue ArrayBlockingQueue elements on which the team has put an offer methods and methods, although some point in time you know when to use offer method, when the queue is full it will be dropped into the team element, then offer method returns false, and It does not block the current thread; while using put method, when the queue is full, it will suspend the current thread until the idle queue element into the team after a successful return. But people are forgetful, unused for a while, forget their differences, and when you go to use, you need to go inside and put methods offer, look at their source code implementation. Offer a look into the method, Oh, the queue is full return directly false; methods put into the look, oh, the original queue is full, the direct use condition variables await suspends the current thread. They know the difference, you can choose according to their needs.

The biggest advantage is to see the source code can open thinking, the ability to enhance the architecture design . Some things alone books and think for themselves is difficult to learn, you must look through the source code to see how other people design and then ponder why this design can be realized.

Empowerment is not that much code you write, how much do the project, but rather to give you a business scene, you can come up with some tricky solutions, and say their advantages and disadvantages. And how can we come up with, one to rely on experience, and second, to sum up by induction, and look at the source code can quickly add to your experience .

A summary

Why look at the source of it:

  1. By learning open source framework, the source code for open source projects, we can learn a variety of solutions to the scene of multiple technologies, the rapid increase in experience ;
  2. When using a framework or tool for development, if you understand its source code, it can be maximized to reduce the possibility of failure;
  3. Benefit you study a code logic of the new system, a new system quickly took over;
  4. The biggest advantage is you can open thinking, the ability to enhance the architecture design , by looking at the source code, see how other people design and then ponder why this design can be realized .

How to see the source code

How to read the source code it?

Before you look at the source code of a certain framework, go to Google to find the official description of this open-source framework, by understanding the data frame has several modules, each module is doing what, what is the connection between, each module has what core classes in reading the source code can focus on looking at these classes.

Then interest which module to go write a small demo, first look at the specific role of this module, and then enter debug depends on the specific implementation.

In the process of debug, the first pass is cursory, brief look at the call logic, are used which classes; the second time the need to focus debug, look at these classes as an architectural view in which the function, use the What design patterns.

If you feel there is a second time, they generally know the function of the overall implementation of the code, but not very clear on the overall structure of the code, after all, more than one class calls back and forth inside the code, it is easy to forget at the current breakpoint;

Then you can be the third time debug, this time you'd better call a timing diagram of the major classes and class structure diagram drawn, and so after a good draw, then against the timing of the call flow chart analysis, you can clearly know the class between the call relationship, and can know the dependency between each functional class and their class by FIG. In addition, open-source framework which each function or class method generally have a comment, these notes are first-hand information , such as some comments JUC package of concurrent components, has indicated that their design principles and usage scenarios.

In reading the source code, it is best to draw timing diagrams and class diagrams , because people are always forgetful. If you've seen before from time to time and then go to the source, although some impressions, but when you want to see the logic of a module, but also to be based on a debug demo again from scratch. And maybe if you have a map, you can directly from maybe find a map inside, and look at the timing diagrams to know the context of the entire module.

In addition, check the best framework for use Quguan network check (which is the source of information is without someone to translate), although it is in English, but look long enough, there is still Google Translate na!

Of course, the study does not have to debug the code three times, in fact, said here are three master degree, if you debug again be able to grasp, it is naturally better friends.

Two summary

How to read the source code:

  1. Find Google open source framework of the official introduction, the following information
    • It has several modules
    • The role of the module
    • The link between the module
    • Each module of the core classes (in the process of reading the source, the focus at the Core Class )
  2. Interest which module to go write a small demo, first look at the specific role of this module.
  3. debug look into the implementation. (There can make full use of debugging function of IDEA)
    • The first pass is cursory, brief look at the call logic, are used which classes;
    • The second time the need to focus debug, look at these classes as an architectural view in which features , what the use of design patterns .
    • The third time debug, the call timing diagram of the major classes and class structure diagram drawn, facing the timing diagram call flow analysis

Note specific point:

  • Read source code, and the class is preferably a timing chart shown in FIG.
  • Comments are hand information, good comments often been described their design principles and usage scenarios

reference

  • "Java Concurrency in the United States"

Guess you like

Origin juejin.im/post/5d9aa29f6fb9a04e3b11111e