코딩 형식 지침.
원문출처: Design Guidelines, Managed code and the .NET Framework
1. 탭과 확장
Tab 문자(Ox09)는 사용하지 않으며, 확장은 공백(space) 문자 4개를 사용한다.
2. 중괄호
중괄호는 블락을 이끄는 구문의 아랫줄 처음에서 시작한다.
중괄호에 딸린 내용은 공백 문자 4개 이후에 기록한다.
if (someExpression)
{
DoSomething();
}
else
{
DoSomethingElse();
}
"case" 구문은 "switch" 구문 아래 다음처럼 작성된다:
switch (someExpression)
{
case 0:
DoSomething();
break;
case 1:
DoSomethingElse();
break;
case 2:
{
int n = 1;
DoAnotherThing(n);
}
break;
}
블락에 단 한 줄의 구문이 사용되더라도, 중괄호를 사용하도록 한다.
이는 코드의 가독성 및 유지 보수 효율을 높인다.
3. 단줄 구문
한 줄로 구성된 구문은 중괄호를 같은 줄에 함께 사용할 수 있다.
public class Foo
{
int bar;
public int Bar
{
get { return bar; }
set { bar = value; }
}
}
이는 중괄호를 사용하는 모든 구문(if, while, for, etc.)에 적용될 수 있다.
4.1 주석
주석은 개발 목표, 알고리즘 개관, 논리 흐름을 보이기 위해 사용되어야 한다.
주석 자체를 읽는 것만으로 함수의 목적, 동작, 일반 기능에 대한 이해를 할 수 있어야 한다.
4.2 판권 알림
각각의 파일은 판권에 대한 고지로 시작을 한다.
다음 예시의 문장은 제품에 따라 바뀔 수 있지만, 이와 유사한 형태여야 한다:
//-----------------------------------------------------------------------
// <copyright file="ContainerControl.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
4.3 기록 주석
주석 파일의 빌드 과정에서 오류를 피하기 위해, 삼중 슬래시를 사용하지 않을 수 있다.
하지만, XML의 사용은 추후 주석의 교환 등에서 편의를 제공한다.
특히 methods 관련 주석의 경우, XML doc 주석 형식을 사용한다.
지역적 dev 주석은 <devdoc> 태그를 사용한다.
public class Foo
{
/// <summary>Public stuff about the method</summary>
/// <param name=”bar”>What a neat parameter!</param>
/// <devdoc>Cool internal stuff!</devdoc>
///
public void MyMethod(int bar) { ... }
}
외부에 존재하는 XML 주석 파일을 삽입하려면, <include> 태그를 사용한다.
public class Foo
{
/// <include file='doc\Foo.uex' path='docs/doc[@for="Foo.MyMethod"]/*' />
///
public void MyMethod(int bar) { … }
}
4.4 주석 형식
이중 슬래시 형식은 대다수의 상황에서 사용된다.
가능하다면 주석의 위치는 코드의 옆보다는 위에 사용한다.
예시는 다음과 같다:
// This is required for WebClient to work through the proxy
//
GlobalProxySelection.Select = new WebProxy( ... );
// Create object to access Internet resources
//
WebClient myClient = new WebClient();
주석은 공간이 허용한다면, 줄의 마지막에 위치할 수도 있다:
public class SomethingUseful
{
private int itemHash; // instance member
private static bool hasDoneSomething; // static member
}
5. 공백(space) 문자의 사용
공백 문자는 코드 밀도를 감소시켜 가독력을 높여준다.
몇가지 지침을 소개한다:
ㆍ 함수 구문에 존재하는 따옴표 다음에는 하나의 공백 문자를 사용한다.
Right: Console.In.Read(myChar, 0, 1);
Wrong: Console.In.Read(myChar,0,1);
ㆍ 소괄호 시작과 끝에서는 공백 문자를 사용하지 않는다.
Right: CreateFoo(myChar, 0, 1);
Wrong: CreateFoo( myChar, 0, 1 );
ㆍ 소괄호와 함수 사이에서는 공백 문자를 사용하지 않는다.
Right: CreateFoo()
Wrong: CreateFoo ()
ㆍ 대괄호 안에서는 공백 문자를 사용하지 않는다.
Right: x = dataArray[index];
Wrong: x = dataArray[ index ];
ㆍ 흐름 제어 구문에서는 공백 문자 하나를 사용한다.
Right: while (x == y) { ... }
Wrong: while(x == y)
ㆍ 비교 명령 앞 뒤에서는 공백 문자 하나를 사용한다.
Right: if (x == y) { ... }
Wrong: if (x==y)
6. 작명
다음은 .NET Framwork에서 지역 및 전역 변수에 이름을 부여하는 방법이다.
핵심적인 것은 다음과 같다:
ㆍ 헝가리안 표기법을 사용하지 않는다.
ㆍ 멤버 변수를 위한 접두어(_, m_, s_, etc.)를 사용하지 않는다.
지역 변수와 전역 변수를 구분하고 싶다면,
C#에서는 "this."를 VB.NET에서는 "Me."를 사용한다.
ㆍ 멤버 변수는 camel 표기법을 사용한다.
ㆍ 매개 변수는 camel 표기법을 사용한다.
ㆍ 지역 변수는 camel 표기법을 사용한다.
ㆍ 함수, 클래스, 이벤트 등은 파스칼 표기법을 사용한다.
참고)
파스칼식 대/소문자:
식별자의 첫 번째 문자와, 연결된 각 후속 단어의 첫 번째 문자를 대문자로 표기한다.
예) BackColor
카멜식 대/소문자:
식별자의 첫 번째 문자는 소문자로 표기한다.
연결된 각 후속 단어의 첫 번째 문자는 대문자로 표기한다.
예) backColor
ㆍ 인터페이스 이름에는 접두어 "I"를 사용한다.
ㆍ enum, class 에서 접두어를 사용하지 않는다.
이는 일관성 있고 가독성 높은 코드 생산을 위하여, 알려진 지침을 확장시킨 것이다.
7. 작명 규약
7.1 Interop Classes
DllImport 구문을 가진 클래스들은, 다음과 같은 작명 규약을 따라야 한다:
ㆍ NativeMethods: No suppress unmanaged code attribute, these are methods that can be used anywhere because a stack walk will be performed.
class NativeMethods
{
private NativeMethods() { ... }
[DllImport(“user32”)]
internal static extern void FormatHardDrive(string driveName);
}
ㆍ UnsafeNativeMethods: Has suppress unmanaged code attribute. These methods are potentially dangerous and any caller of these methods must do a full security review to ensure that the usage is safe and protected as no stack walk will be performed.
[SuppressUnmanagedCode]
class UnsafeNativeMethods
{
private UnsafeNativeMethods() { ... }
[DllImport(“user32”)]
internal static extern void CreateFile(string fileName);
}
ㆍ SafeNativeMethods: Has suppress unmanaged code attribute. These methods are safe and can be used fairly safely and the caller isn’t needed to do full security reviews even though no stack walk will be performed.
[SuppressUnmanagedCode]
class SafeNativeMethods
{
private SafeNativeMethods() { ... }
[DllImport(“user32”)]
internal static extern void MessageBox(string text);
}
모든 interop 클래스는 private 이어야 하며, 모든 method는 지역적이어야 한다.
또한 모든 private 생성자는 인스턴스 생성을 예방한 후 제공되어야 한다.
8. 파일 구성
ㆍ각각의 소스 파일에는 하나의 public 클래스만 담는다.
ㆍ소스 파일의 이름에는 내부 public 클래스의 이름이 담겨있어야 한다.
ㆍ디렉토리 이름은 각 클래스의 namespace를 따른다.
예를 들어, "System.Windows.Forms.Control"라는 public 클래스를
"system\Windows\Forms\Control.cs"라는 디렉토리에서 찾으려 할 때,
ㆍ클래스 멤버는 알파벳 순서로 정렬되어 있어야 하며, 부문별로 그룹지어져 있어야 한다.
(Fields, Constructors, Properties, Events, Methods, Private interface, etc.)
ㆍ사용된 구문은 namespace 정의에 존재하여야 한다.
namespace MyNamespace
{
using System;
public class MyClass : IFoo
{
// fields
int foo;
// constructors
public MyClass() { ... }
// properties
public int Foo { get { ... } set { ... } }
// events
public event EventHandler FooChanged { add { ... } remove { ... } }
// methods
void DoSomething() { … }
void FindSomethind() { … }
//private interface implementations
void IFoo.DoSomething() { DoSomething(); }
// nested types
class NestedType { ... }
}
}
'SAME OLD SHIT' 카테고리의 다른 글
| 코딩 형식 지침. (0) | 2009/06/15 |
|---|---|
| 상옥이. (0) | 2009/06/09 |
| 8 3 10. (0) | 2009/06/08 |
| Nocturne Op.9 No.2, Yundi Li. (0) | 2009/06/05 |
| 그리고 당신들에게 전하는 글. (0) | 2009/05/29 |
| 당신을 위해 글을 쓰려 했습니다. (0) | 2009/05/29 |



