Reading source code skills

Reprint--Original address: www.don't
just inadvertently saw a post about source code reading in the forum. Looking back on my previous years, the excitement and sense of accomplishment (1) of reading the source code, I can't help but have another kind of excitement. Source code reading, I think there are three core points: technical foundation + strong curiosity + patience. When it comes to the technical foundation, let me draw an analogy, such as... I
inadvertently saw a post about source code reading in the forum just now. Looking back on my previous years, the excitement and sense of accomplishment (1) of reading the source code, I can't help but have another kind of excitement.
Source code reading, I think there are three core points: technical foundation + strong curiosity + patience.

When it comes to technical foundations, let me make an analogy. If you have not studied Java, or any programming language such as C++, it is difficult for you to absorb nutrients from it when you start to read "Core Java", especially It is a book like "In-depth Java Virtual Machine", which others think is good, may not be suitable for you now.
While Tomcat's source code is beautiful, I would never recommend reading it in the first place. I will talk about this specifically in the article, and I will not expand it for the time being.

A strong thirst for knowledge, I think, is the core driving force for reading source code. I have seen the vast majority of programmers, and their attitude towards learning is basically these levels (very extreme):
1. Only focus on the project itself, and baidu if you don't understand it.
2. In addition to doing a good job in the project, you will also read technical books related to the project, see wikipedia.
3. In addition to reading books related to projects, you will also read books in the IT industry. For example, when learning Java, you will also learn about functional languages, such as LISP.
4. Look for some open source projects, try a lot of third-party frameworks, and write demos.
5. Read the basic framework, J2EE specification, Debug server kernel.

Most of the program supervisors are the first type, and the fifth type requires not only strong interest, but also courage: Can I read it? Actually, you can read it.

Patience is really important. Because you rarely see instructive articles or books for reading source code, and no one asks or recommends that you read them. You will often get stuck while reading, and one card owner may be stuck in a maze. At this time, what you need to do may be to temporarily interrupt it, and then look at it from the periphery: such as the API structure and the design diagram of the framework.

I will talk about how to read Java source code, as well as my previous reading insights.

First contact with Java source code
If you have been developing for about a year, you like to use the debug function of eclipse. Well, you now have the technical basis for reading source code.
I recommend starting from the JDK source code, which is directly integrated with eclipse and does not require any configuration.

You can start from the JDK toolkit, which is the Java version of "Data Structures and Algorithms" we learned, such as List interface and ArrayList, LinkedList implementation, HashMap and TreeMap, etc. These data structures also involve sorting and other algorithms, killing two birds with one stone.
During the interview, the examiner always likes to ask the difference between ArrayList and Vector. If you spend 10 minutes reading the source code, it is estimated that you will never forget it for a lifetime.

Then there is the core package, which is String, StringBuffer, etc.
If you have a certain Java IO foundation, then you might as well read classes such as FileReader. I suggest everyone to take a look at "Java In A Nutshell", which has the architecture diagram of the entire Java IO. Java IO class library, if you do not understand its interfaces and inheritance relationships, it is always confusing to read.
The Java IO package, I think is the most elegant use of inheritance and interfaces. If you are an architect in the future, you will definitely deal with it frequently, such as the development of core classes related to deployment and configuration in the project.

When reading these source codes, you only need to understand some core classes, such as 20 or 30 classes similar to ArrayList. For each class, it is not necessary to supervise each method. Some methods like String have reached the virtual machine layer (native method), such as the hashCode method.

Of course, if you are interested, you can check the source code of JRockit, the same set of API, two implementations, it is very interesting.
If you want to drill again, you might as well look at the code for the virtual machine, such as the principle of System ClassLoader, which is not in the JDK package, and the JDK is based on it. The source code Zip package of JDK is only about 10 M, it seems to have about 50 M, and Sun has downloaded it, but it is very secret. I was excited for a while to find and read it for myself.

Java Web development source code
Before reading Tomcat and other source code, there must be a certain accumulation. My practical experience, it can also be said that a better ladder is:
1. I have written some Servlet and JSP codes. Note that it is difficult to touch the essence of Servlet without using Struts. Using Struts well is just the skin.
2. Read "Servlet and JSP Core Programming"
3. Read Sun's Servlet Specification
4. Read rfc of http protocol, debug HTTP packets
If you have the above foundation, I don't recommend you start reading Tomcat source code. I suggest that you read the Struts source code before reading the Tomcat source code. The Struts source code is much simpler than WebWork. This framework is 100% readable, at least I didn't understand WebWork 100%. I once wrote a web framework myself because I read the Struts source code.

Of course, before reading the Struts framework, it is best to read its MailReader and other demos, which are very, very good.
If you've done some Struts projects, you'll be more comfortable reading it.
Before reading Struts, it is recommended to look at the source code of mvnforum, which partially implements the functions of Struts, although this BBS does not dare to compliment it.

If you've read Struts, start thinking about reading the Tomcat source code.
However, I still do not recommend reading it directly, first read the series of articles "How Tomcat Works" on the onJava website, it is the simplest version of Tomcat. It tells you how HttpServletRequest is implemented inside the container, how Tomcat accepts external requests through Socket, and how your Servlet code is called (callback) by the Tomcat container.
To learn JSP, be sure to study the Servlet source code compiled by the container to JSP.
Why I always call Tomcat a container instead of a server is a question left to you.

If you must read Tomcat, then read Jetty. At least it is embedded, you can set breakpoints and debug directly in eclispe. Although Tomcat also has an embedded version.

Java database source code reading
I recommend that you read Sun's JDBC specification first.
I think you must have written JDBC code, then you can start reading the source code at this time.
If you understand the JDBC specification (interface), then its implementation and JDBC Driver must be understood. My suggestion is to read the mysql jdbc driver, because it is open source and elegant in design. When reading mysql's JDBC driver source code, it is recommended to look at the inside story of mysql. The official book just happened to have a book, "Mysql Internals", which I read part of five years ago. For example, you can know how the JDBC driver of mysql interacts with the mysql server developed in C++ through socket packets (connect, query).

Through the above reading, you can know how your business code, JDBC specification, JDBC driver, and database work together.
If you understand these insides, then you will be handy when you learn persistence frameworks such as Hibernate and iBatis.

After reading the JDBC driver, the next step must be to read the database. And there happens to be a powerful database developed in Java, Hsqldb. It is an embedded database, such as used in desktop client software, such as Mail Client.
I wrote a small article about this four years ago, so I won't introduce it.

Java messaging paddle client software
I highly recommend instant messaging software wildfire and Spark. You can understand wildfire as an MSN server and Spark as an MSN client. They communicate via the XMPP protocol.
I used to customize Spark in a project, of course, including some changes on the server side. So I have read the source code of them.
The reason why I recommend them. This is because:
1. XMPP is lightweight enough to be easily understood
. 2. Learning Socket communication implementation, especially C/S architecture design
. 3. Modular design. They are based on modules. You can understand both the modular architecture and the technical support of modularization: the application scenario of the ClassLoader of the Java virtual machine.
4. Event Driven architecture. While GUI supervisors are Event-driven, Spark's design is especially elegant. Let's

just say, read their source code and you'll be proud of being a programmer, because both their architectural design and code supervisors are so pretty.

Java enterprise application
Of course , it is frameworks such as Hibernate and Spring.
Before reading the Spring source code, be sure to read "J2EE Design and Development" written by Rod Johnson, which is the design idea of ​​Spring. Note that it is not the Chinese version, the Chinese version is completely ruined.
Before reading the Hibernate source code, you must read the book "Hibernate in Action" written by Gavin King, and at the same time, you should read "Enterprise Application Architecture Patterns" written by Martin Fowler, which specifically talks about the design ideas of the persistence framework. When you think you have read through these two books, go to their source code.
Moreover, before reading the source code, you will find that they use a lot of third-party Jar packages, twenty or thirty, you'd better understand those Jar packages one by one.

When it comes to enterprise applications, workflow is definitely involved. I read the source code of jBPM back then, and there are articles on the Internet that introduce the jBPM kernel (Silver Fox). I feel that its kernel is only 2,000 lines, don't be afraid. I used to read the blog of jBPM source code.
Of course, to read the source code of the workflow, the premise is that you must have a deep understanding of its theoretical model, and have written some demos or done some projects.

The ones I have introduced above are ones that I have read myself and are also suitable for ordinary people to read.
I have also read some non-Java source code, I feel good, and I recommend it to everyone:
The dojo source code has a very elegant architecture design, imitating Java's imports and extends. But the actual application is a mess. We developed our own framework based on this, but I am not the main force.

Flex source code Flex was just open sourced at the end of 2008, I used it to do a medium-sized project, it should be said to be a domestic technology pioneer. There were no in-depth books and no open source projects on the market at that time. I just looked at Flex's Help documentation and source code to get the project done. Two or three years have passed, and now I think the system design is quite elegant.

Well, let's introduce it here first.
The Java source code mentioned above, I have read 4 years ago or even earlier. Technological changes are so fast, like the rapid development of the Internet, which has spawned many high-performance, distributed databases, such as hadoop. When I looked at it, I realized that I had fallen behind.
In the past few years, there must have been many excellent frameworks, and you may wish to share them.

Postscript to the question In
the past three years, I have been starting a business, mainly for technical applications, but for business. I don't read much source code, but I appreciate those fanatics who focus on technology.
Let's take a break from starting a business and enter an e-commerce company, responsible for the revision and operation of its B2C website.
(Advertisement) If you have a soft spot for technology and large-scale B2C development with high load, you may wish to take a look here.

Guess you like