basics
caseUse camelCase for most varNames See important notes on case on the parent page for user facing names! commentsWe follow http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Comments for placement of comments As for style, we use javadoc's in classes and methods (public or private) and simple comments for variables and inside code /** * My class has X as a goal in life * Note: my class is fully synchronized */ class DoesX { ... /** * This methods prints something and turns off the lights. * @param y the something to be printed */ void printAndGo(const string& y) const; ... private: // a map from a namespace into the min key of a chunk // one entry per chunk that lives in this server map< string , BSONObj > _chunkMap; /** * Helper that finds the light switch */ Pos _findSwitch() const; /** @return the light switch state. */ State _getSwitchState() const; }; void DoX( bool y) {
// if y is false, we do not need to do a certain action and explining
// why that is takes multiple lines.
if (! y ) {
}
Don't forget – even if a class's purpose is obvious, you can put a inlines
stringsSee
Use str::startsWith(), str::endsWith(), not strstr(). Use << 'c' not << "c". Use str[0] == '\0' not strlen(str) == 0. See Kernel string manipulation. bracketsif ( 0 ) { } else if ( 0 ) { } else { } do { } while ( 0 ); class membersclass Foo {
int _bar;
};
functionsDeclaration: void foo( int v, MyType myItem );
Avoid declarations of extern functions in source files! Instead, #include a proper .h file. Be sure to match the header filename to the source filename where the function definition appears. Definition: void foo( int v, MyType myItem ) {
}
Invocation: foo( 1, MyType() ); templatesset<int> s;
namespacesnamespace foo {
int foo;
namespace bar {
int bar;
}
}
start of filelicense (AGPL or Apache, depending on C++ driverness) assertionsSee Kernel exception architecture. return earlyBAD int foo(){ if ( x ){ ... } } GOOD int foo(){ if ( ! x ) return; ... } Keeps indentation levels down and makes more readable. numeric constantsLarge, round numeric constants should be written in multiplied form so that you never need to count digits. const int tenMillion = 10*1000*1000; const int megabyte = 1024*1024; explicit constructorsTo avoid implicit type conversion, use the "explicit" keyword before constructors that take a single parameter. #includes
file names
|

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.
blog comments powered by Disqus