Detailed introduction to writing C ++ code that is easy to understand

Source: Internet
Author: User

This section introducesVisual C ++ 6.0Tool development environment and development process,This section describes the good C ++ code. Visual C ++ 6.0 is a C ++ development tool launched by Microsoft. It is the most widely used development tool, next we will learn.

1. Standardized and easy-to-understand code

At this stage, software development relies on the cooperation of teams. Programmers are no longer synonymous with personal heroism. On the one hand, programmers must rely on a large number of other programmers to complete the code, and on the other hand, provide a large amount of code for others to use. The Code actually has two elements: the first is to provide a reliable function, and the second is to clearly express the author's thoughts. Any communication must have certain specifications before it can be carried out. In code, the specifications are easy to understand. In addition, standardized and easy-to-understand code can be reused. Standardized Code has a longer life cycle, better maintainability, and more convenient extension later.

Several features of good code

What kind of code can be regarded as a standard and easy to understand? It is reflected in countless arguments in details. In fact, no matter the style and habits, good code has several common features:

1. Good naming: A good variable name and function name let people who read the code immediately know the role of the variable or function, and it is easy to understand the approximate structure and function of the program. It is necessary for programmers to understand the Hungarian naming law.
2. consistency: consistency leads to better programs. consistent code indent style can display the code structure. It doesn't matter what indent style is used. In fact, specific Code styles are far from consistent.
3. NOTE: Annotations are a way to help program readers, and program authors are also one of the Future Program readers. The best comments are the highlights of the program, or provide an overview to help others understand the program. However, if comments only indicate what the code has already said, or conflict with the code, or confusing and interfering with readers in a well-organized manner helps.

Good habits

As mentioned above, it is important that the specific C ++ code style is far from consistent. Therefore, it is A waste of time to put too much energy into the choice of A or B, what you need to do is persistence. Below are some tips on how to write standard and easy-to-understand code and develop good habits.

1. Name the variables and functions according to the Hungarian naming method.
2. Follow the international popular code style.
3. When writing code, you must follow your naming rules and writing style. You must never remedy the issue afterwards.
4. Use the tool Parasoft C ++ Test) to check your code and evaluate whether you have formed good habits.
5. persevere until you develop habits.

Write secure and reliable code

In large application software systems, code snippets constitute a complete system. Code interactions are frequent, and program crashes do not occur when errors occur, it is a delay of a period of time. It takes a lot of time and effort to locate and search for errors. How can we timely reflect errors in the program, how can we avoid some naive Semantic Errors in code? A function is often used by other programmers, but how can he correctly use functions compiled by others? This part can be partly) help solve these problems.

Contract Programming
The idea of Contract programming Design by Contract is mentioned in C ++ Bible-level works, Bjarne Stroustrup, the father of C ++ code, in C ++ programming language, the Bible-level book object-oriented software architecture in the OO field illustrates contract programming in a large space. More and more software developers recognize the importance of contract programming, and gradually adopt contract programming in actual work.

A simple explanation of contract programming is: Define the contract between the implementer and caller of the function and the caller of the function to implement code block functions and classes by specifying the call condition constraints) and output results.
As for our work, developers should define the contract for each function and class completed. Contract programming seems to be unremarkable and does not have any specific help for program development. In fact, contract programming can ensure the reliability and security of software to the maximum extent in the development stage.

In actual work, whenever you need to use modules provided by other programmers, you do not know how to call them or whether the parameters you pass in are legal, sometimes the processing results of functional modules cannot be believed. This should have been obvious because the module provider does not provide explicit information.

As a result, the caller can only feel the stones and cross the river in an uneasy manner, wasting a lot of time. In order to make his code more secure and reliable, he made a lot of judgments and assumptions in the code, it causes damage to the code structure and execution efficiency. Finally, the caller still cannot ensure that his or her calls are correct.

By strictly specifying functions or classes), contractual programming defines mutual rights and obligations between function providers and callers to avoid the above situations, it ensures the quality of C ++ code and software.

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.