이것저것

C++ 명명법 (C++ Naming Style) 본문

스터디/C & C++

C++ 명명법 (C++ Naming Style)

호준 2022. 8. 28. 16:48

이 글은 아래의 구글 C++ 스타일 가이드의 명명법을 번역, 요약한 것이다. 꼭 이렇게 변수이름을 지어야한다는 법은 없다.

https://google.github.io/styleguide/cppguide.html#Naming

 

Google C++ Style Guide

Google C++ Style Guide Background C++ is one of the main development languages used by many of Google's open-source projects. As every C++ programmer knows, the language has many powerful features, but this power brings with it complexity, which in turn ca

google.github.io

 

이름은 코드에서 객체가 타입인지 변수인지 함수인지 등등을 즉각적으로 알려줄 수 있는 하나의 방법이다. 이름은 다른 팀의 사람이 보았을 때도 이해할 수 있도록 가독성이 좋아야한다. 코드가 길어지는 것을 걱정하지 말고 코드가 다른 사람들에게 이해하기 쉽도록 만들도록 하자. 단어의 특정 글자를 삭제하는 등의 약어는 최대한 줄이도록 하자. 위키피디아에서 찾아볼 수 있는 정도의 약어만 사용하도록 하자.

 

또한 객체가 사용되는 스코프를 생각하여 너무 모호하지도 않고 너무 자세하지도 않게 이름을 짓도록 하자.

예를 들어....

// 좋은 예
class MyClass {
 public:
  int CountFooErrors(const std::vector<Foo>& foos) {
    int n = 0;  // 함수 스코프 내에서 충분히 이해할 수 있는 변수명
    for (const auto& foo : foos) {
      ...
      ++n;
    }
    return n;
  }
  void DoSomethingImportant() {
    std::string fqdn = ...;  // 잘 알려진 약어(fqdn : Fully Qualified Domain Name)
  }
 private:
  const int kMaxAllowedConnections = ...;  // 문맥상 의미가 명확
};

class MyClass {
 public:
  int CountFooErrors(const std::vector<Foo>& foos) {
    int total_number_of_foo_errors = 0;  // 함수 스코프 내에서는 너무 자세한 이름
    for (int foo_index = 0; foo_index < foos.size(); ++foo_index) {  // 반복문에서 자주 사용되는 i를 사용해야함
      ...
      ++total_number_of_foo_errors;
    }
    return total_number_of_foo_errors;
  }
  void DoSomethingImportant() {
    int cstmr_id = ...;  // 단어 내 글자를 삭제해서 이해하기 어려움
  }
 private:
  const int kNum = ...;  // 클래스 스코프 내에서 이해하기 어려운 이름
};

 

각 객체마다 명명 규칙은 아래와 같다.

 

타입

타입의 이름은 대문자로 시작하고 각단어의 시작마다 대문자를 사용한다. Underscore( _ )는 사용하지 않는다.

예시)

// 클래스, 구조체 이름
class UrlTable { ...
class UrlTableTester { ...
struct UrlTableProperties { ...

// typedefs
typedef hash_map<UrlTableProperties *, std::string> PropertiesMap;

// using aliases
using PropertiesMap = hash_map<UrlTableProperties *, std::string>;

// enums
enum class UrlTableError { ...

 

변수

변수의 이름(파라미터 포함)은 항상 소문자를 사용하고 단어의 사이에는 underscore( _ )를 사용한다.

클래스의 멤버변수 끝에도 underscore를 사용한다.

// 일반적 변수명
string table_name; // 좋은 예
string tableName; // 나쁜 예

// 클래스 멤버변수명
class TableInfo {
  ...
 private:
  std::string table_name_;  // 끝에 underscore를 붙임
  static Pool<TableInfo>* pool_;
};

// 구조체 멤버변수명
struct UrlTableProperties {
  std::string name; // 끝에 underscore를 붙이지 않음
  int num_entries;
  static Pool<UrlTableProperties>* pool;
};

 

상수

상수의 이름은 항상 k로 시작하고 단어의 시작마다 대문자를 사용한다. 대문자로 단어를 구분하기 어려울때는 underscore를 사용할 수 있다.

const int kDaysInAWeek = 7;
const int kAndroid8_0_0 = 24;  // Android 8.0.0

 

함수

함수의 이름은 대문자로 시작하고 당어의 시작마다 대문자를 사용한다. (타입의 명명법과 같다.)

AddTableEntry()
DeleteUrl()
OpenFileOrDie()

 

네임스페이스

네임스페이스는 항상 소문자로 작성하고 단어 사이에는 underscore를 사용한다.

 

Enumerator

Enumerator는 상수의 이름처럼 항상 k로 시작하고 담어의 시작마다 대문자를 사용한다.

// 좋은 예
enum class UrlTableError {
  kOk = 0,
  kOutOfMemory,
  kMalformedInput,
};

// 나쁜 예
enum class AlternateUrlTableError {
  OK = 0,
  OUT_OF_MEMORY = 1,
  MALFORMED_INPUT = 2,
};

 

매크로

매크로는 왠만하면 쓰지 말도록 하자. 꼭 써야한다면 아래와 같이 항상 대문자를 사용하고 단어 사이에는 underscore를 사용한다.

#define ROUND(x) ...
#define PI_ROUNDED 3.0

 

'스터디 > C & C++' 카테고리의 다른 글

float 숫자 표현  (0) 2021.08.20