Code Style Conventions

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

GENERAL ------Use real tabs that equal 4 spaces.

Use typically trailing braces everywhere (if else structures type"efs class "efiniti!ns etc.# if ( x ) { } $he else state%ent starts !n the sa%e line as the last cl!sing brace. if ( x ) { } else { } &a" parenthesi'e" e(pressi!ns with spaces if ( x ) { } Instead of if (x) { } And x = ( y * 0.5f ); Instead of x = (y * 0.5f); Use precisi!n specificati!n f!r fl!ating p!int values unless there is an e(plicit nee" f!r a "!uble. float f = 0.5f; )nstea" !f float f = 0.5; functi!ns

An" float f = 1.0f; )nstea" !f float f = 1.f; *uncti!n na%es start with an upper case+ void Function( void ); )n %ulti-w!r" functi!n na%es each w!r" starts with an upper case+ void T isFunction!oes"o#et in$( void ); $he stan"ar" hea"er f!r functi!ns is+ %* ==================== Function&a#e !esc'i(tion ==================== *% ,ariable na%es start with a l!wer case character. float x; )n %ulti-w!r" variable na%es the first w!r" starts with a l!wer case character an" each successive w!r" starts with an upper case. float #ax!istanceF'o#)lane; $ype"ef na%es use the sa%e na%ing c!nventi!n as variables they always en" with -.t-. ty(edef int file*andle+t; h!wever

/truct na%es use the sa%e na%ing c!nventi!n as variables they always en" with -.t-. st'uct 'ende',ntity+t;

h!wever

Enu% na%es use the sa%e na%ing c!nventi!n as variables h!wever they always en" with -.t-. $he enu% c!nstants use all upper case characters. 0ultiple w!r"s are separate" with an un"ersc!re. enu# contact+t { -.&TA-T+&.&,/ -.&TA-T+,!0,/ -.&TA-T+1.!,23,4T,5/ -.&TA-T+T413,4T,5 }; Na%es !f recursive functi!ns en" with -.rvoid 6al78")+'( int node ); 1efine" na%es use all upper case characters. 0ultiple w!r"s are separate" with an un"ersc!re. 9define "I!,+F4.&T 0

Use 2c!nst3 as %uch as p!ssible. Use+ const int *(; int * const (; const int * const (; 1!n3t use: int const *(; 4LA//E/ ------%% (ointe' to const int %% const (ointe' to int %% const (ointe' to const int

$he stan"ar" hea"er f!r a class is+


%* =============================================================================== !esc'i(tion =============================================================================== *%

4lass na%es start with -i"- an" each successive w!r" starts with an upper case. class id3ec;; 4lass variables have the sa%e na%ing c!nventi!n as variables. class id3ec; { float float float } x; y; <;

4lass %eth!"s have the sa%e na%ing c!nventi!n as functi!ns. class id3ec; { float } 2en$t ( void ) const;

)n"ent the na%es !f class variables an" class %eth!"s t! %a5e nice c!lu%ns. $he variable type !r %eth!" return type is in the first c!lu%n an" the variable na%e !r %eth!" na%e is in the sec!n" c!lu%n. class id3ec; { float float float float const float * } x; y; <; 2en$t ( void ) const; ToFloat)t'( void ) const;

$he 6 !f the p!inter is in the first c!lu%n because it i%pr!ves rea"ability when c!nsi"ere" part !f the type. 7r"ing !f class variables an" %eth!"s sh!ul" be as f!ll!ws+

1. =. ;. ?. 5. @. A.

list of f'iend classes (u>lic va'ia>les (u>lic #et ods ('otected va'ia>les ('otected #et ods ('ivate va'ia>les ('ivate #et ods

T is alloBs t e (u>lic inte'face to >e easily found at t e >e$innin$ of t e class. Always %a5e class %eth!"s 2c!nst3 when they "! n!t %!"ify any class variables. Av!i" use !f 2c!nst.cast3. 8hen !b9ect is nee"e" t! be %!"ifie" but !nly c!nst versi!ns are accessible create a functi!n that clearly gives an e"itable versi!n !f the !b9ect. $his 5eeps the c!ntr!l !f the 2c!nst-ness3 in the han"s !f the !b9ect an" n!t the user. Return 2c!nst3 !b9ects unless the general usage !f the !b9ect is t! change its state. *!r e(a%ple %e"ia !b9ects li5e i"1ecls sh!ul" be c!nst t! a %a9!rity !f the c!"e while i"Entity !b9ects ten" t! have their state %!"ifie" by a variety !f syste%s an" s! are !5 t! leave n!n-c!nst. *uncti!n !verl!a"ing sh!ul" be av!i"e" in %!st cases. instea" !f+ *!r e(a%ple

const idAni# * 0etAni#( int index ) const; const idAni# * 0etAni#( const c a' *na#e ) const; const idAni# * 0etAni#( float 'ando#!ive'sity ) const; Use+ const idAni# * 0etAni#8yIndex( int index ) const; const idAni# * 0etAni#8y&a#e( const c a' *na#e ) const; const idAni# * 0et4ando#Ani#( float 'ando#!ive'sity ) const; E(plicitly na%e" functi!ns ten" t! be less pr!ne t! pr!gra%%er err!r an" ina"vertent calls t! functi!ns "ue t! wr!ng "ata types being passe" in as argu%ents. E(a%ple+ Ani# = 0etAni#( 0 ); $his c!ul" be %eant as a call t! get a ran"!% ani%ati!n but the

c!%piler w!ul" interpret it as a call t! get !ne by in"e(. 7verl!a"ing functi!ns f!r the sa5e !f a""ing 2c!nst3 accessible functi!n is all!wable+ class idAni#ated,ntity : (u>lic id,ntity { idAni#ato' * 0etAni#ato'( void ); const idAni#ato' * 0etAni#ato'( void ) const; }; )n this case a c!nst versi!n !f GetAni%at!r was pr!vi"e" in !r"er t! all!w GetAni%at!r t! be calle" fr!% c!nst functi!ns. /ince i"Ani%ate"Entity is n!r%ally a n!n-c!nst !b9ect this is all!wable. *!r a %e"ia type which is n!r%ally c!nst !perat!r !verl!a"ing sh!ul" be av!i"e"+ class id!ecl1!5 : (u>lic id!ecl { const id1!5Ani# * 0etAni#( ani#*andle+t andle ) const; id1!5Ani# * 0et,dita>leAni#( ani#*andle+t andle ); }; i" /tu"i! Na%es --------------idCna#eD!l$ idCna#eD-t'l idCna#eDF'# idCna#eD3ieB idCna#eD *)LE NA0E/ --------Each class sh!ul" be in a seperate s!urce file unless it %a5es sense t! gr!up several s%aller classes. $he file na%e sh!ul" be the sa%e as the na%e !f the class with!ut the -i"- prefi(. (Upper:l!wer case is preserve".# class id6indin$; files: 6indin$.c(( 6indin$. %% %% %% %% %% dialo$ class dialo$ cont'ol class f'a#e BindoB vieB BindoB any ot e' class

8hen a class spans acr!ss %ultiple files these files have a na%e that starts with the na%e !f the class with!ut -i"- f!ll!we" by an un"ersc!re an" a subsecti!n na%e. class id4ende'6o'ld; files: 4ende'6o'ld+load.c(( 4ende'6o'ld+de#o.c(( 4ende'6o'ld+(o'tals.c(( 8hen a class is a public virtual interface t! a subsyste% the public interface is i%ple%ente" in a hea"er file with the na%e !f the class with!ut -i"-. $he "efiniti!n !f the class that i%ple%ents the subsyste% is place" in a hea"er file with the na%e !f the class with!ut -i"- an" en"s with -.l!cal.h-. $he i%ple%entati!n !f the subsyste% is place" in a cpp file with the na%e !f the class with!ut -i"-. class id4ende'6o'ld; 4ende'6o'ld. 4ende'6o'ld+local. 4ende'6o'ld.c(( %% (u>lic vi'tual id4ende'6o'ld inte'face %% definition of class id4ende'6o'ld2ocal %% i#(le#entation of id4ende'6o'ld2ocal

You might also like