How to read large C + + program code quickly

Source: Internet
Author: User
Tags naming convention

To figure out someone else's code, first of all, you need to understand the code involved in the domain knowledge, this is the most important, do not understand the domain knowledge, only look at the code itself, it is impossible to understand.
Second, you need to find various documents: The requirements document (what to do), the design document (how to do), first understand what you are about to read, at least the overall structure of the code to clear: How the overall architecture, there are several modules, inter-module communication, operating environment, building tools and so on.
Finally, if not all of the code belongs to you, focus on the module that will belong to you, other code to understand the next.

How to read large C + + program code quickly(2012-06-14 11:25:03)
Tags: c programming Category: C-Learning
Six chapters:
(1) Read the program code, so that the heart is used by me.
(2) To understand the structure, you can easily grasp the whole picture.
(3) High-quality tools in hand, read the program is not difficult.
(4) Words too literally the function of the component.
(5) Find the program entrance, and then the upper and lower cobwebs.
(6) Read the fun, through the program code to understand the author.

read other People's Code (1)---Read the code, so that the heart is used by me
The program code is written by others, only the original author really understand the purpose and meaning of the program code. Many people in the program have an unconscious sense of fear and are afraid of being forced to touch code written by others. However, rather than resisting the acceptance of other people's Code, it is better to thoroughly understand the relevant language and practices as a cornerstone of self-empowerment.
Writing code may be a happy thing for most people, but I believe that more people read the code written by others as perilous undertaking. Many people prefer to write their own code again, rather than receiving code from others, correcting bugs, maintaining them, and even enhancing functionality.

Where is the key? If gave away, in fact, is also very simple, the program code is written by others, only the original author really understand the purpose and meaning of the program code. Many people in the program have an unconscious sense of fear and are afraid of being forced to touch code written by others. This is the primitive fear of strangers from the depths of the human heart.

Read the code that someone else has written, so you get a full harvest
However, for many practical reasons, the program person is often forced to receive code from others. For example, a colleague leaves the job, must take over his legacy of work, it is possible that you are just into the department of rookie, and colleagues experience is enough, upgrade, the wind turbine circulation, a generation of rookie change rookie. Even the projects your company undertakes must take over or integrate the systems that the customer has left behind, and you only have the original code for that system (and when you're lucky, there are a number of different files).

Such stories, in fact, often on the side of the program or the body continues to perform. Many program people will take over the code of others as a tragic thing. Everyone doesn't want to take on the code that someone else is writing, because they don't want to spend time exploring, preferring to spend their productivity generating new code instead of learning about the code.

understand the system architecture and behavior patterns first, then read them carefully.
If writing code is one of the important skills of a program person, then it is another important skill to read the code of others and then modify it.

If you are not familiar with this work, not only in the situation you do not want to face, not solve the problem of taking over the other person's code, more importantly, when you look at the existing program code, but do not know how to extract their own needs, resulting in the last only into Baoshan empty-handed back, hope of the sigh.

Access to other people's program code, can be broadly divided into three degrees: one, understand, two, modify, expand, three, extract, refine. Understanding other people's program code is the most basic work, if you do not know the code you want to deal with, do not talk about modification or expansion, more unlikely to chaff, extract their own needs, recycling and other people write code. Although "reading", but the code is not like articles or novels, through this approach, you can get a certain degree of understanding. When you read an article or a novel, you read it almost sequentially, and you only have to turn to the first page and read it in a row. However, there are many people who try to read other people's code, but often do not know how to read the difficulties.

It is not difficult to find the first page of the system (that is, the starting point of the code execution), but a system with high complexity is sometimes very large and sometimes complex.

Starting from the start point of the program code to read, a sequential reading of all the program code is time-consuming, and in this way to understand the system, it is difficult to build a system in the brain, and then understand the real behavior of the system. Therefore, the focus of reading code is not to read each line of code, but to efficiently through exploration and reading, so as to understand the system's architecture and behavior patterns. So that when you need to know the details of any fragment, you can quickly map to the specific code location in your brain, until that moment, the time to read it carefully.

familiar with the language of communication and idioms
In any case, some basic preparation is necessary to read someone else's code.

First of all, you'd better understand the programming language written in the code. If you want to read a novel written in French, you can't even understand French. Some cases are very special. Although we do not understand the code to write the language, but because of the high-level modern language, and the popular programming language is mostly similar pedigree, so even if not so familiar, sometimes can be done.

In addition to knowing the language, it is necessary to first confirm the naming convention (the naming convention) used by the code. It is important to understand the naming conventions, and the differences may be significant for different program people or development teams.
This naming convention covers a range that typically includes the name of the variable, the name of the function, the name of the category (if it is object-oriented), the source code file, or even the name of the project construction directory. If you use methods like design patterns, these names have some specific representations.

Naming conventions are a bit like a set of communication jargon that a program person constructs on top of a programming language. The procedural person will express some higher-order concepts through common constraints and observance of the naming convention. For example, a well-known Hungarian nomenclature combines variable names with attributes, types, and descriptions. For the procedural person, this approach can provide richer information to understand the role and nature of the variable.

It's important to be familiar with code reading, because when you understand the practice of the system as a whole, you can try to understand it in terms of the vocabulary they use to work together. If you do not understand the conventions it uses, then these additional information will not be available to you. Like the code written in design mode, it is also filled with the name of the pattern, such as: Factory, façade, agent and so on. The categories that refer to these names also express their own functions directly through their names. For those who understand this naming convention, they do not need to delve into it and can quickly capture the meaning of these categories.

When you get a set of code that must be read, it's a good idea to get a description of the naming convention first. However, this type of documentation is not attached to each set of code. Another way to do this is to go through the code and browse through it, and an experienced program person can easily discover the naming conventions used by the system.

The common naming method is not to take off those categories, at this time experience is very important, if you know the more the practice, the more easily identify the practice of others. If you have a bad luck, the code is used in the Convention is not seen before, then you have to take some time to summarize, with your own strength to find out the rules of the code naming.

mastering the mentality and habits of program code writers
Most code, basically, follows a consistent naming convention. But when luck is worse, a set of systems may be flooded with multiple naming conventions. This may be because the development team consists of multiple groups of people, each team has a different culture, and in the project development management is not properly controlled. In the worst case, the code has no obvious convention at all, and it's harder to read.

To read the code, try to understand the "heart" of the code author first. If you want to do this, you need to know more about the language used by each other, and the usual vocabulary. In the next step, we will continue to explore topics related to reading code.
Read other people's Code (2)-Understand the structure and easily grasp the whole picture
In this article, we focus on: to understand a system, it is best to take a top-down approach. Try to capture the architecture of the system first, don't get into the details too early, because that's usually not helpful for you to know the whole picture. Reading code does not need to be read from the first line, and our goal is not to read through each piece of code.
For many reasons, a program person needs to read the code written by someone else. The most positive value for programmers in the 2.0-era program is that people who can read other people's programs have the ability to extract the procedures they need to improve productivity.

The purpose of reading the code is to understand the full picture rather than the details
The basic basis for reading the code is to understand the programming language and naming conventions used by the other person. With this foundation, the basic reading ability is considered. As I mentioned before--to read a novel written in French, you can't even understand French. Reading the code and reading the literature requires an understanding of the language used in writing and the vocabulary of the author idiomatic.

But we are usually reading literary works in a sequential way, that is, starting from the first page, a line of reading, follow the author for you to lay down the pace, gradually into the world he prepared for you. Reading the code is much different. We seldom start reading from the first line, because it is rarely done unless it is a simple, single-threaded program. Because if you do, it's hard to get a complete picture of the whole system. Yes, we've got a point here, and the purpose of reading the code is to understand the full picture of the system, not just to read through every piece of code for the carpet.

In the case of a system written in object-oriented programming language, the entire system is dismantled and analyzed into separate categories. Read the code for individual categories, perhaps to understand the individual behavior of each category of objects. But how to interact with each kind of object, how to work together, it is easy to fall into elephant predicament. This is because each kind of code, only describes the behavior of individual objects, and fragments of reading can only create one-sided understanding.

The composition relationship can be easily understood after the structure is clarified from top to bottom
If you want to jump off the hook and don't want to waste a lot of time reading the code, but always capturing only the system fragments, you have to switch to another perspective to see the system. Starting from an individual category of behavior is a bottom-up (bottom-up) approach, while reading the code, you should first take the top-down (top-down) approach. For code reading, from top to bottom, you have to understand the entire system architecture first.

The architecture of the system is the backbone and pillar of the whole system. It shows the most prominent features of the system. Knowing what kind of system architecture belongs to that type usually greatly benefits the static and dynamic relationships between the individual components of the system. Some systems determine the top-level architecture because of the technology or framework used. For example, using the Java servlet/JSP technology application system, the outermost architecture is based on the EE (or at least the Web container in Java ee) as the root.

When using the Java servlet/JSP technology, the relationship between some components is determined. For example, the Web container loads all of the servlets, listeners, and filters based on the contents of XML. Whenever an event occurs in context (such as initialization), it notifies the listener category. Each time it receives a request from the client, it follows the set of all filter chains, giving each filter the opportunity to check and process the request, and then the request to the servlet that is used to process the request.

When we understand that a system uses such an architecture, it is easy to know the relationship between the various components. Even though we don't know how many servlets we have, we will know that whenever a request is received, there will always be a corresponding server to handle it. When you want to focus on how a request is handled, I should find the server that corresponds to the request.

Understanding the architecture, you must add a sense of hierarchy
Similarly, in Java-written web applications, an MVC framework such as struts might be applied, as well as a data access framework like hibernate. They can all be seen as sub-architectures under the most important architectures. And each application system may even be under struts and hibernate to build its own more subordinate architecture.

How to read large C + + program code quickly

Related Article

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.