본문 바로가기
프로그래밍/C#

C# 괄호 알아보기

by bantomak 2023. 3. 20.
반응형

괄호 종류

  한글 명칭 영어 명칭
() 소괄호 Round Brackets 또는 Parentheses
[] 대괄호 Square Brackets 또는 Brackets
{} 중괄호 Curly Brackets 또는 Braces
<> 홑화살괄호 Angle Brackets 또는 Chevrons

복합문에서의 중괄호 배치 (들여쓰기 스타일)

중괄호는 사이에 문장이 입력되기 때문에 간격이 많이 벌어지게 된다. 그래서 이에 따른 많은 코딩 스타일이 존재하게 된다. 개인적으로는 Allman 스타일을 선호한다.

중괄호 배치 스타일
while (x == y)
{
    something();
    somethingelse();
}
Allman
while (x == y) {
    something();
    somethingelse();
}
K&R
while (x == y)
  {
    something ();
    somethingelse ();
  }
GNU
while (x == y)
    {
    something();
    somethingelse();
    }
Whitesmith
while (x == y)
{   something();
    somethingelse();
}
Horstmann
while (x == y)
{   something();
    somethingelse(); }
Pico
while (x == y) {
    something();
    somethingelse();
    }
Ratliff
while (x == y)
  { something();
    somethingelse(); }
Lisp
#define W(c,b) {while(c){b}}
W(x==y,s();se();)
APL

Allman 스타일의 특징

중괄호의 위치

  • 여는 중괄호( { )는 항상 새 줄에 위치
  • 닫는 중괄호 ( } ) 는 여는 중괄호와 같은 들여쓰기 레벨에서 새 줄에 위치

가독성

  • 여는 중괄호가 새 줄에 위치하므로 코드 블록의 시작과 끝을 명확해져 구분하기 쉽다.
  • 계층 구조가 시각적으로 더 명확해져 복잡한 코드에서도 가독성을 높일 수 있다.

K&R 스타일의 특징

중괄호 위치

  • 여는 중괄호( { )가 조건문, 루프 또는 함수 선언과 같은 줄에 배치된다.
  • 닫는 중괄호 ( } ) 는 여는 중괄호와 같은 들여쓰기 레벨에 위치

간결함

  • 한 줄에 여는 중괄호를 배치하여 코드가 더 짧고 간결하게 보인다.
  • 더 많은 코드를 한 화면에 표시할 수 있다.
  • 많은 언어 및 프로젝트(특히 C와 Linux 커널)에서 기본 스타일로 사용되므로 익숙한 개발자가 많다.

참고하면 좋은 글

 

Indentation style - Wikipedia

From Wikipedia, the free encyclopedia Computer programming convention In computer programming, indentation style is a convention, a.k.a. style, governing the indentation of blocks of source code. An indentation style generally involves consistent width of

en.wikipedia.org

 

Code style

Code style. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

댓글