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() {
  // ...
};