Practical C ++ coding specifications

Source: Internet
Author: User

1 typographicalTip 1-1: The program block must be written in an indent style. In the VC Editor, The Tab key is used for indentation. Do not use the tab key and Space key to mix and indent. Note: by default, the tab key in VC contains four spaces. When you use different editors to read the program, the program layout may be uneven due to the number of spaces set by the tab key. Between 1-2: Empty lines must be added after the description of variables between relatively independent program blocks. Limit 1-3: A long statement (> 80 characters) should be divided into multiple lines of writing. A long expression should divide a new line at the lower-priority operator, and the operator should be placed at the first of the new line, the new lines should be properly indented to make the layout neat and the statement readable. Limit 1-4: If a long expression or statement exists in statements such as loop and judgment, the adaptive division is required. A long expression must divide a new line at a low-priority operator, operators are placed at the beginning of a new line. Limit 1-5: if there are many parameters in a function or process, divide them appropriately. Limit 1-6: Multiple phrase sentences cannot be written in one row, that is, only one statement can be written in one row. Statement 1-7: if, for, do, while, Case, switch, default, and other statements occupy one row, and if, for, do, while, and other statements must be enclosed in brackets no matter how many execution statements {}. Duration 1-8: the code in statements such as the start of a function or process, definition of the structure, and loop, and judgment must adopt the indent style, when a case statement is used, the processing statement must also comply with the statement indent requirements. Period 1-9: the boundary of the program block (for example, braces '{' and '}' in C/C ++) should be exclusive to each row and be located in the same column, at the same time, it is left aligned with the statements that reference them. At the beginning of the function body, the definition of the class, the definition of the structure, the definition of enumeration, and the programs in the IF, for, do, while, switch, and case statements must adopt the above indent method. Limit 1-10: When two or more keywords, variables, and constants perform peer-to-peer operations, spaces must be added before, after, or before and after the operators between them. For non-peer operations, if it is an immediate operator (such as->) That is closely related, no space should be added. 2 NoteLimit 2-1: Generally, the valid comments of the source program must be greater than 20%. Note: The principle of annotation is to help you read and understand the program. It is added to the place where the annotation is added. The annotation language must be accurate, easy to understand, and concise. Tip 2-2: The source file header should be annotated, and the notes must be listed: Copyright description, generation date, file path, brief description, author information, etc. We recommend that you use visual assistant to automatically generate the following comments:/*** copyright (c) 2004-2006 Tom Online Inc * @ time * @ file E: /workspace/projects/skypewebtoolbar/log. H * @ brief print the test information's log class * @ author chenmin (chen_min@tomonline-inc.com) */Feature 2-3: function module comments that must be listed: function name, function description, parameter information, return results. /*** @ FN void logresult (char * szinfo, int nretcode, const char * cszstrip) * @ brief records the requests received by the API server in the log, and the returned result * @ Param szinfo [in]-char * after processing, the received request information * @ Param nretcode [in]-int, the Returned Code * @ Param cszstrip [in]-const char *, request source IP address * @ return void */void logresult (char * szinfo, unsigned int nretcode, const char * cszstrip) limit 2-4: class and structure comments. The comments must be listed: Class Name (Structure name), which is a brief description. The format is as follows:/*** @ Class cdbparameter * @ brief database connection parameter class */class cdbparameter {public: Char m_szhost [128]; /// 3. identifier nameTip 3-1: The name of an identifier must be clear, clear, and meaningful. You can also use complete words or abbreviations that are generally understandable to avoid misunderstanding. Note: short words can be abbreviated by removing the "vowel". long words can be abbreviated by the first few letters of a word. Some words are accepted as abbreviations. Example: The abbreviations of the following words can be basically recognized by everyone. Temp can be abbreviated to TMP; flag can be abbreviated to flg; statistic can be abbreviated to stat; increment can be abbreviated to Inc; message can be abbreviated to MSG; timeout 3-2: For variable naming, it is not allowed to take a single character (such as I, J, K ...), in addition to specific meanings, we recommend that you also specify the variable type and data type. However, I, J, and K can be used as local cyclic variables. Unless necessary, do not use numbers or strange characters to define identifiers to avoid readers being confused. Note: variables, especially local variables, are represented by a single character and can be easily typed incorrectly (for example, I is written as J), but cannot be checked during compilation, it may take a lot of time to identify this small error. Token 3-3: Use a correct antgroup to name variables with mutex meanings or functions with opposite actions. Note: The following are some antgroups that are commonly used in software. Add/Remove begin/end create/destroy insert/Delete first/last get/releaseincrement/decrement put/getadd/delete lock/unlock open/closemin/MAX old/new start/stopnext/ previous source/Target show/hidesend/receive source/destinationcut/paste up/down limit 3-4: we recommend that you use the Hungary encoding standard name, which is outlined below: 1. class Name: A combination of uppercase letters in C +. It is usually a namespace phrase and generally does not contain underscores (_). For example, cmyclass2. function (class member function): A combination of uppercase letters, normally, phrases in a dynamic object structure do not contain underscores, such as po. Stmessage (), saveprofile (), etc. 3. member variable: starting with M _, lowercase type tag + combination of uppercase words in the first letter, usually a namephrase. DWORD is abbreviated as DW, int as I, usigned int as U or N, handle as H, window as WND, and string ending with 0 as sz, the cstring value is str, for example, m_szname, m_strname, m_nage 4. the global variable is the same as the member variable, but the M _ is changed to g _ 5. static variables are the same as member variables, but M _ is changed to S _ 6. parameter, same as the member variable, only remove M _, no prefix, such as saveprofile (sznewprofile) 7. pointer, which is usually expressed as P in front of the variable. The pointer to the pointer is pp.

 

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.