C + + Coding specification (excerpt)

Source: Internet
Author: User

In the same project group, it should be specified that the validity of the parameters of the interface function should be checked by the caller of the function or by the interface function itself, which is the default by the function caller. The size of the function is limited to 200 lines. Description: Comments and blank lines are not included. A function accomplishes only one function. Functions should be predictable, that is, the same output should be produced as long as the input data is the same. Check the validity of all parameter inputs for the function. Check the validity of all non-parametric inputs of the function, such as data files, public variables, and so on. Description: There are two main types of input functions: One is parameter input, the other is global variable, data file input, non-parameter input. The function should perform the necessary checks before using the input. The function name should accurately describe the function's function. 6-30: When a procedure (function) has more references to a longer variable (usually a member of a struct), you can replace it with a macro that is quite meaningful.      Description: This can increase programming efficiency and readability of the program.      Example: More references to thereceivebuffer[firstsocket].bydataptr in a procedure      can be replaced by the following macro definition:         # define Psockdata Thereceivebuffer[firstscoket].bydataptr
When you define an expression with a macro, you use complete parentheses. #define Rectangle_area (A, B) ((a) * (b)) put multiple expressions defined by a macro in curly braces.
Use the commissioning switch to switch between the debug and official versions of the software, instead of having a different source file for both the official and debug versions, to reduce the difficulty of maintenance. The debug version and release version of the software should be uniformly maintained, not allowed to be separated, and always pay attention to ensure that the two versions in the implementation of functional consistency. Before writing the code, we should design the method and means of debugging and testing the program, and design various switch and corresponding test code such as printing function. In the premise of guaranteeing the correctness, stability, readability and testability of software system, the efficiency of code is improved. Through the division of the system data structure and organizational improvements, as well as the optimization of the program algorithm to improve space efficiency. Note: This approach is the fundamental solution to the efficiency of software space. This paper analyzes and optimizes the function in the module, and improves the structure of the function in the module, and improves the efficiency of the program. Description: The efficiency of software system is mainly related to algorithm, task mode, system function and function structure. Code Quality Assurance Priority principle (1) correctness, refers to the program to achieve the design requirements of the function. (2) Stability, security, refers to the procedure is stable, reliable, safe. (3) testability, refers to the procedure to have a good testability. (4) specification/readability, refers to the procedural writing style, naming rules, etc. to conform to the norms. (5) Global efficiency refers to the overall efficiency of a software system. (6) Local efficiency refers to the efficiency of a module/sub-module/function itself. (7) Personal expression/personal convenience, refers to personal programming habits.
Reference only the storage space that belongs to itself. Note: If the module package is good, then the illegal reference to other people's space generally does not occur. Prevents references to memory space that has been freed. The memory allocated in the procedure/function to be released before the procedure/function exits. The file handle (used for opening the file) requested in the procedure/function to be closed before the procedure/function exits. Prevents memory operations from overstepping. Description: The memory operation mainly refers to the operation of an array, a pointer, a memory address, and so on.
It is strictly forbidden to change the settings and configuration of other modules or systems arbitrarily. Description: When programming, you cannot change your own module as usual, the size of the array, and so on. You cannot arbitrarily change the interface to other modules. Under UNIX, a multithreaded child thread exit must take the form of an active exit, that is, the subnet thread executes return exit. Do not misuse the goto statement. Note: A goto statement destroys the structure of a program, so it is best not to use a goto statement unless you really need it.
Use the recommended standard statements to improve the portability and reusability of software without using statements that are very relational to the hardware or the operating system.
Carefully construct and delimit the molecular module, and organize sub-modules reasonably by the "interface" and "kernel" parts to improve the portability and reusability of the "kernel" part. Always pay attention to whether the expression will overflow or overflow. When using variables, be aware of their boundary values. To provide users with a good interface interface, so that users can more fully understand the system's internal operating conditions and related system errors. The system should have some fault-tolerant ability, and can be automatically remedied for some error events (such as user misoperation). : When using a software development kit or control provided by a third party, be aware of the following: (1) fully understand the application interface, the use of the environment and the use of considerations. (2) It is not too much to believe its correctness. (3) Do not use unfamiliar third-party toolkits and controls unless necessary.
When writing code, be careful to save it at any time and back up regularly to prevent the loss of code due to power outages, hard disk damage, etc. With the Product Software (project group), it is best to use the same editor and use the same setup options. 1⁄210-6: Check the source program using the Code checker tool (such as C language with Pc-lint). 1⁄210-7: Use software tools (such as Logiscope) for code review. Unit tests begin by tracking each statement and observing changes in data flow and variables. 11-3: Clean, tidy or optimized code should be reviewed and tested. 11-4: Code version upgrades are subject to rigorous testing. 11-5: Use the tool software to maintain the code version. 11-6: Any changes to the software on the official version should be documented in detail. 1⁄211-1: Found errors are immediately modified and are to be recorded. Design and analyze test cases carefully so that test cases cover as many cases as possible to improve the efficiency of test cases. 1⁄211-4: As far as possible to simulate a variety of errors in the program, error handling code to fully test. 1⁄211-5: Carefully test the code to manipulate the data and the bounds of the variables. Persist in the coding phase of the code for thorough unit testing, do not wait for future testing work to identify problems.


A, the types of data, especially the type of int are different lengths under different bits of the machine's platform. B, in order to ensure the universality of the platform, the program should try not to use the Long data type. You can use a fixed-size data type macro definition. typedef unsigned char int8_t; do not assume that int and pointers are the same length. Do not assume that int and long are the same length. You can use only one system to develop programs for multiple platforms. For example, with GCC, you can build 32-bit x86 applications and 64-bit AMD64 applications on a 64-bit AMD64 platform, and you only need to bring "-m32" in the compiler when generating 32-bit programs.

23. On UNIX systems, the ILP32 (int, long, pointer pointer) data model is no longer applicable to 64-bit applications. If you had already assumed that all int, long, and pointer lengths were 32 bits before you wrote the program, you now have to go back and rewrite it, because long and the pointer are already 64 bits long (LP64). The specific thing to remind you is to check everything about int, long, pointer assignments, and comparison statements. 24. On a 64-bit Windows system, the length of long is still 32 bits, but the pointer and long long are 64 bits, which is called the LLP64 model. 26. To specify the data type in the constant expression. If you want to make a constant long, you must clearly follow the value of L or L, otherwise the compiler might treat it as an int. 28. Be careful with the symbol extension problem in C language. For example, when converting a unsigned int to a 64-bit unsigned long, it must first be converted to a signed int, then signed long, and finally converted to unsigned long. During this period, any value greater than 2 of the 31-square int value will be treated as a negative number, and the minus sign will be extended to the 32 bits added to a long and the last converted unsigned Long value will be much larger than the original value.

C + + Coding specification (excerpt)

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.