Necessity of code reading
Reading other people's code as a developer is a common task. One is reading others' code when learning a new programming language is the best learning method, and the other is accumulating programming experience. If you have the opportunity to read some operating system code, it will help you understand some basic principles. What's more, when you are a Quality Assurance personnel or a small leader, if you do not have the ability to read the code during a white box test, you cannot complete the corresponding tasks. The last one is that if you take over a project halfway or provide after-sales services for a project, you must be able to read the code.
Collect all possible Materials
The first thing to do when reading the code is to collect all project-related information. For example, if you want to provide after-sales services for a project, you must first understand what the project is, you must first obtain the survey documents, outline design documents, detailed design documents, test documents, and user manuals. If you want to learn, try to collect information related to your learning. For example, if you want to learn the code of the linux File System, it is best to find the linux User Manual, the file system design method, and the description of the data structure. (These materials can be found in the bookstore ).
There are several types of materials
1. Basic information.
For example, if you read the original code of turbo c2, you need to have a function manual of turbo c2, user manual, and other professional books. For msc 6.0 or java, you need not only a function manual, but also a class library function manual. These materials are your basic materials. In addition, it is a good choice for you to have some information about uml as a query manual.
2. Professional Program-related documents.
Every program is related to the relevant industry. For example, I have read a code about meteorological analysis, because a complicated data conversion formula is used in it, so I have to find out my college textbooks to review the contents of advanced mathematics. If you want to read the file management code of linux, It is very helpful to find a book to explain the file system of linux.
3. Documents of relevant projects
There are two types of materials in this section, for example, if you want to read the code of a tax system, it is essential to have some professional information about the financial/tax system and relevant national laws and regulations. In addition, it is the requirement analysis report, outline design report, detailed design report, user manual, test report, etc. It is very important to collect as much as possible for your future code reading.
Knowledge preparation
Understand the basic knowledge, read the code without coming up, and lay the foundation to get twice the result with half the effort.
Keep backups to build a running environment
The first thing after getting the code is to back up the data first. The best thing is to engrave it on a CD. It is very difficult to read the code without moving it, especially when you want to do some modification or enhanced maintenance. Once the changes are made, problems may occur, and recovery will often happen. If you cannot use the version control software very well, leaving a backup is the minimum requirement.
After the backup is complete, it is best to construct a runable environment for yourself. Of course, it may be very troublesome, but it is much difficult to read the runable code and the unrunable code. Therefore, it is worthwhile to use a little time to build an environment, and we read the code mainly to modify the problems or perform porting operations. Code that cannot be run is of limited use in addition to learning some technologies.
Locate the start point
You must know where to start and read the program. In C language, you must first find the main () function and then read it layer by layer. Other programs, both vb and delphi, must first find the program header, otherwise, it is very difficult for you to clearly analyze the program hierarchy.
Hierarchical reading
When reading the code, do not stick it all at once. It is easy to see that the trees are not in the forest. A better way to read the code is to traverse the breadth of Binary Trees first. Generally, the Program subject is relatively simple and calls fewer functions. The general purpose of each function can be determined based on the function name and hierarchy, write your understanding on the edge of these functions as an annotation. Of course, it is very difficult to write all the annotations correctly at a time. Sometimes it may even be the result of your guess, but it doesn't matter whether these annotations are constantly corrected during the reading process, until you fully understand the code. Generally, the layer-by-layer reading method keeps your understanding in the correct direction. Avoid sticking to details all at once. When reading data in different levels, you must note that the system functions are separated from the code written by R & D personnel. C ++, java, and delphi all have their own system functions. Do not read these system functions unless you want to learn their programming methods, otherwise, it will only waste your time. Express system functions and indicate their functions. There are several methods to distinguish between system functions and self-compiled functions. One is that the programming style of system functions is generally better, the programming style of Self-compiled functions is generally relatively poor. It can be distinguished from the variable name, indentation between rows, and note solution. Another one is that ms c6 ++ will generate a large number of files for you during programming, there are a lot of files that you cannot use. You can identify the system function by file name. If you are not sure, then, use the help system of the R & D system to check the function name and determine the parameters.
Write Annotation
Writing annotations is the most important step in reading code. The original code we read is generally a system we are not familiar with. reading other people's Code usually has several problems, 1. It is not very easy to understand others' programming ideas, even if you know the ideas of this program. 2. When reading the code, the amount of code is usually relatively large. If you do not write the annotation in time, you will often find that you have forgotten the front and back. 3. Comprehension errors are inevitable when you read the code. If you do not write annotations in time, it is very difficult to find these errors in time. 4. If you do not write an annotation, sometimes it is very difficult for you to determine a function that you have read and understand.
Well, let's talk about some basic methods for writing Annotations: 1. When I started to read a code, it was very difficult for you to determine all the functions of the function at once, you may wish to use the method of speculation to write an annotation, and write a rough annotation Based on the name and position of the function. Of course, there will be errors, but your annotation is actually not only adjusted, until you finally understand all the code. 2. Write by function. Do not write the annotation into a syntax description book. Do not open the file when you see fopen, and write and read data when you see fread. Such annotation is useless at all, the R & D parameter setting file (***) should be written here (****. Dat) read the system initialization parameters..., which is a useful annotation. 3. When writing annotations, note that the system automatically generates the code and the code developed by the user. Generally, there is no need to write the code automatically generated by the system. Like delphi code, we often need to write some of our own code segments and modify some automatically generated code segments. These codes need to be annotated during reading, however, some automatically generated codes that have not been modified do not need to be annotated. 4 In the main code segment to write more detailed notes. Some functions or classes play a key role in the program, so you need to write more detailed annotations. This helps you understand the code. 5. You need to write detailed annotations for areas that are difficult to understand. There are usually some programming skills in these areas. If you don't understand these programming skills, it will be difficult for you to understand or transplant them later. 6. write Chinese annotations. If your English is good enough and you don't need to read this article, but many people are not very good at English, write Chinese annotations, we write annotations to increase our understanding speed. In most cases, Chinese is more suitable for Chinese than English. It is better not to write English annotations that no one can understand.
Repeated reading
No one can read all the code at a time. At least I have never met. Reading the same piece of code repeatedly helps you understand the code. In general, when you read the code for the first time, you can skip a lot of code segments that you don't understand for a while. You can only write some simple annotations, which will be used in subsequent repeated reading processes, your understanding of the Code is more profound than the previous one, so that you can modify the comments that are wrong and those that were not understood last time. Generally, you can read the code three or four times to understand the meaning and function of the Code.
Run and modify the code
If your code is runable, let it run first, and read the code in a single step tracking method, which will speed up your code. The Code understands the meaning of the Code by looking at the intermediate variables, and will be very helpful for future changes.
Use your own code to replace the original code to see the effect, but keep the original code before.
It is very difficult to read a 600-row function. Programmers are not a good habit. When I read this code, I changed the code to 14 functions. Each row is about 40-50 rows.