Navigation Menu

Skip to content

Commit

Permalink
doc coding-style: add about number in condition
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Mar 15, 2012
1 parent 3d65ee6 commit 8266156
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions doc/source/developer/coding_style.rst
Expand Up @@ -467,3 +467,21 @@ bool型を用いる
char *name = NULL;
if (name == NULL) { ... }
数値は比較する
^^^^^^^^^^^^^^

CやC++では ``0`` は偽、 ``0`` 以外は真の値となるが、条件式に数値を使う場合は ``strcmp(...) == 0`` などというように明示的に比較する。

C++では真偽値に ``bool`` を使うためこのような状況は発生しないが、C言語由来のAPIでは ``int`` で真偽値を表現している場合が多い。しかし、 ``int`` だけでは真偽値として使っているか本当に数値として使っているかがわかりにくいため、 ``int`` のときはすべて数値として扱う。

よい例:

if (memcmp(value1, value2, value_size) == 0) {
printf("same value!\n");
}

悪い例( ``0`` を偽の値として扱っている):

if (!memcmp(value1, value2, value_size)) {
printf("same value!\n");
}

0 comments on commit 8266156

Please sign in to comment.