CodeStyleConventions 代码风格约定(二)
s 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 should 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 should 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 should 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 calls to functions due to wrong data types being passed in as arguments. Example:
Anim = GetAnim( 0 );
This could be meant as a call to get a random animation, but the compiler would 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 should be avoided:
class idDeclMD5 : public idDecl {
const idMD5Anim * GetAnim( animHandle_t handle ) const;
idMD5Anim * GetEditableAnim( animHandle_t handle );
};
id Studio Names
---------------
idDlg // dialog class
idCtrl // dialog control class
idFrm // frame window
idView // view window
id // any other class
FILE NAMES
---------
Each class should be in a seperate source file unless it makes sense to group several smaller classes.
The file name should be the same as the name of the class without the "id" prefix. (Upper/lower case is preserved.)
class idWinding;
files:
Winding