Read other people's code (see others write, feel good, share)

Source: Internet
Author: User

The need for code reading

Reading other people's code as a developer is a constant thing to do. One is learning a new programming language when reading someone else's code is one of the best ways to learn, and the other is to accumulate programming experience. If you have the opportunity to read some of the operating system code will help you understand some basic principles. And when you are a quality assurance officer or a small leader, if you want to do a white box test without the ability to read the code can not complete the corresponding task. The last one is to have the ability to read the code if you take over a project halfway or when you give a project a service.

Collect all the materials that may be collected

The first thing to do when reading code is to collect all the information related to the project. For example, you have to do a project after-sales service, then you first have to understand what the project to use, then research documents, Profile design documents, detailed design documents, test documents, use manuals are the first to get your hands. If you are trying to learn as much as possible to collect information related to your study, such as the code that you want to learn about Linux's file system, it is best to find the Linux manual, as well as the method of file system design, data structure description. (This information can be found in the bookstore).

The types of materials are divided into several categories

1. Basic information.

For example, you read the source code for Turbo C2. You need to have Turbo C2 's function manual, manual, and other professional books, MSC 6.0 or Java, not only have a function manual, but also have a library function manual. All of this information is your basic information. In addition, you need to have some information about UML can be used as a query manual is also a good choice

2. Professional information related to the procedure.

Each program is related to the relevant industry. For example, I read a code on meteorological analysis, because it used a complex data conversion formula, so I had to find my college textbooks to review the content of higher mathematics. If you want to read Linux's file management code, finding a book that explains the Linux file system will help you a lot.

3. Documentation of related projects

This part of the information is divided into two types, a related industry information, such as you want to read a tax system code so there are some financial/tax system of professional information and the country's relevant laws, regulations and information is essential. In addition, this project needs analysis report, summary design report, detailed design report, use manual, test report and so on, collect as much as possible to your later code reading is very important

Knowledge Preparation

Understand the basic knowledge, do not come up on the reading code, lay a good foundation can do less than the effect

Backup to build a running environment

The first thing to do after the code is to do the backup, preferably engraved on a CD-ROM, when the code reading a little bit of the code is very difficult to do, especially if you want to do some modification or enhance the maintenance of the time. And once the change may have a problem, then to restore is often happened, if you can not use the version control software so that the first to leave a backup is a minimum requirement.

It's a good idea to build a running environment for yourself after a backup, which can be cumbersome, but running code and running code can be a lot harder to read. So it is worthwhile to spend a little more time building an environment, and we read the code primarily to modify the problem or do the porting. Code that cannot be run is of limited use, in addition to learning some techniques.

Find the place to start

Do everything you need to know where to start, read the program is no exception. In C, the first to find the main () function, and then read through the layer, other programs, whether VB, Delphi must first find the program head, otherwise you are very difficult to analyze the hierarchical relationship of the program.

Reading at different levels

In reading the code when not to go down, it is often easy to trees trees, reading code is a good way to have a bit like a binary tree breadth first traversal. In general, the program body will be relatively simple, call the function will be less, according to the function name and hierarchical relationship can generally determine the approximate use of each function, your understanding as a note written on the edge of these functions. Of course it's hard to write all the annotations correctly at once, sometimes even if you guessed it, but it doesn't matter. These annotations are constantly being revised in the reading process until you fully understand the code. Generally speaking, the method of reading by layer can be the understanding of your system in the right direction. Avoid a sudden plunge into the details of the problem. One thing to note when reading at a layered level is to separate the system's functions from the developer code area. In C, C++,java, Delphi 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. The system functions are expressed, indicating their role can be differentiated system functions and functions have several methods, one is the system function of the programming style will generally be better, and the function of the custom programming style is generally relatively poor. From the variable name, the indentation between lines, annotations and so on can generally be distinguished, the other is like Ms C6++ will be in your programming when you generate a lot of files out of which there are many files you can not use, according to the file name to distinguish the time is a system function, and finally if you are not sure, Then use the development system to help the system to check the function name, the parameters, etc. to determine.

Write notes

Writing annotations is one of the most important steps in reading code, where the source code we are reading is generally unfamiliar to the system, there are a few problems in reading other people's codes,

1 It's not easy to figure out what someone else's programming idea is, even if you know the program's thinking.

2 reading code when the amount of code will generally be relatively large, if not in time to write notes will often result in reading understand the phenomenon behind the forgotten.

3 reading the code will inevitably appear to understand errors, if not timely write notes it is difficult to find these errors in a timely manner. 4 Do not write annotations Sometimes it's hard to determine a function you have read, what its function is, often occurs repeated reading, understanding phenomenon.

Well, say some basic ways to write notes: 1 guesses to write, just start reading a code, you can not easily determine all the functions of the function, you may use a guessing method to write notes, according to the function name, location to write a general comment, of course, there will be errors, but your annotation is not only the adjustment of the actual Until you finally understand all the code. 2 according to the function to write, do not put notes into the grammar instructions, do not see fopen on the write open file, see Fread to write the data, such annotations are not useful, but should be written here to develop parameter configuration file (* * *. DAT) read out system initialization parameters ..... , this is a useful annotation. 3 in the use of write annotations Another important point to note is that the system automatically generated code and the user's own development of the code, generally do not need to write the system automatically generated code. Like Delphi code, we often have to write some of their own code snippets, but also to some of the system automatically generated code snippet to modify, the code in the reading process is to write annotations, but there are some not modified automatically generated code does not need to write annotations. 4 in the main code snippet to write a more detailed annotation. There are some functions or classes that play a key role in the program, so write more detailed annotations. This is a great way to help you understand the code. 5 more difficult to understand where you have to write detailed annotations, where there are often some programming skills. Not understanding these programming techniques will be problematic for your future understanding or porting. 6 Write Chinese annotation. If your English is good enough, do not need to see this, but a lot of people in English is not how, then write the Chinese annotation bar, we write notes is to speed up their understanding. Chinese is more suitable for Chinese than English in most times. It's better not to write an English annotation than someone who can't read it.

Repeat Reading

One time you can read all the code to understand that the person is not. At least I haven't met yet. Repeatedly read the same piece of code to help get the code understanding. In general, you can skip a lot of code snippets that you don't understand for the first time you read the code, just write some simple annotations and use them in the next reading process, and your understanding of the code will be more profound than the last one, so you can modify the places where the annotations are wrong and the one you didn't understand last time. In general, read the code 3, 4 times can basically understand the meaning and role of the code.

Run and modify code

If your code is operational, just let it run and read the code in a step-by-step way to increase your code speed. The code understands the meaning of the code by looking at the intermediate variables and provides a great help for future modifications

Replace the original code with your own code, see the effect, but before you want to keep the source code

600 lines of a function, it is difficult to read, programming people is not a good habit. When you read this code, you change the code into 14 functions. Each one is about 40-50 lines or so.

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.