기초뿌셔

C++ 이름 규칙(Naming Rule) 정리

hayo_su 2024. 1. 12. 21:29

https://hayo-su.tistory.com/93

 

240112 C++ 백준 런타임에러(NZEC)

문제 발생) visual studio 2022에서 정상적으로 작동하며, 아무리 생각을 다시해봐도 코드 로직 자체에 문제가 없다고 생각되었다. 해결 과정) 1. 다른 블로그의 체크리스트를 확인해보았다. (나는 해

hayo-su.tistory.com

 

바보같은 이슈 이후 이름 규칙정리를 시작한다.

 

명명된 개체 유형, 변수, 함수, 상수, 매크로 등 어떤 종류인지 즉시 알 수 있도록 이름을 짓는 것(가독성)이 중요하다.

 

팀 프로젝트를 진행한다면 팀원과 Nameing Rule을 공유하여 팀 Nameing Rule을 만들고 프로젝트를 진행할 것이다.


표기법

파스칼 표기법

  • 모든 단어에서 첫 글자를 대문자로 쓰는 방식
  • ex) randommaigcweapon→ RandomMagicWeapon

 

카멜 표기

  • 첫 단어를 제외하고 나머지 단어의 첫 번째 글자만 대문자로 쓰는 방식
  • ex) RandomMagicWeapon → randomMagicWeapon

 

스네이크 표기법

  • 단어 사이에 underscore(_)를 활용하는 표기법
  • ex) username user_name 

  1. Class (타입) : 파스칼 표기법에 따른다
    // classes and structs
    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 { ...​
  2. Variable (변수) : 모두 소문자로 표기하며 스네이크 표기법을 사용한다 
    1. Common Variable (공통변수) 
      std::string table_name;  // OK - snake_case.​
    2. Class Data Members (클래스 맴버변수) : 전역변수 여부와 상관없이 단어 끝에 언더스코어(_)를 붙인다.
      class TableInfo {
        ...
       private:
        std::string table_name_;  // OK - underscore at end.
        static Pool<TableInfo>* pool_;  // OK.
      };​
    3. Struct Data Members (구조체 맴버변수)
      struct UrlTableProperties {
        std::string name;
        int num_entries;
        static Pool<UrlTableProperties>* pool;
      };​
  3. Constant (상수) : 앞에k를 붙히고 파스칼 표기법에 따른다.
     const absl::string_view kPrefix = "prefix";​
  4. Function  (함수) : 파스칼 표기법에 따른다.
    AddTableEntry() 
    DeleteUrl() 
    OpenFileOrDie()​
  5. Namespace  (네임스페이스)
    가능하면 사용하지 않고 명시적으로 표기한다.

  6. Enumerator (열거형) : 상수와 마찬가지로 k를 앞에 붙히고 파스칼 표기법에 따른다.
    enum class UrlTableError { 
      kOk = 0, 
      kOutOfMemory, 
      kMalformedInput, 
    };
  7. Macro (매크로)
    매크로는 가능하면 사용하지 않는다. 참고) https://google.github.io/styleguide/cppguide.html#Preprocessor_Macros 
    대신 const 변수를 사용하는 것을 권장한다.
    #undef이후에 사용한다면 모두 대문자프로젝트명 + 매크로이름 표기하며,
    언더스코어(_)를 사용하여 단어를 구분한다.

 

 

 

 

 

 

 

참고 : 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