Use camelCase for most varNames. Use ProperCase for names of classes and structs. Use camelCase for instances of such classes.
See important notes on case on the parent page for user facing names!
We follow http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Comments for placement of comments.
As for style, we use javadocs 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 std::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
std::map<std::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 explaining
// why that is takes multiple lines.
if (!y) {
printf("y is true!\n");
}
}
Don’t forget – even if a class’s purpose is obvious, you can put a comment on it as to why it exists!
Put long inline functions in a -inl.h file.
If your inline function is a single line long, put it and its decl on the same line e.g.:
int length() const { return _length; }
If a function is not performance sensitive, and it isn’t one (or 2) lines long, put it in the cpp file. Keep code out of headers.
See util/mongoutils/str.h and bson/stringdata.h
Use
str::startsWith()
str::endsWith()
and not strstr().
Use
<< 'c'
and not << "c".
Use
str[0] == '\0'
and not strlen(str) == 0.
if (0) {
}
else if (0) {
}
else {
}
do {
} while (0);
class Foo {
int _bar;
};
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.
void foo(int v, MyType myItem) {
}
foo(1, MyType());
set<int> s;
namespace foo {
int foo;
namespace bar {
int bar;
}
}
license (AGPL or Apache, depending on C++ driverness)
BAD
int foo() {
if (x) {
...
}
}
GOOD
int foo() {
if (!x)
return;
...
}
Keeps indentation levels down and makes more readable.
Large, 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;
To avoid implicit type conversion, use the explicit keyword before constructors that take a single parameter.
Use “double quotes” for 10gen code, <angle brackets> for 3rd party or library headers.
examples:
#include "mongo/pch.h"
#include <boost/thread.h>
#include <vector>
Always use forward relative path from mongo/src/; do not use ..
correct:
#include "mongo/db/namespace_details.h"
incorrect:
#include "../db/namespace_details.h"
example for classy.cpp:
#include "mongo/pch.h"
#include "mongo/db/classy.h"
#include <boost/thread.h>
#include <stdio.h>
#include <string>
#include "mongo/db/db.h"
#include "mongo/db/namespace_details.h"
#include "mongo/util/concurrency/qlock.h"
Aspire to embrace RAII
When writing functions that take or return bare pointers, document the ownership semantics in the header comment.
Prefer caller-retains ownership of parameters and takes ownership of returned pointers, but use the appropriate policy for each situation.
Generally, bare calls to delete and free() are red flags
Use smart pointers such as boost::scoped_ptr and std::auto_ptr (know the difference between them!) to avoid memory leaks and ensure all new‘s and malloc‘s are paired with delete‘s and free‘s
Use ON_BLOCK_EXIT or ScopeGuard to protect other resources that must be released