Backend

[우아한테크코스/프리코스] Java Code Conventions를 IntellJ에 적용하기

ellie.strong 2021. 11. 28. 02:53
728x90

우리는 지금까지 NAVER CAMPUS HACKDAY의 Java Code Conventions를 학습하였다. 

📌 [우아한테크코스/프리코스] Java Code Conventions (tistory.com)

 

[우아한테크코스/프리코스] Java Code Conventions

우아한테크코스 프리코스에서 권고하는 NAVER CAMPUS HACKDAY의 Java Code Conventions를 학습하고 이를 적용시켜 보려한다. 📌 캠퍼스 핵데이 Java 코딩 컨벤션 (naver.github.io) 캠퍼스 핵데이 Java 코딩 컨벤..

programmer-ririhan.tistory.com

 

그렇다면 이번에는 NAVER CAMPUS HACKDAY의 Java Code Conventions를 IntellJ 설정에 적용한다. 

📌 캠퍼스 핵데이 Java 코딩 컨벤션 (naver.github.io)

 

캠퍼스 핵데이 Java 코딩 컨벤션

중괄호({,}) 는 클래스, 메서드, 제어문의 블럭을 구분한다. 5.1. K&R 스타일로 중괄호 선언 클래스 선언, 메서드 선언, 조건/반복문 등의 코드 블럭을 감싸는 중괄호에 적용되는 규칙이다. 중괄호

naver.github.io

 

 

🍑 .editorconfig 파일 설정

.editorconfig는 다양한 에디터와 IDE에서 공통적으로 지원하는 코드 스타일에 대한 설정 파일이다. 

같은 프로젝트를 베이스로 작업하는 사람들의 코드 컨벤션을 일관성 있게 유지할 수 있으며 다양한 에디터로 파일을 고칠 때 같은 규칙을 참조할 수 있게된다.

✔️ 가급적 이 파일을 소스 저장소에서 올려서 공유하는 것을 권장한다.

ref : EditorConfig

# top-most EditorConfig file
root = true

[*]
# [encoding-utf8]
charset = utf-8

# [newline-lf]
end_of_line = lf

# [newline-eof]
insert_final_newline = true

[*.bat]
end_of_line = crlf

[*.java]
# [indentation-tab]
indent_style = tab

# [4-spaces-tab]
indent_size = 4
tab_width = 4

# [no-trailing-spaces]
trim_trailing_whitespace = true

[line-length-120]
max_line_length = 120

IntellJ : Add an .editorconfig file

ref: Configuring code style | IntelliJ IDEA (jetbrains.com)

ref: Managing Code Style on a Directory Level with EditorConfig | The IntelliJ IDEA Blog (jetbrains.com)

"New" > "EditorConfig FIle"

🍑 Checkstyle 사용법

Checkstyle 은 코딩컨벤션 검사 도구이다.

이 가이드에서 안내하는 규칙을 검사하는 checkstyle 규칙 설정 파일을 제공한다.

🍑 빌드 도구 설정 : Gradle

인코딩 지정

Java plugin의 속성으로 인코딩을 지정한다.

plugins {
    id 'java'
}

...

// 방법1
compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

// 방법 2
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

// 방법 3
tasks.withType(Compile) {
    options.encoding = 'UTF-8'
}

🍑 편집기 설정 : IntelliJ

NAVER HACKDAY Java Code Conventions를 지키는데 도움이 되는 코드 편집기와 뷰어 설정 방법이다.

Formatter 적용

naver-intellij-formatter.xml 파일 다운로드

📌 hackday-conventions-java/naver-intellij-formatter.xml at master · naver/hackday-conventions-java (github.com)

💡 프로젝트 폴더에 해당 파일을 추가시켜줘야하는 줄 알았는데 그냥 아무 곳에나 다운받고 IntelliJ에 한번 적용시키면 이후에도 그냥 setting에서 해당 formatter를 사용한다는 scheme 설정만 추가해주면 된다!!

Scheme 설정

✔️ "Code" > "Reformat Code"(Ctrl+Alt+L)로 formatter 적용

파일의 마지막에 새줄 문자가 없는 경우 추가하기 설정

공백 문자 보이기 설정

탭과 스페이스가 섞여 있는 프로젝트의 코드를 정리할 때는 탭과 스페이스를 눈에 보이게 표시한다.

 

 

728x90