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 spaces
If (x ){
}
Instead
If (x ){
}
And
X = (y * 0.5f );
Instead
X = (y * 0.5f );
Use precision specification for floating point values unless there is an explicit need for a double.
Float F = 0.5f;
Instead
Float F = 0.5;
And
Float F = 1.0f;
Instead
Float 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,
Contact_trmvertex
};
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 0
Use 'const' as much as possible.
Use:
Const int * P; // pointer to const int
Int * const P; // const pointer to int
Const int * const P; // const pointer to const int
Don'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 classes
2. Public Variables
3. Public Methods
4. Protected Variables
5. Protected Methods
6. Private Variables
7. Private methods
This 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 leave
Non-Const.
Function overloading shocould be avoided in most cases. For example, instead:
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 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 class
ID <Name> CTRL // dialog control class
ID <Name> frm // frame window
ID <name> View // View window
ID <Name> // any other class
File Names
---------
Each class shocould be in a seperate source file unless it makes sense to group several smaller classes.
The file name shoshould be the same as the name of the class without the "ID" prefix. (upper/lower case is preserved .)
Class idwinding;
Files:
Winding. cpp
Winding. h
When a class spans implements 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:
Renderworld_load.cpp
Renderworld_demo.cpp
Renderworld_portals.cpp
When 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 Interface
Renderworld_local.h // definition of Class idrenderworldlocal
Renderworld. cpp // Implementation of idrenderworldlocal