CodeStyle Guide

From SfzWiki
Jump to: navigation, search

this is just a rough guideline, not a definite rulebook


classes are prefixed with a lower-case "c", it does help distinguishing class constructors

 cMyClass(bla);  

from function/method calls

 MyMethod(bla);

since we don't lowercase functions/methods


CamelCase for functions and methods

we use a simple hungarian (i for int, f for float, p for pointer/reference ,..) for parameters and member-variables, but not for every little local variable.

other prefixes :

 c	class
 k	constants
 i	integers
 f	floats
 v	vectors (3d,x,y,z)
 q	quaternions (4d-vector orientation,rotation)
 m	members
 l	lists,maps
 p	pointers,references(to big classes, not to primitive types)

constants (enum and define) start with k like

 kSomeValue 

macros (define) should be all upper case , like

 WRITE_FLOAT(..)

in function definitions and declarations there should be some whitespace (tab or space) between the functionname and the opening ( for the parameter list)

and in function calls there should be no whitespace between the functioname and the ( e.g.

 void  MyFun   (int a);      but   MyFun(123);

that makes searching the code easier in some cases


the opening { brace is usually on the same line as the function (unless there are many parameter so there are newlines in the parameter list) to avoid spreading the code too much unneccessarily e.g.

 void    MyFun (int a) {
    print(a);
 }

i have an editor that shows the association nicely, and the indention of the void and the closing } should be the same same with if if one part of the if has a bracet, the other should have one as well


both these are ok :

 if (..)
   MyFun();
 else MyFun2();
 if (..) {
   MyFun();
 } else {
   MyFun2();
 }

but this is not ok :

 if (..) {
   MyFun();
 } else MyFun2();  // BAAAAD CODE STYLE !, should have { brace for both parts


Pointer declarations should have the * left associated (since we are defining a pointer type), the dereference operator should be right aligned (since it is operator on that pointer), and the multiplication operator should be the only one with whitespace on both side.s

 void MyFun  (void*  myPtr);
 (double*)
 int* pNumber = new int;  *pNumber = 5;  delete pNumber;  


use C++ style comments for single lines

 // mycomment  

because if you want to comment out a big block for debugging, /* ... */ doesn't nest

we try to use doxygen for function descriptions a bit

 three /// directly above the function
 ///< right of member vars
 /** for block comments **/