Original article: Why I love reading other people's code and you shoshould too by Alan skorkin
Translation: Yurii
I hate reading code
I found that many programmers hate to read code-please don't hide it, admit it. Almost everyone likes to write code. However, it is really not easy to read the code, and it is still annoying and unavoidable. The code written by others is always very spam (even if we don't say it, we think so in our hearts ).Even if you write your own code, it will be a little Spam in a few hours.And a long time later, it will be more junk. So, what is the reason to look at other people's poor code, rather than writing a few pretty hands on their own? Can you ignore this problem for the time being, spend a few hours reading other people's code, and then look back at your own pretty? Even if a master sits in front of you, if you cannot learn from him, you will never become a master. One way is to find such a master and let him teach you all his knowledge. Is this possible? -- Of course there are, but it is difficult to get lucky. But you don't have to be so lucky,I am lucky to be a programmer because all the ideas and skills of the masters are embedded in their code for us to learn.. All you have to do is read the code. Of course, if someone tells you a little bit, it will save more time, but this is basically a dream. In other words, to be a good carpenter, you have to study a lot of exquisite furniture carefully.
I love to read code. I always think that the gains of reading code are huge. Yes, it may be annoying or even annoying, but your benefits are definitely greater than this. For example, if you want to be a big writer, is it enough to concern yourself with writing? You can give it a try, but it is estimated that you will not be able to make any effort. It is widely accepted that most writers are eager to read a lot. To write a good work, you must first read the work of other writers, absorb different styles, understand others' attempts, and then cultivate your creativity. In this way, your knowledge can be accumulated gradually, and the content you write will become somewhat mature, and you will find the "Sense ". The same is true for code writing,If you don't read The Great code, how can you write the program beautifully?? Reading great code is like reading great works to writers (I am not sure about the beauty, but Peter norvig is a famous lisp programmer, the current Google R & D Director said that he is not a simple character, so remember this sentence ).
That is to say, you do not believe the above, but there is no denying the fact. To be a competent developer, it is very important to be familiar with the code. Today, as long as it is not a joke project, it is the result of team collaboration, so there will always be such code: it is not written by you, but you must deal with it and modify it, or expand. As a result, reading the code is probably the most important and most commonly used ability on your body. Just bite your teeth and practice this skill-and it should be faster.
Like... Those guys who will read the code are the same... Read code
I can't tell how many times I have seen such a thing: the programmer dragged a piece of unfamiliar code up and down, and then looked down a few minutes later. Soon they announced that this piece of code could not be understood at all, not to waste time, but they could find a way to bypass this issue. I don't know what they really want to do. I just need to figure out the meaning of the Code, or keep my eyes open and wait?Reading the Code does not rely on watching it for years. What you need to do is understand it and turn it into your own. The following are some of my skills, but not all of them are useful.
Looking at the code
1.Try to build and run the program yourself. This step is usually easy to do, for example, when you are looking at the real running code (rather than the odd code from unknown sources. However, it is not easy to build and run every time. If it is not easy to build and run, you can recognize the high-level structure of the Code during the process of building and running. Also, read the working code and you will be very familiar with how to build a project. Building is usually complicated, but understanding the building process and how code becomes executable will greatly deepen your understanding.
2.Don't stare at details. The first thing to do when reading code is to find the structure and style of the Code. You should first browse and try to find out what each part of the code is doing. In this way, you can familiarize yourself with the high-level structure of the entire set of code and understand what code you are looking at (the reconstruction is good, or it is messy ). In this case, you need to find the entry (which may be the main function, Servlet, Controller, and so on) and see how the branches below are expanded. This step should not take too much time. You can return it at any time after you are more familiar with all the code.
3.Make sure you understand all construct). Unless you are very proficient in this programming language, you may not know anything about it. In the rough understanding of the Code stage, you should note down every unfamiliar structure. If there are many such structures, the next step is not self-explanatory. If you do not understand the meaning of code behavior, you cannot do anything. Even if you are not familiar with only a few constructor structures, it is a good thing to observe them carefully. You may find some unknown knowledge about this language, and I am happy to spend a few hours on it.
4.After you are familiar with most of the structures, you can perform several random in-depth probes.. The method is similar to step 1, which allows you to quickly browse the code base. However, you need to randomly select a few functions or classes and read them in a row. The real test starts now, but your benefits are also shown from this step. The main purpose is to go deep into the concept system (component structure) of the entire code you have read. This is not a fast step, but at this step, we must try to understand a number of complicated details. This step is similar to Step 3. Every time you learn more about the context, you can go back and look at it to get a deeper understanding.
5.There is something you may not understand in the first few steps. Now the most suitable thing is to run a few tests and check out the existing tests.. Testing may greatly dispel your doubts and deepen your understanding of the tested code. I often cannot understand why some developers ignore rigorous and sophisticated test suites when reading and understanding code. Of course, sometimes it is because there is no test program.
6.If there is no test program, write a few. This method has many advantages. It will deepen your understanding. It can improve the entire code base and write code while reading the Code. This will benefit both existing code and you, you can also get involved. Even if you have a ready-made test program, you can write more to deepen your understanding. Testing people's Code generally requires a different angle of thinking, and the concepts you are confused about will become clearer.
7.Take the unintelligible part to a separate program.. I found that it is very interesting to read the code, even if it is just to adjust the pace of reading. Even if you do not understand the underlying details of the code, you may have high-level thoughts on the code functions. So why don't we test some functions in a separate program? If you only run a short program, debugging will be much easier, and this process will in turn deepen your understanding.
8.Is the code messy (dirty) very bad (smelly )? Rebuild it.. I am not suggesting that you reinvent the entire code library, but restructuring some parts can indeed bring your understanding to the next level. At the beginning, you need to change the functions you understand into independent functions. Before you really understand it, refactoring the previous functions just seems okay, And Refactoring can turn them into what you want. With refactoring, you can turn your code into your own, rather than rewrite it all. A good test program can be helpful, but if you don't have it, you can test and refactor it, and modify only the functions that you have understood. Even if the test seems too few and too imperfect, you need to trust your own development level. Sometimes you just need to make a bold attempt (if you really need it, you can always roll back (revert ).
9.If none of the above works, find a companion to read the code with you.. You may not be the only one who can benefit from reading the code, so you may wish to catch others and read them together. But don't look for experts. They give you high-level explanations, so that all the details that you pay attention to when looking at the code are ignored. If you can't find anyone to read them together, you can't understand them. Sometimes the best way is to ask others. You can ask your colleagues. If the code you are reading is open-source, you can also ask others online. Remember, this is only the Final Solution. Do not do it in the first place.
If the time is too short and you must understand some code in a short time, you can only select one step in the above steps, and I will choose to refactor (step 1 ). You may not be able to understand much, but you can certainly understand what actually has been done. Whether time is short or not, remember:If you get into the important code library for the first time, you cannot understand it at once, or even quickly understand it.. You may need to read for a few days, weeks, or even months. Even if an expert is sitting by your side, it won't save much time (I wrote the last article on teaching and learning to talk about this question ). If you are patient enough to read (or write) Code and have the proper method, you will surely become very familiar with all aspects of the project, this is also the person who answers questions about the code library. You can also choose to be the one you want to explain without reading the code. I know exactly who I want to be.
Seize the opportunity to read the code. Don't miss it.
We all like writing new code. The temptation is that we can solve the problem. At least, this time cannot be done, but it can be done next time. The truth is:You are constantly improving in practice, and you will never be able to get it done in one minute.. This is the value of new code writing. You are practicing and your level is improving, but reading and modifying the code written by others is also very valuable (at least as much as writing it again ), what you learn from this is not only valuable technical knowledge, but also knowledge about the entire work area (after all, code is the final form of documents). This kind of knowledge is usually more valuable.
Every piece of code that does not follow the traditional and obscure rules is valuable. You know what I'm talking about. Although it looks messy, it doesn't mean so (or probably because it is a Perl code :)). Every time I see such code, I think so. Read it as a guessing game and think about what you can learn. Yes, it's tough, but you have to admit that you also want to write such bad code. If you take the time to read such code, you are certainly more likely to write such code-this is not to say that you must write such code, but you certainly wantYes. Finally, attitude is always very important.If you think of reading code as a complex task, it is a complex task, and you will surely escapeBut if you think of it as an opportunity, things will be different.