High quality C/C ++ programming (3)

Source: Internet
Author: User

Chapter 2 naming rulesThe famous naming rule is Microsoft's "Hungary" method. The main idea of this naming rule is to "add prefixes to variables and function names to improve people's understanding of programs ". For example, all character variables are prefixed with CH, and if it is a pointer variable, the prefix P is appended. If a variable starts with PPCH, it indicates that it is a pointer to a character pointer. The biggest disadvantage of the "Hungary" method is that it is cumbersome, such as int I, j, k; float X, Y, and Z. If the "Hungary" Naming rule is used, it should be written as int II, IJ, ik; // prefix I indicates the int type float FX, fy, Fz; // The prefix F indicates that the float type is so cumbersome that the vast majority of programmers cannot bear it. According to the investigation, there is no naming rule that can be approved by all programmers. Generally, No naming rule is specified in the programming textbooks. Naming rules are not a matter of success or failure for software products. We should not focus too much on trying to invent the best naming rules in the world, A naming rule should be formulated to satisfy most project members and implemented in the project. 3.1 Common rulesThe common rules discussed in this section are adopted by most programmers. We should follow these common rules to expand specific rules, such as section 3.2. L Rule 3-1-1 ]Identifiers should be intuitive and readable. They do not need to be decoded ". It is recommended that identifiers use English words or their combinations to facilitate memory and reading. Do not use Chinese pinyin for naming. The English words in the program are generally not too complex and should be accurate. For example, do not write currentvalue as nowvalue. L Rule 3-1-2 ]The length of the identifier must comply with the "Min-length & Max-Information" principle. A few decades ago, ansi c specified that the name should not exceed 6 characters. Today's c ++/C does not have this limit. In general, long names can better express meanings. Therefore, function names, variable names, and class names can contain up to a dozen characters. So is the name a longer appointment? No! For example, the variable name maxval is better than maxvalueuntiloverflow. Single-character names are also useful. Common ones include I, J, K, M, N, x, y, and z. They are usually used as local variables in functions. L Rule 3-1-3 ]Naming Rules should be consistent with the operating system or development tool style. For example, the identifiers of Windows applications are usually in a case-insensitive manner, such as addchild. The identifier of a Unix application is usually "lowercase and underlined", for example, add_child. Do not mix these two types of styles. L Rule 3-1-4 ]Do not show similar identifiers that are case sensitive. For example: int X, X; // variables X and X are easy to confuse void Foo (int x); // functions Foo and foo are easy to confuse void Foo (float X); L Rule 3-1-5 ]The local variables and global variables with identical identifiers should not appear in the program. Although the scopes of the two are different without syntax errors, they may be misunderstood. L Rule 3-1-6 ]The variable name should use a "noun" or "Adjective + noun ". Example: Float value; float oldvalue; float newvalue; L Rule 3-1-7 ]The name of a global function should be "verb" or "Verb + noun" (verb phrase ). Class member functions should only use the "verb", and the omitted nouns are the object itself. Example: drawbox (); // global function box-> draw (); // class member function L Rule 3-1-8 ]Use a correct antgroup to name variables with mutex meanings or functions with opposite actions. For example: int minvalue; int maxvalue; int setvalue (...); Int getvalue (...); 2. [3-1-1 is recommended. ]Avoid numeric numbers in the name, such as value1 and value2, unless the number is required logically. This is to prevent programmers from being lazy and refuse to name their brains, resulting in meaningless names (because numbers are the most convenient ). 3.2 simple naming rules for Windows ApplicationsThe authors have simplified the naming rules of Hungary. The following naming rules are simple and easy to use, and are suitable for the development of Windows application software. L Rule 3-2-1 ]The class name and function name are combined with words starting with an upper-case letter. For example: Class node; // class name class leafnode; // class name void draw (void); // function name void setvalue (INT value); // function name l Rule 3-2-2 ]Variables and parameters are combined with words starting with lowercase letters. Example: bool flag; int drawmode; L Rule 3-2-3 ]Constants all use uppercase letters and underline to separate words. Example: const int max = 100; const int max_length = 100; L Rule 3-2-4 ]Static variables are prefixed with S _ (indicating static ). Example: void Init (...) {Static int s_initvalue; // static variable ...} L Rule 3-2-5 ]If you have to use a global variable, add the global variable with the prefix G _ (indicating global ). Example: int g_howmanypeople; // global variable int g_howmuchmoney; // global variable L Rule 3-2-6 ]The data member of the class is prefixed with m_( indicating Member). This prevents the data member from having the same name as the parameter of the member function. Example: void object: setvalue (INT width, int height) {m_width = width; m_height = height;} l Rule 3-2-7 ]To prevent conflicts between some identifiers in a software library and other software libraries, you can add prefixes that reflect the nature of software to various identifiers. For example, all library functions of 3D graphics standard OpenGL start with GL, and all constants (or macro definitions) start with GL. 3.3 simple naming rules for Unix applications

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.