このドキュメントはオリジナルの Java コーディング標準を C++ 用に変更したものです.
オリジナル:http://www.objectclub.jp/community/codingstandard/CodingStd.pdf
フィードバックを歓迎します.
フィードバック先: fujiwo@shos.info
add/remove
insert/delete
get/set
start/stop
begin/end
send/receive
first/last
get/release
put/get
up/down
show/hide
source/target
open/close
source/destination
increment/decrement
lock/unlock
old/new
next/previous
//////////////////////////////////////////////////////////////////////////////
// 内容 ファイルの説明
// 作成・修正 2001/12/01 氏名
// 修正 2002/07/01 氏名 修正内容
// 著作権 会社名 2001-2002
//////////////////////////////////////////////////////////////////////////////
#include <iostream>
// クラスの説明
class Stack
{
public:
// 要素の追加
// @param item 追加する要素
void push(Object item) {
if (itemCapacity <= itemCount) {
// ...
} else {
// ...
}
}
// 先頭要素の取得.先頭要素は取り除かれる.
// @return 先頭要素
Object pop() {
// ...
return top;
}
};
double length = Math::sqrt(Math::pow(Math::random(), 2.0) + Math::pow(Math::random(), 2.0));
// 方針(1)
double xSquared = Math::pow(Math::random(), 2.0);
double ySquared = Math::pow(Math::random(), 2.0);
double length = Math::sqrt(xSquared + ySquared);
// 方針(2)
double length = Math::sqrt(Math::pow(Math::random(), 2.0,
Math::pow(Math::random(), 2.0);
// 方針(3)
return (this == obj
|| (this->parameter == obj->parameter
&& this->field == obj->field));
class LongNameClassImplemenation :
public AbstractImplementation,
public Serializable, public Cloneable
{
void longNameInternalIOMethod(int a, int b)
throw IOException {
// …
}
void longMethodSignature(int a, int b, int c,
int d, int e, int f) {
// …
}
// …
}
Stack stack;
stack.itemCount = 79; // ここで内部状態が崩れてしまう.
Point point;
point.x = 30; // 内部状態の整合性は崩れない.
class PoorInitialization
{
private:
static std::string name;
public:
PoorInitialization()
{
name = “initial_name”;
}
};
String PoorInitialization::name = "initial_name";
void f(int start) {
int i, j; // 初期値なしの宣言
// 多くのコード
// ...
i = start + 1;
j = i + 1;
// i, jを使う
}
void f(int start) {
// 多くのコード
// ...
// 使う前,はじめて宣言と初期化
int i = start + 1;
int j = i + 1;
// i, jを使う
}
void f(int N, int delta) {
int i; // 初期値なしの宣言
for (i = 0; i < N; i++) {
// iを使う
}
for (i = 0; i < N; i++) {// またiを使う
if (...) {
break;
}
}
if (i != N) { // 最後まで回ったかの判定にiを使っている
// ...
}
i = N - delta*2; // またまた再利用
// ...
}
void f(int N, int delta) {
for (int i = 0; i < N; i++) {
// iを使う
}
for (int i = 0; i < N; i++) {
// 別のiを使う
if (...) {
found = true;
break;
}
}
if (found) {
// ...
}
int total = N - delta*2; // 別の意味ある変数
// ...
}
void moveX(Point p, int dx) {
p.setX(p.getX() + dx); // 引数を変更している(なるべく避ける)
}
void moveX(Point p, int dx) {
p = Point(p.getX() + dx, p.getY());
// これは呼び出し側に伝わらない
}
void reset(int x_, int y_) {
x = x_;
y = y_;
}
void reset(int x, int y) { // 引数名を x_, y_ などとしない
this->x = x;
this->y = y;
}
/*
* 戦略:
* 1. まずnodeを探す
* 2. cloneする
* 3. inserterにcloneを追加要請
* 4. 成功したら,nodeを削除
*/
int index = -1; // -1はinvalidな値を意味する
参考: この方がさらによい.
static const int INVALID= -1;
int index = INVALID;
return cond == 0 ? a < b && b < c : d == 1;
良い例:return (cond == 0) ? ((a < b) && (b < c)) : (d == 1);
悪い例:
// 単位行列を作るが,時間もかかるし誰も読めない.
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
M[i-1][j-1] = (i/j)* (j/i);
Copyright © 1997-2008 Sho's Software