Skip to content

コメント

ビジネスロジックが複雑な部分にのみコメントを記載してください

コメントは言い訳であって必須要件ではありません。優れたコードはほとんどの場合自己文書化します

悪い例:

javascript
function hashIt(data) {
  // The hash
  let hash = 0;

  // Length of string
  const length = data.length;

  // Loop through every character in data
  for (let i = 0; i < length; i++) {
    // Get character code.
    const char = data.charCodeAt(i);
    // Make the hash
    hash = (hash << 5) - hash + char;
    // Convert to 32-bit integer
    hash &= hash;
  }
}

良い例:

javascript
function hashIt(data) {
  let hash = 0;
  const length = data.length;

  for (let i = 0; i < length; i++) {
    const char = data.charCodeAt(i);
    hash = (hash << 5) - hash + char;

    // Convert to 32-bit integer
    hash &= hash;
  }
}

コードベースにコメントアウトされたコードを残さないでください

バージョン管理を使う意味を考えましょう。古いコードは履歴に残すべきであり、現在のコードベースに残す必要はありません

悪い例:

javascript
doStuff();
// doOtherStuff();
// doSomeMoreStuff();
// doSoMuchStuff();

良い例:

javascript
doStuff();

変更履歴コメントを残さないでください

バージョン管理システムを活用しましょう。デッドコード、コメントアウトされたコード、特に変更履歴コメントは不要です。履歴確認にはgit logを使用してください

悪い例:

javascript
/**
 * 2016-12-20: Removed monads, didn't understand them (RM)
 * 2016-10-01: Improved using special monads (JP)
 * 2016-02-03: Removed type-checking (LI)
 * 2015-03-14: Added combine with type-checking (JR)
 */
function combine(a, b) {
  return a + b;
}

良い例:

javascript
function combine(a, b) {
  return a + b;
}

位置マーカーの使用を避けてください

これらは視覚的なノイズの原因になります。関数名や変数名の適切な命名、適切なインデントとフォーマットにより、コード構造を視覚的に表現しましょう

悪い例:

javascript
////////////////////////////////////////////////////////////////////////////////
// Scope Model Instantiation
////////////////////////////////////////////////////////////////////////////////
$scope.model = {
  menu: "foo",
  nav: "bar"
};

////////////////////////////////////////////////////////////////////////////////
// Action setup
////////////////////////////////////////////////////////////////////////////////
const actions = function() {
  // ...
};

良い例:

javascript
$scope.model = {
  menu: "foo",
  nav: "bar"
};

const actions = function() {
  // ...
};