CString STR_COMMA(long  i)
{
    BOOL bMinus = FALSE;
    if (i < 0)
    {
        i = i * (-1);
        bMinus = TRUE;
    }
    
    CString str;
    str.Format(_T("%d"),i);

    int nLength=str.GetLength();
    int j=0,k=0;
    for(j=nLength;j>3;j-=3)
        str.Insert(j-3,_T(","));

    if(bMinus)
        str.Insert(0,_T("-"));
    
    return str;
}
Posted by 모과이IT
,

팀 프로젝트를 수행할때 필요한것 중 하나가..
서로의 변수명과 함수명등을 결정하는 것이다.
팀 내의 규칙에 다라서 결정하는 방법도 있고..
그냥 자신의 습관에 따라 결정하는 방법도 있겠지만..

그래도 추후 타 독자의 이해를 도우면서 가독성을 높일 수 있는 방법이 가장 좋지 않을까?
그런 의미에서 헝가리안 표기법은 많은 도움을 줄 수 있다.
헝가리 태생 MS의 연구원 Charles Simonyi 가 고안한 것으로 알려져 있다.

현재 인터넷에 너무나 많은 관련 글 들이 있어.. 어디가 원본인지.. 찾아 보기 어려워..
몇군데의 내용을 모아서 정리를 해보고자 한다.. 거의 글어다 붙이기 정도지만... ㅎㅎㅎ

 

헝가리안 표기법의 목적은 개체에 대한 정보를 간결하고 효율적으로 전달하는 것이다.


헝가리안 표기법은 익숙해지려면 시간이 조금 걸리지만, 한번 익숙해지면, 그 다음부터는

아주 자연스럽게 되어 버린다.

헝가리안식 개체 이름의 형식은 다음과 같다.


[접두어] 태그 [기본이름 [접미어] ]


  * 꺾쇠괄호([])는 개체 이름에 있어서 선택적 사항임을 나타낸다.


다음은 각 항목에 대한 설명이다.


접두어 
 부가적인 의미를 나타내기 위해 태그를 보조한다. 접두어는 모두 소문자이다.
 이들은 대개 이 문서에 나오는 표준적인 접두어들 중에서 선택된다. 
 
태그
 문자들의 준말로서 그 개체의 형식을 나타낸다. 모두 소문자로 이루어지고
 마찬가지로 이 문서에 나오는 표준적인 태그들 중에서 선택된다.
 
기본이름
 개체가 무엇을 나타내는 지를 알려주는 한 두 개의 단어.
 각 단어의 첫 문자는 대문자로 만든다.
 
접미어
 기본이름의 의미를 부수적으로 도와준다. 접미어의 각 단어의 첫 문자는
 대문자로 만든다. 그리고 접미어는 이 문서에서 나오는 표준적인 접미어들
 중에서 선택된다.
 

개체 이름으로서 필수적으로 요구되는 항목은 태그이다.
이것은 조금 이상하게 보일 수 있다; 여러분은 기본이름이 개체 이름 중에 가장
중요한 요소라고 느낄 것이다. 하지만, 임의의 폼 상의 한 프로시저를 생각해 보자.
이 프로시저가 중요하긴 하지만, 그 폼 전체를 나타내는 것은 아니다.
이 프로시저가 다른 많은 형식의 폼 상에서 작동될 수 있기 때문에, 여러분이
필수적으로 기본이름을 가져야만 하는 것은 아니다.
어쨌든, 만일 여러분이 프로시저 내에서 하나 이상의 참조되는 개체를 가지고 있다면,
그들을 구분하기 위해 기본이름이 필요하다.


실 생활에서 사용되는 헝가리안 표기법은 다음과같다


a           배열
b           또는 f BOOL형 변수(b는 "bool",f는 "flag"의 약자)
by         BYTE(unsigned char)형 변수
c           카운터로 사용되는 변수
ch         char형 변수
cx,cy     x,y길이를 나타내기 위해 사용되는 변수
d           날짜형 변수
dbl        double형 변수
h          핸들(HANDLE)형 변수
n 또는 i   int형 변수
l            long형 변수
p          포인터 변수
lp          long(far)포인터 변수(32비트 프로그래밍에서는 일반 포인터와 같음)
s          문자열
sz        널(NULL)로 끝나는 문자열
u          unsigned int형 변수
w          WORD(unsigned short)형 변수
dw        DWORD(unsigned long)형 변수
str        CString형 변수
m_       멤버 변수

----------------------------------------------------
x_xXxxxxxx
0123456789


0 : 변수의 위치를 지정한다. g(전역변수), m(멤버변수), 없음(지역변수)
1 : 0 위치에 g 나 m 을 지정한 경우 _ 을 기술한다.
2 : 자료형의 종류를 나타낸다.


   n, i : 정수형(n 은 카운트 목적, i 는 인덱스 목적)
   l : long 형 (과거 int 와 구분하기 위해서 사용했다.)
   u : 부호없는 정수형
   w : 부호없는 2 byte 정수형
   dw : 부호없는 4 byte 정수형
   p : 포인터 타입
   f, d : 실수형(f 는 float, d 는 double)
   sz : char 배열(문자열 표현)
   클래스 이름에 대해서는 관습적으로 자음축약형을 사용한다.

3 ~ : 변수의 의미 있는 이름을 기술하며, 3 위치는 대문자를 사용한다. 변수 이름이 너무 긴 경우 자음만을 기술한다.
예) g_nCnt
_______________________________________________________________________________

사용자 삽입 이미지

Table 1. Some examples for procedure names

Name

Description

InitSy

Takes an sy as its argument and initializes it.

OpenFn

fn is the argument. The procedure will "open" the fn. No value is returned.

FcFromBnRn

Returns the fc corresponding to the bn,rn pair given. (The names cannot tell us what the types sy, fn, fc, and so on, are.)



Table 2. Standard type constructions

pX

Pointer to X.

dX

Difference between two instances of type X. X + dX is of type X.

cX

Count of instances of type X.

mpXY

An array of Ys indexed by X. Read as "map from X to Y."

rgX

An array of Xs. Read as "range X." The indices of the array are called:

iX
index of the array rgX.

dnX

(rare) An array indexed by type X. The elements of the array are called:

eX


(rare) Element of the array dnX.

grpX

A group of Xs stored one after another in storage. Used when the X elements are of variable size and standard array indexing would not apply. Elements of the group must be referenced by means other then direct indexing. A storage allocation zone, for example, is a grp of blocks.

bX

Relative offset to a type X. This is used for field displacements in a data structure with variable size fields. The offset may be given in terms of bytes or words, depending on the base pointer from which the offset is measured.

cbX

Size of instances of X in bytes.

cwX

Size of instances of X in words.



Table 3. Standard qualifiers

XFirst

The first element in an ordered set (interval) of X values.

XLast

The last element in an ordered set of X values. XLast is the upper limit of a closed interval, hence the loop continuation condition should be: X <= XLast.

XLim

The strict upper limit of an ordered set of X values. Loop continuation should be: X < XLim.

XMax

Strict upper limit for all X values (excepting Max, Mac, and Nil) for all other X: X < XMax. If X values start with X=0, XMax is equal to the number of different X values. The allocated length of a dnx vector, for example, will be typically XMax.

XMac

The current (as opposed to constant or allocated) upper limit for all X values. If X values start with 0, XMac is the current number of X values. To iterate through a dnx array, for example:

for x=0 step 1 to xMac-1 do ... dnx[x] ...

or

for ix=0 step 1 to ixMac-1 do ... rgx[ix] ...

XNil

A distinguished Nil value of type X. The value may or may not be 0 or -1.

XT

Temporary X. An easy way to qualify the second quantity of a given type in a scope.



Table 4. Some common primitive types

f

Flag (Boolean, logical). If qualifier is used, it should describe the true state of the flag. Exception: the constants fTrue and fFalse.

w

Word with arbitrary contents.

ch

Character, usually in ASCII text.

b

Byte, not necessarily holding a coded character, more akin to w. Distinguished from the b constructor by the capital letter of the qualifier in immediately following.

sz

Pointer to first character of a zero terminated string.

st

Pointer to a string. First byte is the count of characters cch.

h

pp (in heap).



- 참조 -

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvsgen/html/HungaNotat.asp

http://web.archive.org/web/20001018073323/www.strangecreations.com/library/c/naming.txt

http://ttongfly.net/bbs/view.php?id=windowsc&page=1&sn1=&divpage=1&sn=off&ss=on&sc=on&select_arrange=headnum&desc=asc&no=2

http://blog.naver.com/shadovv13?Redirect=Log&logNo=50012280887

http://blog.haz3.com/tag/%C7%EB%B0%A1%B8%AE%BE%C8%20%C7%A5%B1%E2%B9%FD


Posted by 모과이IT
,

내 맘대로 정한 헝가리안 표기법... 

MS의 MFC와 WTL에 사용된 코딩 규칙을 대부분 적용 하였다.


약간 변형 시키긴 했다만...

 

이런 규칙을 정하는 이유는 코딩 또한 하나의 문서라고 보는 철학에서 나왔다.

문서란 작성하는 사람위주가 아니라. 보는 사람 위주로 만들게 된다.

 

일관된 패턴과,

보는 사람관점의 '파악하기 쉬운'(?)

이라는 철학으로 코딩규칙을 정하고 개발 하게 되면

 

팀프로젝트에서 다른 팀원이 개발한 코드, 또는 몇년이 지나고 자신이 개발한 코드를 볼때도 빠르게 파악하게 된다.

 

아래 규칙은 "코드를 파악하기 쉬운 규칙"이라는 단 하나의 기준 만으로 정해 놨다.

 

불합리 하다고 생각되시는 분은 리플 달아 주심 감사 하겠습니다.

 

Prefix

Type

Example

dw 

double word

dwTotal

w

WORD

wNumber

l

long

lTotal

i

Integer

iNumber

f

float

fAvarage

b

bool

bCheck

n

short

nCount

by

BYTE

byOffset

ch

CHAR

chKey

sz

CHAR[] Null Terminatied

szFlag

sz

WCHAR[] Null Terminatied

szPin

C

Class

CButton 

s

static

sHelper

p

Pointer

pButton

str

CString 

strAddress

r

RECT

rDraw

a

Array

aBags

g_

Global Val

g_bShow

m_

Member Val

m_iMans

si

size_t

siCount

 

 

 

vec

vector

vecMan

map

map

mapWoman

lst

list

lstHome

hsh

hash

hshHome

h

handle

hWindow

 

 

 

 

Original

Type

Example

unsigned char

BYTE

byOffset

unsigned short

WORD

nCount

unsigned long

DWORD

dwTotal

long

LONG

lNumber

int

int

iNumber

BYTE

BOOLEAN

bChecked

WCHAR *

LPWSTR

pszCaption

const WCHAR *

LPCWSTR

pcszCaption

LPCWSTR

LPCTSTR

pcszCaption

char *

LPSTR

pszCaption

const char *

LPCSTR

pcszCaption

 

 

1. 변수의 접두어는 모두 소문자

   ex) m_pCheckBox, m_CheckBox, g_nTotal, g_SessionManager

         nCount, strCaption, pListBox


2. 변수에서 접두어가 없는 경우 첫 단어는 소문자로 시작

   ex) m_checkBox, g_total, g_memberCount; m_memberList;

        로컬 변수일경우:  checkbox, item, 


3. 함수의 첫글자는 대문자

   DestroySession(), Connect(), Close(), ViewMap(),


4. 함수의 이름은 동사+명사

   GotoURL(), ViewMap(), PlayMovie(), ParseURL()


5. Class는 접두어를 제외한 첫번째는 대문자

   CCaption, CMembers, CResourcePaint, CFileStream


6. 여러 단어가 합쳐 졌을때는 각 단어의 시작은 모두 대문자

   ConnectToTheServer(), SendToMetaData() g_nMetaDataCount


7. Typedef, #define, enum, union등의 type은 모두 대문자

typedef enum {
    T_1,
    T_2,
    T_3
}TEST_DATA;

단 struct와 union의 맴버는 m_를 붙이지 않으며 모두 대문자를 사용하지 않고 위의 표기법으로 사용한다.
맴버는 맴버이지 하나의 type이 아니기 때문이다.

8. if문 및 switch등 제어문의 표기법

   if( i = 1 )

   {           <== 반드시 개행시켜서 표현한다.

....

   }

 

   for( i = 0; i < 100 && !myIterator.empty() ; i++ )   {

      do {

          if( a == b ) {


          }

     

          switch( nCount ) {

        

          }

       }while( nCount < 100 )

   }


   이런 식의 사용은 if문 및 switch문에서 문장의 전체 범위가 한번에 들어 오지 않는다.  

   따라서 개행해서 코딩 하도록 한다.

  

   for( i = 0; i < 100 && !myIterator.empty() ; i++ )  

   {

       do

       {

           if( a == b )

           {


            }

     

           switch( nCount )

           {

        

           }

       } while( nCount < 100 )

    }



   switch( nCount )

   {

    case CNT_FIRST:

            {

            }

            break;

   }


9. 문단으로 구분한다.

   하나의 함수 안에서도 처리 하는 부분들이 구분 될 수 있다. 이럴때는 아래위 공백을 하나씩 사용해서 구분 해준다.


10. 제어문에서 ( )는 본문과 한칸씩 띄워준다.

   위의 switch나 if, while문등이 명령어 다음 바로 (가 붙고 조건 맨끝에서 한칸뒤에 )가 붙어 있다.

   이것또한 조건과 명령어등이 명확히 보이도록 하고 내부에서 ()가 사용되었을 경우 정확히 끝이 어디인지 한눈에 알아보기 쉽기 위해서 

   다

  1.  if (nTotal < 1000 && myIterator !=  m_vecStudent.end())


  2.  if( nTotal < 1000 && myIterator !=  m_vecStudent.end() )  


   두개의 문장중 어떤것이 조건의 시작과 끝을 보기 편한가?


11. Class와 함수의 윗줄에 구분줄을 표시 한다.

   이 규칙은 Borland C++이나 Delphi에 있던 규칙인데 이것을 사용하게 되면 함수의 구분과 class의 영역 구분 또한 명확해 진다.

   만약 Doxygen이나 JavaDoc 같은 주석을 사용한다면 명확히 구분이 되기 때문에 사용하지 않아도 된다.

 

ex)   아래 코드는 즉석해서 만들어낸 예제 코드임으로 코드적 결함이 있을 수 있다.

//--------------------------------------------------------------------------------------------------------

class CBookmark

{

private:

 

public:

       CBookmark();

       ~CBookmark();
}  

 

//--------------------------------------------------------------------------------------------------------

class CInputBox

{

private:

 

public:

       CInputBox();

       ~CInputBox();
}  

 

//--------------------------------------------------------------------------------------------------------

class CViewMap

{

private:

 

public:

       CViewMap();

       ~CViewMap();
}  

 

//--------------------------------------------------------------------------------------------------------

void CViewMap::StopRandering(CParent * pParent)

{

      if( pParent->m_bsState == BS_RUNNING )

      {

            .

            .

       }

     

     pParent->Cancel();

}

 

//--------------------------------------------------------------------------------------------------------

BOOL CViewMap::BeginRandering(CParent * pParent)

{
.

.

}

 

//--------------------------------------------------------------------------------------------------------

CHandle* CViewMap::InitRanderer(CParent * pParent)

{

.

.

}

 


Posted by 모과이IT
,