Skip to content

Instantly share code, notes, and snippets.

@unnonouno
Last active December 11, 2015 20:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unnonouno/4655412 to your computer and use it in GitHub Desktop.
Save unnonouno/4655412 to your computer and use it in GitHub Desktop.
Jubatusコード規約

Jubatusコード規約

原則 Google C++ Style Guide に従います。 但し、以下の点に関して差分があります。

なお、外部から導入された jubatus/server/third_party 以下のファイルは対象外です。

Header Files

Scoping

Classes

Jubatus-Specific Magic =====================

Smart Pointers

pfi::lang::shared_ptr を用いる。

cpplint

tools/codestyle/cpplint/cpplint.py を利用する。

Other C++ Features

Reference Arguments

constのつかない通常の参照を許す。

Exceptions

例外を利用する。 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();
}

Run-Time Type Information (RTTI)

実行時型情報を利用する。

Streams

Streams の使用を許す。

64-bit Portability

Jubatusは64ビット標準とする。 ただし、32ビットの移植性も保つようにする。

Boost

Boostは使わない。 Boostの代わりにpficommonを用いる。

Naming

Type Names

小文字とアンダースコアで表記する(スネークケース)。

// 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 { ...

Constant Names

kHogeHogeのようなkはprefixに付けない。 大文字・アンダースコアで表記する。

const int DAYS_IN_A_WEEK = 7;

Comments

Formatting

Function Declarations and Definitions

引数は一行に納めるか、全部改行して書く。 改行後は関数にインデントをあわせず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();
  ...
}

Pointer and Reference Expressions

ポインタやリファレンスを宣言するときは、アスタリスクを型名につける。

// These are fine
char* c;
const string& str;

変数名につけてはならない。

// Bad
char *c;
const string &str;

Assign Values

変数定義などでスペースを加えたインデントを揃えない。 1行の追加、削除が他のコードの修正を強要してしまうこと、コミット時の変更も増えるからである。

以下は良い例。 :

int x = 1234;
int long_name = 5678;

以下のようにはしない。 :

int x         = 1234;  // Bad example
int long_name = 5678;

Braces

if の後に必ず {} をつけて、改行を行う。 1行の時に {} を省略したり、全体を1行で書いてはならない。

良い例 :

if (x == 1) {
  do_something();
}

以下は悪い例。 :

if (x == 1)
  do_something();  // Bad

if (x == 1) { do_something(); }  // Bad

Exceptions to the Rules

@suma
Copy link

suma commented Feb 6, 2013

Pointer and Reference Expressions

説明していることに対して、例及びコメントが逆のことを示している。以下のように直す。

// These are fine
char* c;
const string& str;

変数名につけてはならない。

// Bad
char *c;
const string &str;

@rimms
Copy link

rimms commented Jun 12, 2013

リポジトリの中にもコーディングルール適用外のソースがあるため、対象ソースを明記する(混乱を招かないためにも)。

具体的には以下のディレクトリ配下のソース。

master: src/third_party
develop: jubatus/server/third_party

@kmaehashi
Copy link

3 words 以上の単語は省略するルールについて、要追記。 jubatus/jubatus#257

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment