Java Coding Convention
자바 코딩 컨벤션
- 자바 소스 코드 작성에 대해 관례적으로 통용되는 스타일에 대해 소개한다.
* 본 포스트에서는 Google에서 소개하는 컨벤션을 참고했다. (URL)
Google Java Style Guide
1 Introduction This document serves as the complete definition of Google's coding standards for source code in the Java™ Programming Language. A Java source file is described as being in Google Style if and only if it adheres to the rules herein. Like ot
google.github.io
1 Introduction
1.1 Terminology notes
1.2 Guide notes
2 Source file basics
2.1 File name
2.2 File encoding: UTF-8
2.3 Special characters
3 Source file structure
3.1 License or copyright information, if present
3.2 Package statement
3.3 Import statements
3.4 Class declaration
4 Formatting
4.1 Braces
4.2 Block indentation: +2 spaces
4.3 One statement per line
4.4 Column limit: 100
4.5 Line-wrapping
4.6 Whitespace
4.7 Grouping parentheses: recommended
4.8 Specific constructs
5. Naming (이름 명명법)
5.1 Rules common to all identifiers (모든 식별자들의 공통 명명법)
- 모든 Indentifier들은 반드시 ASCII 문자와 숫자, Underscore(_)로만 구성된다.
- Valid Indentifier의 이름을 Regular Expression으로 표현하면 아래와 같다:
\(Ww+\)
- Google에서는 name_
, mName
, s_name
, kName
과 같은 Special Prefix/Suffix를 사용하지 않는다.
5.2 Rules by identifier type (식별자 타입별 명명법)
Rule | Description | Valid Example |
Package Names | - 소문자와 숫자로만 명명한다. - 단어들이 연속될 경우, 그냥 잇는다. |
com.example.deepspace |
Class Names | - UpperCamelCase 로 명명한다. - 명사 혹은 명사구로 명명한다. - Interface의 경우, 명사/명사구 혹은 형용사/형용사구로 명명한다. - Test Class의 경우, Test로 끝나도록 명명한다. - Test Class가 오직 하나의 클래스만을 커버한다면, 해당 클래스의 이름 뒤에 Test를 붙여 명명한다. |
Character ImmutableList (Class) Readable (Interface) HashIntergrationTest (Test Class) |
Method Names | - lowerCamelCase 로 명명한다. - 동사 혹은 동사구로 명명한다. - Underscore(_)는 JUnit test Method를 명명할 때, Logical Component들을 구분짓는 용도로 사용할 수 있다. |
sendMessage stop (General Method) transferMoney_deductsFromSource (JUnit test Method) |
Constant Names | - UPPER_SNAKE_CASE 로 명명한다. - 명사 혹은 명사구로 명명한다. - Constant란, Deeply Immutable하고 Method들이 영향을 주지 못하는 Static Final Field를 의미한다. (primitive, string, Immutable Value Class, null 등) |
NUMBER NAMES EMPTY_ARRAY COMMA_JOINER |
Non-Constant Field Names | - lowerCamelCase 로 명명한다. - 명사 혹은 명사구로 명명한다. |
computedValues index |
Parameter Names | - lowerCamelCase 로 명명한다. - Public Method에서 Parameter의 이름을 하나의 글자로 명명하는 것은 특히 피해야 한다. |
|
Local Variable Names | - lowerCamelCase 로 명명한다. - Local Variable이 Immutable 하더라도 Local Variable은 Constant로 취급되지 않으며, 따라서 Constant 명명법을 따르지도 않는다. |
|
Type Variable Names | - 단일 대문자로 구성하거나 Class 명명법과 동일하게 명명하되, 끝에 T를 붙인다. |
E T X T2 RequestT FooBarT |
5.3 Camel case: defined
- IPv6나 iOS와 같이, 영문구를 Camel Case로 변환하는 다양한 합리적인 방법이 존재하는데,
Google에서는 불확실성을 해소하기 위해 아래와 같은 결정적인 명명 절차를 갖고 있다:
- 모든 문자는 Plain ASCII로 변환하고, Apostrophe(`)는 제거한다.
- 구성된 이름을 단어 단위로 분할하고, Space나 어떤 Punctuation으로 분할한다.
- 일반적으로, Hypen으로 구분한다.
- 어떤 단어에 대한 Camel-Case가 존재하는 경우, 성분별로 분할한다.
- Acronym을 포함하여 모두 소문자로 구성되었는데, 이들 단어의 첫번째 문자를 대문자로 변환한다.
- UpperCamelCase는 모든 단어의 첫 번째 글자를 대문자로 변환하는 경우이다.
- lowerCamelCase는 첫 번째 단어를 제외한 모든 단어의 첫 번째 글자를 대문자로 변환하는 경우이다.
- 모든 단어를 하나의 Indentifier로 합친다.
- 아래 예시를 참고하자:
Prose Form | Correct | Incorrect |
XML HTTP request | XmlHttpRequest | XMLHTTPRequest |
new customer ID | newCustomerId | newCustomerID |
inner stopwatch | innerStopwatch | innerStopWatch |
support IPv6 on iOS? | supportsipv6OnIos | supportsIPv6OnIOS |
YouTube impoter | YouTubeImporter |
6 Programming Practices
6.1 @Override: always used
6.2 Caught exceptions: not ignored
6.3 Static members: qualified using class
6.4 Finalizers: not used
7 Javadoc
7.1 Formatting
7.2 The summary fragment
7.3 Where Javadoc is used
Reference: Google, Google Java Style Guide, URL, 2023년 1월 7일 검색
Reference: 신용권, 임경균; 이것이 자바다(개정판); 한빛미디어; 2022
Java Coding Convention
자바 코딩 컨벤션
- 자바 소스 코드 작성에 대해 관례적으로 통용되는 스타일에 대해 소개한다.
* 본 포스트에서는 Google에서 소개하는 컨벤션을 참고했다. (URL)
Google Java Style Guide
1 Introduction This document serves as the complete definition of Google's coding standards for source code in the Java™ Programming Language. A Java source file is described as being in Google Style if and only if it adheres to the rules herein. Like ot
google.github.io
1 Introduction
1.1 Terminology notes
1.2 Guide notes
2 Source file basics
2.1 File name
2.2 File encoding: UTF-8
2.3 Special characters
3 Source file structure
3.1 License or copyright information, if present
3.2 Package statement
3.3 Import statements
3.4 Class declaration
4 Formatting
4.1 Braces
4.2 Block indentation: +2 spaces
4.3 One statement per line
4.4 Column limit: 100
4.5 Line-wrapping
4.6 Whitespace
4.7 Grouping parentheses: recommended
4.8 Specific constructs
5. Naming (이름 명명법)
5.1 Rules common to all identifiers (모든 식별자들의 공통 명명법)
- 모든 Indentifier들은 반드시 ASCII 문자와 숫자, Underscore(_)로만 구성된다.
- Valid Indentifier의 이름을 Regular Expression으로 표현하면 아래와 같다:
\(Ww+\)
- Google에서는 name_
, mName
, s_name
, kName
과 같은 Special Prefix/Suffix를 사용하지 않는다.
5.2 Rules by identifier type (식별자 타입별 명명법)
Rule | Description | Valid Example |
Package Names | - 소문자와 숫자로만 명명한다. - 단어들이 연속될 경우, 그냥 잇는다. |
com.example.deepspace |
Class Names | - UpperCamelCase 로 명명한다. - 명사 혹은 명사구로 명명한다. - Interface의 경우, 명사/명사구 혹은 형용사/형용사구로 명명한다. - Test Class의 경우, Test로 끝나도록 명명한다. - Test Class가 오직 하나의 클래스만을 커버한다면, 해당 클래스의 이름 뒤에 Test를 붙여 명명한다. |
Character ImmutableList (Class) Readable (Interface) HashIntergrationTest (Test Class) |
Method Names | - lowerCamelCase 로 명명한다. - 동사 혹은 동사구로 명명한다. - Underscore(_)는 JUnit test Method를 명명할 때, Logical Component들을 구분짓는 용도로 사용할 수 있다. |
sendMessage stop (General Method) transferMoney_deductsFromSource (JUnit test Method) |
Constant Names | - UPPER_SNAKE_CASE 로 명명한다. - 명사 혹은 명사구로 명명한다. - Constant란, Deeply Immutable하고 Method들이 영향을 주지 못하는 Static Final Field를 의미한다. (primitive, string, Immutable Value Class, null 등) |
NUMBER NAMES EMPTY_ARRAY COMMA_JOINER |
Non-Constant Field Names | - lowerCamelCase 로 명명한다. - 명사 혹은 명사구로 명명한다. |
computedValues index |
Parameter Names | - lowerCamelCase 로 명명한다. - Public Method에서 Parameter의 이름을 하나의 글자로 명명하는 것은 특히 피해야 한다. |
|
Local Variable Names | - lowerCamelCase 로 명명한다. - Local Variable이 Immutable 하더라도 Local Variable은 Constant로 취급되지 않으며, 따라서 Constant 명명법을 따르지도 않는다. |
|
Type Variable Names | - 단일 대문자로 구성하거나 Class 명명법과 동일하게 명명하되, 끝에 T를 붙인다. |
E T X T2 RequestT FooBarT |
5.3 Camel case: defined
- IPv6나 iOS와 같이, 영문구를 Camel Case로 변환하는 다양한 합리적인 방법이 존재하는데,
Google에서는 불확실성을 해소하기 위해 아래와 같은 결정적인 명명 절차를 갖고 있다:
- 모든 문자는 Plain ASCII로 변환하고, Apostrophe(`)는 제거한다.
- 구성된 이름을 단어 단위로 분할하고, Space나 어떤 Punctuation으로 분할한다.
- 일반적으로, Hypen으로 구분한다.
- 어떤 단어에 대한 Camel-Case가 존재하는 경우, 성분별로 분할한다.
- Acronym을 포함하여 모두 소문자로 구성되었는데, 이들 단어의 첫번째 문자를 대문자로 변환한다.
- UpperCamelCase는 모든 단어의 첫 번째 글자를 대문자로 변환하는 경우이다.
- lowerCamelCase는 첫 번째 단어를 제외한 모든 단어의 첫 번째 글자를 대문자로 변환하는 경우이다.
- 모든 단어를 하나의 Indentifier로 합친다.
- 아래 예시를 참고하자:
Prose Form | Correct | Incorrect |
XML HTTP request | XmlHttpRequest | XMLHTTPRequest |
new customer ID | newCustomerId | newCustomerID |
inner stopwatch | innerStopwatch | innerStopWatch |
support IPv6 on iOS? | supportsipv6OnIos | supportsIPv6OnIOS |
YouTube impoter | YouTubeImporter |
6 Programming Practices
6.1 @Override: always used
6.2 Caught exceptions: not ignored
6.3 Static members: qualified using class
6.4 Finalizers: not used
7 Javadoc
7.1 Formatting
7.2 The summary fragment
7.3 Where Javadoc is used
Reference: Google, Google Java Style Guide, URL, 2023년 1월 7일 검색
Reference: 신용권, 임경균; 이것이 자바다(개정판); 한빛미디어; 2022