In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is to share with you about how to look at the source code, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
1. Preface
Many people ask me how to look at the source code. Am I very talented in looking at the source code?
Actually, it's not. I'm just an ordinary person, just like everyone else.
It's just that I have no other special hobbies. When I have time, I either write my own code or read other people's code.
I am patient when looking at the source code, purely interest-driven, or a kind of curiosity.
Of course, I will not casually pick up an open source project and look at it, but after some understanding, I decided to look at its source code.
Once I have decided to read it, I will at least finish reading more than 80% of the code of this open source project. It is not a superficial view.
Instead, study each line of code carefully.
two。 I've seen these open source projects.
In chronological order:
2007:
OpenJDK Javac1.7
2008:
Erlang compiler (the one who saw the least, only half of the source code)
2009:
Tomcat6
Junit4
Ibatis2.3
OSCache2.4
Ehcache1.6
Mongodb-mongo-java-driver1.2
Velocity1.6
2010:
MySQL JDBC Driver (mysql-connector-java-5.1.13)
PostgreSQL JDBC Driver (postgresql-jdbc-8.4-701)
Netty3.2
Tomcat7
2011:
Jetty8
At present, we are mainly concerned with these four points:
OpenJDK Javac1.7
Netty4.0
Tomcat7
Jetty8
I now check whether there are any updates in the source code base of these four open source projects every 1 to 7 days.
I installed TortoiseHg, TortoiseGit, TortoiseSVN.
Because it is more convenient to use TortoiseHg to update the source code of Javac1.7.
Look at Netty4.0 using TortoiseGit, and the last two using TortoiseSVN.
3. I classified people who looked at the source code.
There are five types of people:
1) problem solving
This type of people usually encounter a puzzling or thorny problem in their work and study.
I read the document, google looked for it, and my colleagues and classmates also asked.
But the problem still couldn't be solved, so I had to download the source code and debug it while watching it until the problem was solved.
2) three-minute heat type
It may be that others are reading it, or on the forum, some people say that XXX is well designed and performs well, or that some people have posted some articles analyzing the source code on the forum.
In addition, I was full of interest at first, and then I followed the trend. I looked at the code of more than 10 classes. Ah, I found it too painful. I transferred from method to method. It was too roundabout, and my head was about to explode.
Give yourself a reason, this code is too rubbish, damn it, don't read it.
3) half-knowledge type
It is often seen on the Internet that someone is writing articles analyzing the source code, which comes up with the source code of Yituo, and then tells you what it has done and what it has done.
Just add a little Chinese comment, sometimes this Chinese comment is not as easy to understand as the English comment in the source code, and then after a period of time, I found that the article is not updated and there is no follow-up.
4) genuine talent and practical learning
Think like the original author, can easily state the core architecture of this open source project, accurately understand more than 80% of the source code, find out the bug and submit the corresponding patch
5) Innovation
Know the advantages and disadvantages of this open source project, can extract its essence for my use, come up with a better solution to the existing problems, beyond the original author.
4. My experience in looking at the source code
For reference only, do not imitate casually, everyone should find a way that suits them.
The only thing that matters is the following three points:
1) time
It's best to concentrate on it for a long time. For example, if you want to watch Tomcat7, you should spend 3 to 8 hours a day watching it for 3 months.
The longer the time drags on, you will look at the front and forget the back.
2) interest
Look at the code without any utilitarianism, you should be interested in it, full of curiosity, to understand what it does, how to do it.
3) patience
This point is easy to say, but it is extremely difficult to do it. For example, if you can't understand some classes after watching them three times in a row, don't think about whether the code written by others is rubbish.
First, think about whether you have enough background knowledge in this area. For example, when looking at the code related to the Tomcat implementation of the http protocol, looking at the code while looking at the http protocol is the most effective way.
For example, if you want to see the code related to the compiler, at least you should have a basic understanding of the content of this course before you read it.
What if you still can't understand the background knowledge? put down the code that you don't understand. There are some if branches that can only be understood after you have roughly understood the whole project.
To look at it over and over again, three times is just an entry level.
Let's start with a brief account of my experience:
I am still a good student from primary school to junior high school, and I often get the first place in my class.
But the four years from the sixth grade of primary school to the third year of junior high school were quite special. I met a very rubbish math teacher.
So my math is basically self-taught in the past 4 years, and my self-study ability is very strong from childhood to now.
Of course, this can not be attributed to this garbage teacher, I have been very rebellious since childhood, do not like this animal's way of teaching, without this garbage, maybe my life would be a different scene.
I don't finish junior high school and high school like normal people, and then go to college.
But after junior high school, he went directly to a normal university to go to a technical secondary school + self-examination college, and graduated in 2001.
Technical secondary school is majoring in accounting and statistics, and since the junior college entrance examination is computer application, there are only 12 courses in junior college, there is no English, there is only advanced mathematics, and it is the first volume.
There are only pascal, c, 8086 assembly language, FoxPro, analog circuit, data structure, operating system, software engineering, computer interface and technology in the core professional courses.
As long as it is not a professional hardware course, I am very good at it. I just listen to the teacher. I didn't even know what the hard drive looked like at that time.
I was only 19 when I graduated in 2001, and then I came out to look for a job. I just wanted to say that I didn't get much education from school.
In retrospect, the C language, assembly language, data structure and operating system I learned from school provided some help for my future self-study.
Until 2006, I resigned. I had been working for four years, and I was only working on application software projects.
So math is basically unused, and English is rarely used. English is actually worse than the current level of senior high school students.
Therefore, I can't understand it if I want to read English technical articles.
I was supposed to review the graduate entrance examination in 2006. I began to recite English for three months in March. I spent four to eight hours a day memorizing New concept English texts.
I memorized all the first two volumes, the third volume memorized the first 42 lessons, and the fourth volume memorized the first 10 lessons. I thought this level was enough for the postgraduate entrance examination. As a result, I took the examination paper and did it.
Reading comprehension has at least 2 stroke 5 words can not be recognized, have to memorize the English words of the postgraduate entrance exam.
I basically forgot all about math. I went to the new textbook of junior high school and senior high school under the Internet.
High mathematics, linear algebra, introduction are all self-taught, but also bought related mathematics books to read (such as geometry, discrete mathematics, etc.).
Every day to do those junk math problems, but also to memorize Ma Zhe, Mao general, professional courses are pediatrics.
Until October, each school came out the enrollment brochure and wanted to apply to 10 colleges and universities. as a result, others did not care about your junior college students, and your junior college students were not qualified to sign up.
All right, can we change to the second line? the result is still the same. it is not necessary to have an undergraduate degree, or to publish any papers in order to report.
Finally, I reluctantly signed up for a mediocre so-called 211 university.
Since then, I feel more and more that it is extremely boring to recite words every day, coupled with the fact that applying for the examination has dealt a serious blow to my enthusiasm. I have always wanted to give up the postgraduate entrance examination but have been insisting on it.
Until November 23, I went to the bookstore to read a book and found a book about compiler implementation.
That is, the so-called "tiger book". I didn't know what the compiler was at that time, because I didn't learn the principle of compilation when I was at school. I read it in the bookstore for an hour and thought it was very interesting.
I bought it back, and then I threw all the books for the postgraduate entrance examination into a corner.
The next thing you should know, I'm crazy about buying books, "Dragon Book", "Whale Book" and so on. As long as it's about compilers, I buy them both at home and abroad.
Because I didn't understand it all at once, but also because it was easy to understand the first one or two chapters of the Book of Tigers, but I couldn't understand the rest at that time.
I have a habit that if I really can't read a book, I will change it and make sure it is my own fault or the problem of the book itself.
Now I can only say that "Tiger Book" is not well translated, "Dragon Book" is easier to understand, of course, it is not so easy to understand "Dragon Book" from the beginning.
So I even bought books related to formal language and automata at that time.
I have been reading for more than 3 months. At that time, I felt that reading was not enjoyable, so I wanted to find an actual compiler to play with.
I went to GCC and LCC, when sun opened up javac again.
Because only php and java,c have not been used in my work for 4 years, and javac is much smaller than the former two.
So it is the first time to look at the source code of javac since February 2007, and it is also the first time to look at the source code of others.
Javac doesn't have much source code, less than 80, 000 lines, and it took me 3 months, spending an average of at least 7 hours a day reading the code.
Because I only knew a lot of things that came out after java1.4,java1.5 at that time, I also learned the new grammar while looking at the source code of javac.
In May 2007, I also posted my first article about javac on JavaEye. I got the essence and caused a little sensation.
Here's the evidence: http://www.iteye.com/topic/84833.
The content seems to have been deleted by me at that time. I can't remember why. Zhh3007 is my former id.
At that time, the way to look at the code was very primitive, but until now I still use it, but now I sometimes use eclipse to look at it.
Here's what I did:
Look at the original code using EditPlus
Find the first entry class: com\ sun\\ tools\\ javac\\ Main
Write another Debug class yourself, and when you see a method according to the execution process, add a code block similar to the following:
Public static int compile (String [] args) {try {/ / DEBUG.P (Main.class, "compile (1)"); DEBUG.PA ("args", args); com.sun.tools.javac.main.Main compiler = new com.sun.tools.javac.main.Main ("javac") Return compiler.compile (args);} finally {/ / the DEBUG.P I added (0dm Main.class, "compile (1)");}}
Then be sure to recompile the source code and run it again to make sure that the code I added is executed. I will redirect the output debug result to a file.
Then look at the source code while looking at the output, some variables or expressions will also add DEBUG.P (.)
When I come across some classes with many lines of code, I even put each method copy into a new java file
Then open multiple EditPlus, each EditPlus to see a method, and open the EditPlus in the order in which the method is called.
Many people may think that this way is very corny, but I can't say whether it is good or not, starting from this year, because my computer is equipped with Eclipse.
So I use Eclipse to look at the source code of Jetty8, and I no longer type DEBUG output or copy method, but directly use eclipse's debug tracking function.
But now I question it. The code amount of Jetty8 is only 1 Tomcat6 3. I can't save my total time when I use Eclipse to study the source code.
It only took me three months to study Tomcat6 in the most primitive way, and now it has taken me two months to study Jetty8, and look at Tomcat6 and Jetty8 are two similar things.
It should take less time to watch Tomcat6 first and then Jetty8.
I summed up why it was good in the first place, because I had indirectly remembered the layout of the code in the process of constantly messing around with the source code.
When I typed the repeated DEBUG.P code, I printed out local variables, expressions, and fields, which helped me remember.
On the other hand, the debug function of Eclipse just keeps pressing F5-F6-F7-F8 and breaking points, and it is difficult to sort out the relationship when the method call is too deep.
Sometimes after looking at it for a week, I don't even know which class is in which directory, because when debug, the related class eclipse will automatically open that class, so you don't have to find it from the directory.
The code folding granularity of Eclipse cannot be folded to the block level, nor can it collapse if or while. Once there are a large number of lines of the method, it will look very tired and can not distinguish the hierarchical structure of this method.
And EditPlus is very comfortable when looking at the method, because he can fold if and while, and after watching a while, I can fold him up.
When there is more than one if-else, it will not exceed one screen as long as it is folded.
You can open 20 EditPlus, but you can't open 10 Eclipse at the same time, unless your computer's memory is too bad.
It is important to build the environment either way, to be able to compile the source code yourself, and to write more examples to verify the execution flow in the source code.
I don't usually look at the source with examples or test cases in the code, but only when I can't think of it (especially when I look at the compiler, some use cases are hard to think of).
Why should I read so much source code?
In addition to personal interests, there is actually a very funny reason to think about it now:
In the second half of 2007 and the whole year of 2008, many people said that Java was dying and Ruby/Rails and Erlang were very popular.
I often use JavaEye, which is inevitably affected, so in 2008, I also studied Ruby/Rails, Erlang, and even played with the compiler of Erlang.
Later, I found that it was not like that, it was just some big guys advocating it, plus something happened at home in 2008, so I was very depressed.
Whenever I have leisure, I "fight" with the bosses of JavaEye, which is also a kind of fun and a way to relieve stress.
Of course, at the same time, I also think about what's wrong with Java from a technical point of view, so I've been focused on technology since 2009, and I've been tinkering with the prototype of douyu.
The open source projects studied in 2009 are actually very natural. I want to build a http server. Tomcat has been done. I want to know how he did it.
I went to see these projects in order to know what they did, how they did it, where they didn't do well, and whether I could do better than them.
Now, it is my habit to read the code. I bought tens of thousands of yuan of books from the year of graduation to 2008, so that I didn't return the house in Guilin after I came to Hangzhou.
Because of the large number of books, the house in Guilin is now rented to books except for a few days back.
From 2009 to now, I haven't bought any more books for more than two years. I just look at new things in source code.
If it is a completely new field, the best you can do is to find some introductory materials on the Internet, and then look at the source code.
Including in the second half of the year, I am going to study HotSpot, which is already the field of CCMG +. I don't have any study pressure, it's just a matter of time.
A person's self-learning ability is very important. I teach myself everything related to Java and everything except Java.
I didn't ask anyone in particular, nor did I take part in the training. I looked up materials and bought books where I really didn't understand.
If you can't understand the code, you must understand these points:
1) whether the relevant background knowledge is available
2) be patient and watch it several times
3) Don't expect others to tell you the answer. Don't say that others are superior and ignore you if they don't tell you, because it's not the duty of others.
4) after comparison, say that other people's code is bad.
The above is how to look at the source code, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.