The foundation of the libhdfs++ project's coding standards is Google's C++ style guide. It can be found here:
https://google.github.io/styleguide/cppguide.html
There are several small restrictions adopted from Sun's Java standards and Hadoop convention on top of Google's that must also be followed as well as portability requirements.
Prior to submitting a patch for code review use LLVM's formatting tool, clang-format, on the .h, .c, and .cc files included in the patch. Use the -style=google switch when doing so.
Example pre-submission usage:
$ clang-format -i -style=google temp_file.cc
note: On some linux distributions clang-format already exists in repositories but don't show up without an appended version number. On Ubuntu you'll find it with:
"apt-get install clang-format-3.6"
Always add brackets conditional and loop bodies, even if the body could fit on a single line.
BAD:
if (foo)
Bar();
if (foo)
Bar();
else
Baz();
for (int i=0; i<10; i++)
Bar(i);
GOOD:
if (foo) {
Bar();
}
if (foo) {
Bar();
} else {
Baz();
}
for (int i=0; i<10; i++) {
Bar(i);
}
Use the /* comment */ style to maintain consistency with the rest of the Hadoop code base.
BAD:
//this is a bad single line comment
/*
this is a bad block comment
*/
GOOD:
/* this is a single line comment */
/**
* This is a block comment. Note that nothing is on the first
* line of the block.
**/
Please make sure you write code that is portable.
int32_t my_32_bit_wide_int
BAD:
struct Foo {
int32_t x_;
char y_;
int32_t z_;
char z_;
} __attribute__((packed));
/**
* "I didn't profile and identify that this is causing
* significant memory overhead but I want to pack it to
* save 6 bytes"
**/
NECESSARY: Still not good but required for short-circuit reads.
struct FileDescriptorMessage {
struct cmsghdr msg_;
int file_descriptors_[2];
} __attribute__((packed));
/**
* This is actually needed for short circuit reads.
* "struct cmsghdr" is well defined on UNIX systems.
* This mechanism relies on the fact that any passed
* ancillary data is _directly_ following the cmghdr.
* The kernel interprets any padding as real data.
**/