How to improve your programming skills by reading someone else's code

Source: Internet
Author: User

The necessity of code reading
reading someone else's code as a researcher is a regular thing to do. One is to learn a new programming language by reading someone else's code is the best way to learn, plus the accumulation of programming experience. If you have the opportunity to read some of the operating system code will help you understand some basic principles. And even when you're a quality assurance officer or a small leader, if you're going to do a white-box test without the ability to read the code, you can't complete the task. The last one is to have the ability to read code if you take a project halfway or when you give a project a post-sales service.

Collect all materials that may be collected
The first thing to do when reading the code is to collect all the information related to the project. For example, you want to do a project after-sales service, then you have to understand what the project to do, then the research documents, summary design documents, detailed design documents, test documents, manuals are the first you have to get it. If you are studying so as much as possible to collect information about your studies, such as the code of your Linux file system, the best way to find the Linux user's manual, and file system design methods, data structure description. (This information can be found in the bookstore).
The type of material is divided into several types
1. Basic information.
For example, if you read the original code of the Turbo C2 you have to have a function manual for the Turbo C2, a professional book such as a manual, or a library function manual for MSC 6.0 or Java. This information is the basis of your information. In addition, you need to have some information about UML as a query manual is also a good choice
2. Professional information related to the program.
Each program is related to the relevant industry. For example, I read a code about meteorological analysis, because it used a complex data conversion formula, so I had to find their college textbooks to review the content of advanced mathematics. If you want to read the Linux file Management code, then finding a book that explains the Linux file system will help you a lot.
3. Documentation of related projects
This part of the data is divided into two, a related industry information, such as you want to read a tax system code then there are some financial/tax system of professional information and the relevant laws and regulations of the country are essential. In addition to this project needs analysis reports, summary design reports, detailed design reports, manuals, test reports, and so on, to collect as much as possible for your future code reading is very important

Knowledge Preparation
Understand the basics, don't come up to read the code, play a good foundation can do more with less effect

Backup, construct a running environment
The first thing that comes after the code gets to the hand is to make a backup, which is best engraved on a CD, and it's hard to do a little bit of code when you're reading it, especially if you want to do some modification or enhanced maintenance. And once the changes can occur, the time to recover is a frequent occurrence, if you are not very good use of version control software then the first backup is a minimum requirement.
The best place to build a running environment after the backup is done can be cumbersome, but running code and non-running code can be very difficult to read. So it is worthwhile to spend a little time building an environment, and we read the code primarily to modify the problem or to do the porting. Code that cannot be run has limited usefulness in addition to learning some techniques.

Find the place to start
Do everything you need to know from there, reading the program is no exception. In the C language, first to find the main () function, and then read by layer, the other programs, whether VB, Delphi must first find the program header, otherwise you are very difficult to analyze the hierarchical relationship of clear programs.

Hierarchical reading
do not stick at the end when reading code, so it is often easy to trees trees, reading the code is a bit like the breadth-first traversal of a binary tree. In the program body is generally relatively simple, call the function will be less, according to the function of the name and hierarchical relationship generally can determine the approximate purpose of each function, your understanding as an annotation written on the edge of these functions. Of course it's very difficult to write all the annotations correctly at once, and sometimes it may even be the result of your guesses, but it doesn't matter that the annotations are constantly revised in the reading process until you understand the code. Generally speaking, the method of reading by layer is to keep the understanding of your system in the right direction. Avoid a sudden plunge into the details of the problem. When reading at a hierarchical level, it is important to note that the functions of the system and the developers are coded separately. In C, C++,java, Delphi has its 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, annotated their role, there are several ways to differentiate between system functions and self-coding functions, one is the system function of the programming style is generally better, and the self-compiled function of the programming style is generally relatively poor. From the variable name, the indentation between lines, annotations and other aspects can generally be distinguished out, the other is like Ms C6++ will be in your programming when you generate a lot of files out, which has a lot of 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 research and development system of the Help system to check the function name, the parameters and so on to determine.

Write annotations
Write annotations is one of the most important steps in reading code, in the original code we read is generally unfamiliar with the system, reading other people's code will generally have a few questions, 1 understand other people's programming ideas do not is a very easy thing to do, even if you know the idea of the program is the same. 2 reading code when the code is generally relatively large, if not in time to write annotations will often result in reading clear behind the phenomenon of forgetting the front. 3 reading the code will inevitably appear to understand the error, if not timely write annotations very difficult to find these errors in time. 4 Do not write annotations Sometimes you happen to be very difficult to determine a function when you read, what his function is, often repeated reading, understanding of the phenomenon.
OK, say some basic methods of writing annotations: 1 guess to write, just start reading a code, you very difficult to determine all the functions of the function, it is advisable to use a guessing method to write annotations, according to the function name, position to write a general comment, of course, there will be errors, But your annotations are actually not only adjusted until you finally understand the entire code. 2 according to the function to write, do not put the annotations written in grammar, do not see fopen to write open files, see Fread write data, such annotations are not useful, but should be written here to develop parameter settings file (* * * *. DAT) read out the system initialization parameters ..... , this is a useful annotation. 3 in the use of write annotations in addition, one of the problems is to clear the system automatically generated code and the user's own development of code, in general, there is no need to write the system automatically generated code. Like Delphi code, we often have to write some of our own code snippets, but also some of the system automatically generated code snippets, which are written in the reading process is to write annotations, but there are some non-modified automatically generated code there is no need to write annotations. 4 Write more detailed annotations in the main code section. There are functions or classes that play a key role in the program, so write more elaborate annotations. This helps you understand the code very much. 5 It is difficult for you to understand the place to write detailed annotations, where there are often some programming skills. Don't understand how these programming skills will be problematic for your future understanding or transplant. 6 Write Chinese notes. If your English is good enough, do not look at this, but very many people in English is not much, then write Chinese notes, we write annotations to speed up their understanding. Chinese is more suitable for Chinese than English in most times. And it is better not to write some English annotations who can not read.

Repeat Reading
There is no one who can read all the code at once. At least I haven't met yet. Repeated reading of the same piece of code helps to understand the code. In general, when you first read the code, you can skip very many pieces of code that you do not understand, write some simple annotations, in the subsequent repetition of the reading process, you will understand the code more deeply than the last time, so that you can modify those comments wrong place and the last one did not understand the other side. Generally speaking, the code reading 3, 4 times basic understanding of the meaning and role of the code.

Run and modify the code
If your code is operational, it will improve your code speed by letting him run and reading the code in a single-step tracking way. The code understands the meaning of the code by looking at the intermediate variables, and it can be very helpful for future modifications.
Use your own code instead of the original code to see the effect, but before you keep the original code
A function of 600 lines, it is very difficult to read, programming people are not a good habit. When reading this code, the code is modified to 14 functions. Each one is about 40-50 rows or so.

How to improve your programming skills by reading someone else's code

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.