原則 Google C++ Style Guide に従います。 但し、以下の点に関して差分があります。
なお、外部から導入された jubatus/server/third_party 以下のファイルは対象外です。
Jubatus-Specific Magic =====================
pfi::lang::shared_ptr を用いる。
tools/codestyle/cpplint/cpplint.py
を利用する。
constのつかない通常の参照を許す。
例外を利用する。 STLが投げる例外や、依存ライブラリが投げるものはすべてcatchする以外では、基本的にjubatus::exception::jubatus_exceptionを投げるようにする。 またJUBATUS_EXCEPTION()マクロを利用する
例外に用いるメッセージはwhatではなく、error_message機能を活用する。 excepiton::jubaexceptionを継承することを基本とする。
namespace jubatus {
class my_exception : public jubaexception<my_exception> { // CRTP
public:
};
} // jubatus
namespace jubatus {
void throw_exception_func() {
int fd = open("path/to/file",0);
if (fd == -1) {
throw JUBATUS_EXCEPTION(my_exception() << exception::error_message("hello")
<< exception::error_errno(errno)
<< exception::error_api_func("open"));
}
}
} // jubatus
try {
// code which throws
} catch (jubatus_exception& e) { // without const
// push additional information
e << JUBATUS_CURRENT_ERROR_INFO();
// rethrow exception
throw e;
}
exception_thrower_ptr thrower;
try {
// code which throws
} catch (jubatus_exception& e) { // without const
thrower = e.thrower();
}
// throw
if (thrower) {
thrower->throw_exception();
}
実行時型情報を利用する。
Streams の使用を許す。
Jubatusは64ビット標準とする。 ただし、32ビットの移植性も保つようにする。
Boostは使わない。 Boostの代わりにpficommonを用いる。
小文字とアンダースコアで表記する(スネークケース)。
// classes and structs
class url_table { ...
class url_table_tester { ...
struct url_table_properties { ...
// typedefs
typedef hash_map<UrlTableProperties *, string> properties_map;
// enums
enum url_table_errors { ...
kHogeHogeのようなkはprefixに付けない。 大文字・アンダースコアで表記する。
const int DAYS_IN_A_WEEK = 7;
引数は一行に納めるか、全部改行して書く。 改行後は関数にインデントをあわせず4つにする。
以下は良い例。 :: ReturnType ClassName::FunctionName(Type par_name1, Type par_name2) { DoSomething(); ... }
複数行の時は、必ず以下のように書く。 :
ReturnType LongClassName::ReallyReallyReallyLongFunctionName(
Type par_name1, // 4 space indent
Type par_name2,
Type par_name3) {
DoSomething(); // 2 space indent
...
}
以下の書き方は許されない。 :
ReturnType ClassName::ReallyLongFunctionName(Type par_name1, Type par_name2,
Type par_name3) {
DoSomething();
...
}
ポインタやリファレンスを宣言するときは、アスタリスクを型名につける。
// These are fine
char* c;
const string& str;
変数名につけてはならない。
// Bad
char *c;
const string &str;
変数定義などでスペースを加えたインデントを揃えない。 1行の追加、削除が他のコードの修正を強要してしまうこと、コミット時の変更も増えるからである。
以下は良い例。 :
int x = 1234;
int long_name = 5678;
以下のようにはしない。 :
int x = 1234; // Bad example
int long_name = 5678;
if の後に必ず {} をつけて、改行を行う。 1行の時に {} を省略したり、全体を1行で書いてはならない。
良い例 :
if (x == 1) {
do_something();
}
以下は悪い例。 :
if (x == 1)
do_something(); // Bad
if (x == 1) { do_something(); } // Bad
説明していることに対して、例及びコメントが逆のことを示している。以下のように直す。
変数名につけてはならない。