C/C ++ programming specifications

Source: Internet
Author: User
General ------- use real tabs that equal 4 spaces. use typically trailing braces everywhere (if, else, functions, structures, typedefs, class definitions, etc .) if (x) {} The else statement starts on the same line as the last closing brace. if (x) {} else {} pad parenthesized expressions with spacesif (x) {} Instead of IF (x) {} AndX = (y * 0.5f ); instead ofx = (y * 0.5f); Use precision specification for floating point values unless there is an explicit need for a double. float F = 0.5f; instead offloat F = 0.5; andfloat F = 1.0f; instead offloat F = 1.f; function names start with an upper case: void function (void ); in multi-word function names each word starts with an upper case: void thisfunctiondoessomething (void); the Standard Header for functions is: /* ============================== functionname description ====================== ===== */variable names start with a lower case character. float X; in Multi-word variable names the first word starts with a lower case character and each successive word starts with an upper case. float maxdistancefromplane; typedef names use the same naming convention as variables, however they always end with "_ t ". typedef int filehandle_t; struct names use the same naming convention as variables, however they always end with "_ t ". struct renderentity_t; Enum names use the same naming convention as variables, however they always end with "_ t ". the enum constants use all upper case characters. multiple words are separated with an underscore. enum contact_t {contact_none, contact_edge, contact_modelvertex, signature}; names of recursive functions end with "_ r" Void walkbsp_r (INT node); defined names use all upper case characters. multiple words are separated with an underscore. # define side_front 0use 'const' as much as possible. use: const int * P; // pointer to const intint * const P; // const pointer to intconst int * const P; // const pointer to const intdon't use: int const * P; Classes ------- the Standard Header for a class is: /* ===================================================== ========================================================== = Description = ========================================================== = */class names start with "ID" and each successive word starts with an upper case. class idvec3; class variables have the same naming convention as variables. class idvec3 {float X; float y; float Z;} class methods have the same naming convention as functions. class idvec3 {float length (void) const;} indent the names of class variables and class methods to make nice columns. the variable type or method return type is in the first column and the variable name or method name is in the second column. class idvec3 {float X; float y; float Z; float length (void) const; const float * tofloatptr (void) const ;} the * of the pointer is in the first column because it improves readability when considered part of the type. ording of class variables and Methods shoshould be as follows: 1. list of friend classes2. public variables3. public methods4. protected variables5. protected methods6. private variables7. private methodsthis allows the public interface to be easily found at the beginning of the class. always make class methods 'const' when they do not modify any class variables. avoid use of 'const _ cast '. when object is needed to be modified, but only const versions are accessible, create a function that clearly gives an editable version of the object. this keeps the control of the 'const-ness 'in the hands of the object and not the user. return 'const' objects unless the General Usage of the object is to change its state. for example, media objects like iddecls shocould be const to a majority of the Code, while identity objects tend to have their state modified by a variety of systems, and so are OK to leavenon-const.Function overloading shoshould be avoided in most cases. for example, instead of: const idanim * getanim (INT index) const; const idanim * getanim (const char * Name) const; const idanim * getanim (float randomdiversity) const; use: const idanim * getanimbyindex (INT index) const; const idanim * getanimbyname (const char * Name) const; const idanim * getrandomanim (float randomdiversity) const; explicitly named functions tend to be less prone to programmer error and inadvertent callto functions due to wrong data types being passed in as arguments. example: anim = getanim (0); this cocould be meant as a call to get a random animation, but the compiler wowould interpret it as a call to get one by index. overloading functions for the sake (Purpose) of adding 'const' accessible function is allowable: Class idanimatedentity: public identity {idanimator * getanimator (void); const idanimator * getanimator (void) const;}; in this case, a const version of getanimator was provided in order to allow getanimator to be called from const functions. since idanimatedentity is normally a non-const object, this is allowable. for a media type, which is normally const, Operator Overloading shoshould be avoided: Class iddeclmd5: Public iddecl {const idmd5anim * getanim (animhandle_t handle) const; idmd5anim * geteditableanim (animhandle_t handle );}; id studio names --------------- id <Name> DLG // dialog classid <Name> CTRL // dialog control classid <Name> frm // frame windowid <name> View // view windowid <Name>> // any other classfile names --------- each class shocould be in a seperate source file unless it makes sense to group several smaller classes. the file name shocould be the same as the name of the class without the "ID" prefix. (upper/lower case is preserved .) class idwinding; Files: winding. cppwinding. hwhen a class spans using SS multiple files these files have a name that starts with the name of the class without "ID", followed by an underscore and a subsection Name. class idrenderworld; Files: Your a class is a public virtual interface to a subsystem, the public interface is implemented in a header file with the name of the class without "ID ". the definition of the class that implements the subsystem is placed in a header file with the name of the class without "ID" and ends with "_ local. H ". the implementation of the subsystem is placed in a CPP file with the name of the class without "ID ". class idrenderworld; renderworld. h // Public Virtual idrenderworld interfacerenderworld_local.h // definition of Class idrenderworldlocalrenderworld. CPP // Implementation of idrenderworldlocal

* I forgot the specific article mentioned in an article on csdn.

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.