Source code reading methods and skills summary

Reading break rolls, keystroke if God.

Outline

To be a writer, you need to read a lot of literature; to become a leading developer, you need to read a lot of good code. Excellent ability to interpret the code, is one of the developers of internal strength.

So, what is the goal of reading the source code is it?

  • The interpretation of design ideas, understand the technical principle and encountered similar problems, can be applied to the solution;
  • The accumulation of good code, rather than re-create the wheel.

If better read the source code? Not immediately begin to read, but do some pre-analysis work. For example, think about: if it is their design, how to do, what factors to consider?

How can one truly understand the source code? Generally do the following:

  • Pre-analysis and feedback summary. To design their own, thinking what elements; excellent implementation, consider what elements. Make a comparison, to have more insight and harvest;
  • Design ideas. Which contains the design, recorded;
  • Core Technology. What is the point of the core technology, it is irreplaceable?
  • detail. What are noteworthy details.

If we can fully understand and learn design thinking, the core technology and excellent detail code, we can say that this truly understand the source code.

Process steps

STEP0. Understand the overall concept and design of the system

Excellent systems usually have an elegant overall design, by a set of simple concepts series. Best to first understand and is based on the concept of the overall system framework, would understand the structure and operation of the system has an overall guiding role. Meanwhile, in the design of their systems, you will derive great inspiration.

For example, the design and implementation Proxool database connection pool, including: connecting ProxyConnection object definition associated with the database, associated ProxoolDataSource object data source query, the ConnectionPool objects and ConnectionPoolDefinition objects associated with the connection pool definition and management, connected to the flexible management Prototyper related tasks and HouseKeeper task, and the corresponding thread PrototyperThread, HouseKeeperThread. These two threads as a timing task, the connection pool configuration according to the scanning timing of the connection pool, or create a new connection recovery idle connection. And global managers, including connection pool for ConnectionPoolManager objects registered for Prototyper registration and start the task of PrototyperController, for HouseKeeperController objects HouseKeeper registration and start of the task. In short, a reasonable target allocation of responsibilities, carry out their duties.

STEP1. Familiar with the project code organization structure

Excellent open-source projects usually have a clear and hierarchical organizational structure of the project.

Get the project source code, the first thing: Overview project organization, which the main module (package), each module (package) clarify the role of the major; each module (package) the following are the main class of how its role. This step can be bold guess method plus instructions to complete API documentation.

Typically, the topmost package classes and methods used are often directly related to the client, which the lower sub-packets to complete a sub-function or special modules. In addition, each point will have excellent project code is usually involved in the system has a good abstraction, using a small and simple class or interface to express, rather than mixed in a large class. Even just some simple color string, will try to achieve it by enumeration. Many Javaweb project defaults Controller - Service - (Dao, Http) - Model - Utils overall organizational model, which further reduces the difficulty of reading the Java project.

STEP2. Set goals to narrow

Before you start reading the source code, was to set a goal to read the drive. For example, to explore the connection pool proxools sometimes get less than the database connection problems, or to find out extjs internal paging controls PagingBar & java thread pool ThreadPoolExecutor implementation. To invade a small branch, it will reduce the read range to the extent that easy to accomplish. A physicist nor can all of a sudden come to understand the universe.

STEP3. Find the entry point

It can be cut from the external behavior. Read the API documentation, understand its external behavior, needs of the target figure for the design, understanding how these classes or modules is done in collaboration overall goal of reading (if any) or draw the corresponding collaborative design; In addition, according to own development experience and intelligence, active thinking and bold guess: if it is their will how? It may be achieved by what way?

You can also cut the actual concerns. For example, the use of connection pooling Proxool, I am more concerned about is how to get, you can start from ProxoolDataSource.getConnection database connection method, see "Proxool connection pool resolution (in)." For example, use the thread pool usually cut from ThreadPoolExecutor.

STEP4. Classes and methods and the main core of the lock

Any design would implicitly or explicitly division of labor "key role" and "supporting role". Read the source code is not blind aimless behavior, but first locking main and core classes and methods as reading lights lead. For example, the key Proxool class are: ProxoolDataSource, ProxyConnection, ConnectionPool, Prototyper, HouseKeeper, supporting class has ConnectionPoolManager, ConnectionPoolDefinition, PrototyperController, HouseKeeperController like.

STEP5: mark the main processes, collaborative interaction diagram draw

Skip all the details, figure mainly in the main flow, the participation of which modules, classes, and methods which, marking, drawing collaboration interaction FIG.

STEP6: decomposition detail, all broken

After completing STEP5, usually the frame've got a whole understanding, although there are many details unclear. It's ok! Development framework for the pursuit of flexibility and scalability, as opposed to code more difficult to understand some of the business system. In particular details of the intertwined and interdependent influence. The more details broken down into small tasks separately. This step needs to be repeated several times, you may need to consult a lot of knowledge and exposure to some of the "dark place" in order to gradually reach the system of "truth" and eventually make its advantages and disadvantages of reasonable comprehensive assessment.

STEP7: practice, practice!

You can start with some small components, frameworks. For example, Log4j, junit3, java collections framework, these involve only a small number of concepts, interactive and design patterns, a little bit of patience can overcome down; the next, you can start from the actual use of the framework, such as Struts2, Spring, figure out the core module, such as struts2 MVC and interceptors, SpringIOC; if necessary, you can read Java concurrency library the difficulty does not lie in the interaction, but rather the complexity of concurrent itself.

Pattern recognition and accumulation

Coding and programming have some model to follow. These codes are familiar with design patterns to understand the source code is also very good help. For example, to object-oriented systems are usually several fundamental mode.

Independent class

Independent class to complete its separate functions, references to other class methods, but not complicated interaction. For example java.util.Arrays, java.lang.Integer class;

Support class

Class support for a small and simple expression of abstraction, it is generally not directly used, but referenced by other classes to construct more complex functions, such as java.util.AbstractMap $ SimpleEntry;

Inheritance type interactions

Interactions inherited type of follow the "Interface - abstract - concrete classes" model: an interface specification prescribed behavior, abstract class for complete customization framework to achieve, achieve specific concrete class then full functionality, it can be directly used. For example Map -> AbstractMap -> HashMap

Inheritance interactions, to understand how the design of the interface is, why you need to define these methods; to understand how design is an abstract class, how will the core methods and common processes extracted.

Delegate interactions

Delegate interaction follow the "package or agent" mode, will achieve some or all of the functions delegated to implement forward other classes, you may do a little package or proxies. For example, FutureTask will perform Callable tasks entrusted to Sync, Sync while also inherited in AbstractQueuedSynchronizer.

In practice, usually a mixture of a plurality of modes. In solving specific design problems, will apply to some common design patterns, such as Singleton pattern, observer pattern, decorative mode, proxy mode, the chain of responsibility pattern, etc., are familiar with these design patterns is necessary, see "Design mode - Elements of reusable Object-oriented software, "a book.

Tips and tools

  • While reading comments. Where do the necessary to read notes, highlighting its main purpose, the necessary implementation details; you can make notes while being read.

  • Build environment, run, debug. Build a good source environment, write a single test, run up, and then set a breakpoint debugging, observations, good tracking mode;

  • From simple start, good decomposition of small tasks. To Spring, Tomcat source code can not start? From JDK, Junit looks; do not understand Extjs Grid code? From Label, ComboBox started; a huge kind of hard to see? It might be broken down into smaller tasks, separately.

  • Find some other things doing. Read the source code is not the world's most wonderful thing. If at first not very adaptable, you can read the first several functions, and then do some other things, such as Events, Tingtingge look at the video, and then come back to read, so repeatedly, to gradually enhance this sense of patience and adaptation.

Psychological test

The place is difficult to understand the challenges and opportunities for promotion

Always encounter can not read, can not figure out the place; this time, you may need to make up the basics down, it could learn some advanced techniques, must not miss this opportunity to learn.

Patience and perseverance

This is a must over the threshold, we must overcome the city. A little more difficult, it takes a lot of patience and perseverance, but, once the capture, to lay a good groundwork for the back forward.

summary

Like any one skill, like reading the source code skills but also from the training base little by little, until the skill, consummate state.

Guess you like

Origin www.cnblogs.com/lovesqcc/p/11067921.html