-
[클린코드] 형식 맞추기Java/클린코드 2022. 2. 15. 22:54
01 포맷팅이 중요한 이유
02 클린코드 포맷팅
03 Java Class Declarations
04 Coding Convention
포맷팅이 중요한 이유
- 가독성에 필수적이다.
클린코드 포맷팅
- 적절한 길이 유지
- ~ 200 lines < 500 lines
- 밀접한 개념은 가까이 둔다.
- 행 묶음은 완결된 생각 하나를 표현하기 때문에 개념은 빈 행으로 분리한다.
- 변수는 사용되는 위치에서 최대한 가까이 선언한다.
public class BoldWidget extends ParentWidget { public static final String REGEXP = "'''.+?'''"; private static final Pattern pattern = Pattern.compile("'''(.+?)'''", Pattern.MULTILINE + Pattern.DOTALL ); public BoldWidget(ParentWidget parent, String text) throws Exception { super(parent); Matcher match = pattern.matcher(text); match.find(); addChildWidgets(match.group(1)); } public String render() throws Exception { StringBuffer html = new StringBuffer("<b>"); html.append(childHtml()).append("</b>"); return html.toString(); } }public class Assert { static public void assertTrue(String message, boolean condition) { if (!condition) fail(message); } static public void assertTrue(boolean condition) { assertTrue(null, condition); } static public void assertFalse(String message, boolean condition) { assertTrue(message, !condition); } static public void assertFalse(boolean condition) { assertFalse(null, condition); } ... }Java Class Declarations
https://www.oracle.com/java/technologies/javase/codeconventions-fileorganization.html#1852
Class 내부 코드 순서
- static 변수
- public -> protected -> package -> private 순서
- instance 변수
- public -> protected -> package -> private 순서
- 생성자
- 메서드
- public 메서드에서 호출되는 private 메서드는 그 아래에 둔다. 가독성 위주로 그룹핑한다!
Coding Convention
- Team Coding Convention
- 개발 언어의 컨벤션이 우선이지만, 애매한 부분은 팀 컨벤션을 따른다.
- 없다면, 함께 만들어 가는 것도 좋다.
- 참고할 만한 컨벤션
- Google Java Style Guide
- Naver Hackday Java Convention
'Java > 클린코드' 카테고리의 다른 글
[클린코드] 오류 처리 (0) 2022.03.01 [클린코드] 객체와 자료구조 (0) 2022.02.23 [클린코드] 주석 (0) 2022.02.11 [클린코드] 함수 (0) 2022.02.07 깨끗한 코드 그리고 의미있는 이름 (0) 2022.02.06