C ++ design and development specifications (2): naming rules

Source: Internet
Author: User

2. Naming rules "Name from the user's perspective"  Consistent naming can make the program clearer and the code more descriptive, thus improving the readability and maintainability of the program. This specification describes the most basic naming rules, including how to use case sensitivity and provides reference standards for naming the basic concepts involved in software design and development, such as namespace, type, Member, parameter name, etc. 2.1. Case sensitivity conventionsThere are two common case-insensitive conventions: PascalcasingAnd Camelcasing. √ RequirementsThe name is case sensitive. √ RequirementsUsed in naming classes, structures, interfaces, and functions Pascalcasing. For example, you need to use class columninformation instead of class columninformation or class columninformation. √ RequirementsThe names of macro, constant, class, and structure public constants are all capitalized ( Recommended:Separate words with underscores ). For example, raise_exception (exp) const int max_prime_number = 100 class applicationinfo {public: static const int product_version = 0x01000000 ;} × NoUse two or more names or macros that are case-insensitive. √ RequirementsAll types of the name callback function are in uppercase. For example, use typedef void (* clickingeventhandler) () instead of typedef void (* clickingeventhandler )(). √ RequirementsUsed to name function parameters, class/structure member variables, and local variables Camelcasing. For example, use long streampostion instead of long streampostion; use long m_streampostion instead of long m_streampostion. √ RequirementsIn the case of an uppercase letter PascalcasingStyle name)/lower case (Use CamelcasingStyle name ). For example, void processio (STD: stream & iostream ). Void processhtml (STD: stream & iostream ). √ RequirementsAll the acronyms that consist of three or more letters are capitalized ( PascalcasingStyle name)/all lower case (Use CamelcasingStyle name ). For example, xmltag and xmlreader. 2.2. General naming conventions √ RequirementsThe name must be meaningful and case-sensitive. It can be clearly understood as the name suggests. Exceptions:Name of the Loop Variable I, j, k... l Recommendation Do not overUse Hungarian naming convention. For example, try not to use the Hungarian naming method in the SDK interface to ensure that the naming is fair to both Windows and non-Windows (such as Linux/Unix) programmers. × NoUse abbreviations that are not universally accepted. For example, do not use Si to indicate spread items that are not widely accepted, do not use getnthitem, and use getitem × NoUse "ex" in the identifier and use numbers to distinguish the same APIFor example, use void showmessage2 () instead of void showmessageex () to name the second version of void showmessage. L RecommendationDo not use "ex" to distinguish between two similar functions in the same version. API, It is best to use a more meaningful name. For example, void docommand () void docommandex () // void doplugincommand () // is better, and the meaning is clearer. × NoWhen "ex" is used to distinguish the same or different versions Class/Structure, It is best to use a more meaningful name. For example, class streamposition {...} Class streampositionex {...} // poor class fixedstreamposition {...} // good class dynamicstreamposition {...} // good 2.3. namespace name √ RequirementsUse lowercase letters to name the namespace. For example, graph, Dom, v12sys: Io, v12sys, v12sys: Text × NoUse an existing generic name for namespace. For example, STD × NoUse the same name to name the namespace and the type of the namespace. For example, do not define a graph class in the graph namespace. 2.4. Type (class, structure, interface, enumeration, typedef) Naming √ Requirements Use a noun or noun phrase to name a type. Exceptions:In rare cases, you can use adjectives (phrases) to name a type. For example, class pagesetting, class document // good class vcodeparser // good class parsevcode // bad √ Requirements Pascalcasing Case Sensitive. Exceptions:Typedef. L RecommendationWhen you use typedef to name basic types, use uppercase letters. For example, typedef int vint32 typedef long vobjid32. √ RequirementsLet the interface name start with the letter I. For example, interface icopyable, interface iapplication, and interface iobservermanager. 2.4.1. generic type naming √ Requirements Use a noun or noun phrase to name a generic type. Use pascalcasing Case Sensitive. For example, template <typename tinput, typename toutput> class converter // √ RequirementsNaming generic parameters uses the TXXX paradigm rather than the _ TXXX or other paradigm. Template <typename tinput, typename toutput> class converter // good template <typename _ tinput, typename _ toutput> class converter // bad √ RequirementsName the description attribute to name generic parameters. Exceptions:If the generic type has only one parameter and the type parameter has only one letter, use the t class to name the parameter type. For example, template <typename T> bool tostring (const T & Value) template <typename tsession> class sessionchannel (const tsession & session) 2.4.2. Name of Enumeration type √ Requirements Use a noun or noun phrase to name an enumeration type. Use pascalcasing Case sensitive.For example, Enum kfiletype and enum kexporttype × NoAdd the suffix "Enum", "flag", and "Flags" to the enumeration type name. For example, do not define Enum kfiletypeenum or enum kexporttypeflags √ RequirementsUse the "K" prefix for Enumeration type names. √ RequirementsName of an enumerated value = Name of the enumerated type + "_" + name of the true meaning of the enumerated value. For example, Enum kfiletype {kfiletype_none, kfiletype_ansi, kfiletype_unicode}, for example, long g_s_visittimes, unsigned int g_s_currentmessage √ RequirementsAdd the G _ prefix to the global variable name. 2.5. member name2.5.1. Function naming √ Requirements Use a verb or verb phrase to name a function. For example, track, showmessage, getitem √ RequirementsThe name of the function used to set the program State = set + indicates the noun, noun phrase, or adjective of the program state. For example, setdirty, setchangestate, setwindowtext √ RequirementsThe name of the function used to obtain the state of the program = get + indicates the noun, noun phrase, or adjective of the state of the program. For example, int getchangestate () const, int getenabled () constl RecommendationUsed to obtain the program status and Returns a Boolean value.The name of the function = is/CAN/has/contain/include + a noun, noun phrase, or adjective indicating the state of the program. For example, bool contain (INT item) const, bool hasunicodecharacter bool ischanged () const2.5.2. name of the event √ RequirementsAll the naming event types (that is, the return function) are in uppercase. For example, typedef void (* buttoneventhandler )(); √ RequirementsUse a verb or verb phrase to name an event For example, typedef void (* buttoneventhandler) (); buttoneventhandler clicking; buttoneventhandler clicked; √ RequirementsUse the current time and past time to distinguish between an ongoing (previous) and an ongoing (later) event. For example, typedef void (* signature) (); buttoneventhandler onbeforeclick; // The parameter onafterclick is not standardized; // The parameter buttoneventhandler clicking is not standardized; // The parameter value is specified by the parameter 2.5.3. field name √ RequirementsUse a noun or noun phrase to name a field. For example, STD: String m_phone, long m_areacode √ RequirementsAdd the S _ prefix to the static field name. For example, long m_s_timeelapsed, long g_s_visittimes √ Requirements Add M _ to private or protected fields _ Prefix. For example, long m_s_timeelapsed × NoAdd the M _ prefix to public fields. The naming requirements for public fields are as follows: PascalcasingStyle. For example, class pointf {public: static const pointf empty; // ...} 2.6. Name of global variables/functions2.6.1. global variable naming √ RequirementsUse a noun or noun phrase to name a field. √ RequirementsGlobal variables must be named PascalcasingStyle. For example, long g_s_visittime; // good long g_s_visittimes; // bad √ RequirementsAdd the S _ prefix to the static global variable field name. 2.6.2. Global function naming √ RequirementsName a member function. 2.7. Parameter Name √ RequirementsUsed to name Parameters CamelcasingCase sensitive. √ RequirementsUse descriptive parameter names.  For example, void changephoto (const STD: string & photofilename, int phototype) 2.8. typedef √ RequirementsCommon typedef rules follow the following table:

Typedef Specifications
Typedef STD: vector <t> √ RequirementsAdd the suffix "array" for a, such as typedef STD: vector <int> indexarray;
Typedef STD: Stack <t> √ RequirementsAdd the suffix "stack" for a, such as typedef STD: vector <int> orderstack;
Typedef STD: Map <T1, T2> ASTD: multimap <T1, T2> √ RequirementsAdd the suffix "map" for a, such as typedef STD: Map <string, string> configmap;
Typedef STD: List <t> √ RequirementsAdd the suffix "list" for a, such as typedef STD: List <string> namelist;
Typedef STD: deque <t> atypedef STD: queue <t> √ RequirementsAdd the suffix "queue" for a, such as typedef STD: queue <string> studentqueue;
Typedef STD: Set <t> atypedef STD: Multiset <t> √ RequirementsAdd the suffix "set" for a, such as typedef STD: Set <int> primeset;
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.