Program code reading skills

Source: Internet
Author: User

I. 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.

2. 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, I have to find out my college textbooks and 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.

Iii. Knowledge preparation

Understand the basic knowledge, do not read the code, and lay the foundation to get twice the result with half the effort.

4. 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.

5. Find 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.

6. 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.

7. 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 you read the code, the amount of code is generally relatively large. If you do not write the annotation in time, it will often cause the reading to understand and forget the front.
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. Sometimes it is difficult to determine whether or not you have read a function without writing an annotation. What is its function? reading and understanding often occurs.

Let's talk about some basic methods for writing Annotations:

1. write with speculation. When you first read a code, it is very difficult for you to determine the functions of all functions at once. You may wish to use the method of speculation to write annotations, you can write a rough annotation Based on the function name and position. Of course, there are common errors, but your annotation is actually constantly adjusted until you finally understand all the code.
2. write by function, do not write the annotation into the syntax specification, do not see fopen write open file, see fread write read data, such annotation is useless at all, it should be written here in the R & D parameter setting file (****. dat) read system initialization parameters ...... This is a useful annotation.
3. When writing annotations, note that the system automatically generates code and the code developed by the user are clearly defined. 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. Write more detailed notes in the main code segment. There are some functions or classes that play a key role in the program, so you need to write a more detailed annotation. This helps you understand the code.
5. 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.

8. Repeated reading

No one can read all the code at a time. At least I have never met anyone. 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. In general, read the code 3 ~ The meaning and function of the code can be basically understood four times.

9. 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.
The reading of a 600-row function is very difficult. This is a bad habit of programmers. When I read this code, I changed the code to 14 functions. Each row is about 40-50 rows.

An Effective Method for Analyzing a source code is:

1. Read the source code instruction documents, such as readme in this example. The author has carefully read the instructions, when reading a program, you can find the corresponding instructions in the README file, which simplifies the reading of the source program.

2. If the source code contains a document directory, which is generally Doc or docs, you 'd better read it carefully before reading the source code, because these documents also play a very good role in commenting.

3. Start with the MAKEFILE file and analyze the source code hierarchy to find out which is the main program and which is the function package. This helps you quickly grasp the program structure.

4. Start with the main function and read it step by step. skip this step if you encounter a simple function that can be guessed. However, you must pay attention to the global variables used in the Program (if it is a C program). You can copy the key data structure description to a text editor for searching at any time.

5. Analyze the function package (for C Programs). Pay attention to the global functions, internal functions, and extern keywords. Pay the same attention to variables. Analyze internal functions first, and then analyze external functions, because internal functions must be called in external functions.

6. It should be noted that the importance of the data structure: For a C program, all functions operate on the same data, because there is no good encapsulation, the data may appear anywhere in the program and be modified by any function. Therefore, pay attention to the definition and meaning of the data and the functions that are operating on the data, what changes have been made.

7. while reading the program, it is best to store the program into version controllers such as CVS. You can perform some modification tests on the source code as needed, because manual modification is a much better way to read programs than just reading programs. When you modify and run the program, you can call out the original code from CVS to compare it with the modified part (diff command ), you can see the advantages and disadvantages of some source code and be able to practice your own programming technology.

8. When reading the program, pay attention to the use of some gadgets to improve the speed, such as the search function in VI, pattern matching search, marking, and grep, find is the most powerful and commonly used text search tool.

For a program running in the command line mode under Unix/Linux, there are some routines that you can use as a reference when reading the program.

1. At the beginning of a program, the command line is often analyzed. Some variables, arrays, or structures are assigned values according to the command line parameters. The subsequent programs perform different operations based on these variables.

2. After analyzing the command line, prepare the data, usually clearing the counter and clearing the structure.

3. There are some pre-compilation options in the program. You can find the corresponding part in makefile.

4. Pay attention to the log processing in the program and the actions performed when debugging options are enabled, which is of great help to the debugging program.

5. Pay attention to multi-thread data operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.