Kernel code style

basics

  • Use spaces, no literal tabs.
  • 4 spaces per indentation.
  • Limit lines to 100 columns.

case

Use camelCase for most varNames

See important notes on case on the parent page for user facing names!

comments

We 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
comment on it as to why it exists!

inlines

  • 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.

strings

See

  • util/mongoutils/str.h
  • bson/stringdata.h

Use str::startsWith(), str::endsWith(), not strstr().

Use << 'c' not << "c".

Use str[0] == '\0' not strlen(str) == 0.

See Kernel string manipulation.

brackets

if ( 0 ) {
}
else if ( 0 ) {
}
else {
}

do {
} while ( 0 );

class members

class Foo {
   int _bar;
};

functions

Declaration:

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() );

templates

set<int> s;

namespaces

namespace foo {
   int foo;
   namespace bar {
      int bar;
   }
}

start of file

license (AGPL or Apache, depending on C++ driverness)

assertions

See Kernel exception architecture.

return early

BAD

int foo(){
   if ( x ){
     ...
   }
}

GOOD

int foo(){
   if ( ! x )
     return;

   ...
}

Keeps indentation levels down and makes more readable.

numeric constants

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;

explicit constructors

To avoid implicit type conversion, use the "explicit" keyword before constructors that take a single parameter.

#includes

  • 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"
    
    For cpp files:
  • Include mongo/pch.h first. blank line.
  • Include your .h file next, if applicable. blank line.
  • Include third party headers next, sorted. blank line.
  • Include 10gen headers last, sorted
    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"
    
    For h files:
  • #pragma once at the top
  • Forward declare, if possible, in lieu of including 10gen headers in headers. Only include things that are directly used in the header itself.
  • Include third party headers first, sorted. blank line.
  • Include 10gen headers last, sorted.
  • Be extremely careful about including pch.h in a header.

file names

  • Class definitions should go in a header file with the same name as the class. Insert _ in place of a capital letter. Do not use capital letters in filenames!
    example: ClassyClass's definition goes in "classy_class.h". ClassyClass's member function implementations go in "classy_class.cpp".
  • Do not be afraid to make another file, even if it is really small. This is preferable to inserting your new class into a preexisting file.

Follow @mongodb

MongoDB Pittsburgh - May 15
MongoNYC - May 23
MongoDB Paris - Jun 14
MongoDB UK - Jun 20
MongoDC - June 26


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

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

blog comments powered by Disqus