How to read someone else's code

The Necessity of Code Reading
    Reading other people's code is a regular thing to do as a developer. One is that reading other people's code is the best way to learn when learning a new programming language, and the other is to accumulate programming experience. If you have the opportunity to read some operating system  code will help you understand some basic principles. There is also the fact that when you are a quality assurance person or a small leader, if you do not have the ability to read code when you are doing white box testing, you cannot complete the corresponding task. The last one is if you take over a project halfway or do after-sales service for a project, you must have the ability to read code.

    Gather all possible material
    The first thing to do when reading the code is to gather all the material related to the project. For example, if you want to do after-sales service of a project, then you must first understand what the project is for, then research documents, general design documents, detailed design documents, test documents, and user manuals are the first things you need to get. If you want to learn, try to collect information related to your study. For example, if you want to learn the code of the linux file system, it is best to find the linux manual, as well as the description of the file system design method and data structure. (These materials can be found in bookstores).

    The types of materials are divided into several types
    1 basic materials.
    For example, if you read the source code of turbo c2, you must have turbo c2 function manuals, manuals and other professional books, msc 6.0 or java, not only function manuals, but also class library function manuals. These data are your basic data. In addition, you must have some information about uml, which can be used as a query manual, which is also a good choice
    . 2. Professional information related to the program.
    Each program is relevant 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 out my college textbook to review the content of advanced mathematics. If you want to read linux's file management code, then finding a book that explains the linux file system will help you a lot.
    3 Documents of related projects The information in
    this part is divided into two types. One is related to the industry. For example, if you want to read the code of a tax system, then there are some professional information of the financial/tax system and the relevant laws and regulations of the country. is essential. In addition, it is about the requirements analysis report, general design report, detailed design report, user manual, test report, etc. of this project. It is very important to collect as much as possible for your future code reading.

    Knowledge is ready to
    understand the basic knowledge, just read the code without coming up, laying a good foundation can achieve multiplier effect with half the effort

    Keep a backup, construct a runnable environment
    The first thing after getting the code is to make a backup first, preferably engraved on a CD. It is very difficult to move the code when reading the code, especially When you do some modification or enhancement maintenance. Once a modification is made, problems may occur. It is a common thing to restore at that time. If you cannot use version control software well,  it is a minimum requirement to keep a backup first.
    It is best to construct a runnable environment for yourself after making a backup. Of course, it may be very troublesome, but the runnable code and the non-runnable code will be much less difficult to read. So it's worth spending a little more time setting up an environment, and we read the code mainly to fix problems or do porting operations. Code that doesn't work has limited usefulness other than to learn some techniques.

    Find a place to start
    everything you need to know where to start, and reading programs is no exception. In the C language, you must first find the main() function, and then read it layer by layer. For other programs, whether it is vb or delphi, you must first find the program header, otherwise it will be difficult for you to analyze the hierarchical relationship of the program.


    分层次阅读
    在阅读代码的时候不要一头就扎下去,这样往往容易只见树木不见森林,阅读代码比较好的方法有一点象二叉树的广度优先的遍历。在程序主体一般会比较简单,调 用的函数会比较少,根据函数的名字以及层次关系一般可以确定每一个函数的大致用途,将你的理解作为注解写在这些函数的边上。当然很难一次就将全部注解都写 正确,有时候甚至可能是你猜测的结果,不过没有关系这些注解在阅读过程是不断修正的,直到你全部理解了代码为止。一般来说采用逐层阅读的方法可以是你系统 的理解保持在一个正确的方向上。避免一下子扎入到细节的问题上。在分层次阅读的时候要注意一个问题,就是将系统的函数和开发人员编写代码区分开。在c, c++,java ,delphi中都有自己的系统函数,不要去阅读这些系统函数,除非你要学习他们的编程方法,否则只会浪费你的时间。将系统函数表示出来,注明它们的作用 即可,区分系统函数和自编函数有几个方法,一个是系统函数的编程风格一般会比较好,而自编的函数的编程风格一般比较会比较差。从变量名、行之间的缩进、注 解等方面一般可以分辨出来,另外一个是象ms c6++会在你编程的时候给你生成一大堆文件出来,其中有很多文件是你用不到了,可以根据文件名来区分一下时候是系统函数,最后如果你实在确定不了,那就 用开发系统的帮助系统去查一下函数名,对一下参数等来确定即可。
    写注解
    写注解是在阅读代码中最重要的一个步骤,在我们阅读的源代码一般来说是我们不熟悉的系统,阅读别人的代码一般会有几个问题,1搞明白别人的编程思想不是一 件很容易的事情,即使你知道这段程序的思路的时候也是一样。2阅读代码的时候代码量一般会比较大,如果不及时写注解往往会造成读明白了后边忘了前边的现 象。3阅读代码的时候难免会出现理解错误,如果没有及时的写注解很难及时的发现这些错误。4不写注解有时候你发生你很难确定一个函数你时候阅读过,它的功 能是什么,经常会发生重复阅读、理解的现象。
    好了,说一些写注解的基本方法:1猜测的去写,刚开始阅读一个代码的时候,你很难一下子就确定所有的函数的功能,不妨采用采用猜测的方法去写注解,根据函 数的名字、位置写一个大致的注解,当然一般会有错误,但你的注解实际是不但调整的,直到最后你理解了全部代码。2按功能去写,别把注解写成语法说明书,千 万别看到fopen就写打开文件,看到fread就写读数据,这样的注解一点用处都没有,而应该写在此处开发参数配置文件(****。dat)读出系统初 始化参数。。。。。,这样才是有用的注解。3在写注解的使用另外要注意的一个问题是分清楚系统自动生成的代码和用户自己开发的代码,一般来说没有必要写系 统自动生成的代码。象delphi的代码,我们往往要自己编写一些自己的代码段,还要对一些系统自动生成的代码段进行修改,这些代码在阅读过程是要写注解 的,但有一些没有修改过的自动生成的代码就没有必要写注解了。4在主要代码段要写较为详细的注解。有一些函数或类在程序中起关键的作用,那么要写比较详细 的注解。这样对你理解代码有很大的帮助。5对你理解起来比较困难的地方要写详细的注解,在这些地方往往会有一些编程的技巧。不理解这些编程技巧对你以后的 理解或移植会有问题。6写中文注解。如果你的英文足够的好,不用看这条了,但很多的人英文实在不怎么样,那就写中文注解吧,我们写注解是为了加快自己的理 解速度。中文在大多数的时候比英文更适应中国人。与其写一些谁也看不懂的英文注解还不如不写。
    重复阅读
    There is no one who can read all the code at once. At least I haven't come across it yet. Reading the same piece of code over and over again helps in understanding the code. Generally speaking, when you read the code for the first time, you can skip a lot of code segments that you don't understand at the moment, and only write some simple comments, which will be used in the repeated reading process in the future. Your understanding of the code will be better than that of the last time. Go deeper, so that you can correct those annotation errors and those you didn't understand last time. Generally speaking, reading the code 3 or 4 times can basically understand the meaning and function of the code.
    Run and modify the code
    If your code is runnable, then let it run first, and use the single-step method to read the code to improve the speed of your code. The code understands the meaning of the code by looking at the intermediate variables, and it will provide great help for future modifications

    Replace the original code with your own code and see the effect, but before you have to keep
    a function of 600 lines of the source code, it is very difficult to read, and it is not a good habit for programmers. When reading this code, the code is modified and turned into 14 functions. Each one is about 40-50 lines or so.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326103219&siteId=291194637