본문 바로가기
개발/스타일 가이드

C++ 코딩 스타일 가이드

by -=HaeJuK=- 2024. 3. 5.

변수 네이밍 규칙

  • int 변수: 접두사 n 사용 (int nExample)
  • string 변수: 접두사 ss 사용 (std::string ssExample)
  • 포인터 변수: 접두사 p 사용 (int* pExample)
  • HANDLE 변수: 접두사 h 사용 (HANDLE hExample)
  • vector 변수: 접두사 v 사용 (std::vector<int> vExample)
  • map 변수: 접두사 mp 사용 (std::map<int, std::string> mpExample)
  • bool 변수: 접두사 b 사용 (bool bExample)
  • null-terminated 변수: 접두사 sz 사용 (char* szExample)
  • CString 변수: 접두사 ms 사용 (CString msExample)

 

함수 인자 네이밍 규칙

함수 인자는 언더스코어와 접두사를 사용하여 명명 (int GetData(int _nData, int* _pData, std::string _ssData, std::wstring _ssDataW))

 

if문 포맷팅

if( data ) 
{ 
    // ...
} 
else if( condition ) 
{ 
    // ...
} 
else 
{ 
    // ...
}

명시적인 비교 사용:

if( false == data ) 
{ 
    // ...
}

 

enum 사용

enum class 사용, 접두사 e 사용 (enum class eRunOption { eAsUser, eAsAdmin };)

 

중괄호 { } 포맷

새 줄에 배치:

void Function()
{
    if( condition )
    {
        // ...
    }
}

 

파일 헤더 주석 블록

각 코드 예제는 다음과 같은 파일 헤더 주석 블록으로 시작:

/******************************************************************************
* 파일명: ClassName.cpp
* 작성자: 작성자 이름
* 설명: 이 파일은 ClassName 클래스의 구현을 포함합니다.
******************************************************************************/

 

헤더 가드

형식: #ifndef __HXC__CLASSNAME_H__ #define __HXC__CLASSNAME_H__ ... #endif // !__HXC__CLASSNAME_H__

#ifndef __HXC__CLASSNAME_H__
#define __HXC__CLASSNAME_H__

// 코드 내용

#endif // !__HXC__CLASSNAME_H__

 

클래스 네이밍

클래스 이름에 hxc 접두사 사용 (class hxcClassName)

 

예제

헤더 파일

/******************************************************************************
* 파일명: hxcExampleClass.h
* 작성자: 홍길동
* 설명: 이 파일은 hxcExampleClass 클래스의 선언을 포함합니다.
******************************************************************************/

#ifndef __HXC__EXAMPLECLASS_H__
#define __HXC__EXAMPLECLASS_H__

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <windows.h>
#include <atlstr.h> // CString 사용을 위해 필요

class hxcExampleClass
{
public:
    void ExampleFunction(int _nCount, double _dValue, bool _bFlag, int* _pData, std::string _ssName, CString _msCString, std::vector<int> _vNumbers, std::map<int, std::string> _mpData);
};

#endif // !__HXC__EXAMPLECLASS_H__

구현 파일

/******************************************************************************
* 파일명: hxcExampleClass.cpp
* 작성자: 홍길동
* 설명: 이 파일은 hxcExampleClass 클래스의 구현을 포함합니다.
******************************************************************************/

#include "hxcExampleClass.h"

void hxcExampleClass::ExampleFunction(int _nCount, double _dValue, bool _bFlag, int* _pData, std::string _ssName, CString _msCString, std::vector<int> _vNumbers, std::map<int, std::string> _mpData)
{
    if( false == _bFlag )
    {
        std::cout << "Flag is false" << std::endl;
    }

    if( nullptr != _pData )
    {
        std::cout << "Pointer is not null" << std::endl;
    }

    std::cout << "Count: " << _nCount << std::endl;
    std::cout << "Value: " << _dValue << std::endl;
    std::cout << "Name: " << _ssName << std::endl;
    std::wcout << L"CString Name: " << (LPCTSTR)_msCString << std::endl;

    for( const auto& number : _vNumbers )
    {
        std::cout << "Number: " << number << std::endl;
    }

    for( const auto& [key, value] : _mpData )
    {
        std::cout << "Key: " << key << ", Value: " << value << std::endl;
    }
}

int main()
{
    int nCount = 10;
    double dValue = 3.14;
    bool bFlag = false;
    int nData = 42;
    int* pData = &nData;
    std::string ssName = "Example";
    CString msCString = _T("CStringExample");
    std::vector<int> vNumbers = {1, 2, 3};
    std::map<int, std::string> mpData = {{1, "One"}, {2, "Two"}};

    hxcExampleClass example;
    example.ExampleFunction(nCount, dValue, bFlag, pData, ssName, msCString, vNumbers, mpData);

    return 0;
}
반응형