새로운 팀프로젝트에 합류하면서 Google 의 컨벤션을 베이스로 한다고 해서 이번기회에 하나하나 살펴보려고 한다.
딱 봤을 때 납득이 되거나 잘 사용되지 않을 것 같은 부분은 따로 정리하지 않고,
왜 이런 컨벤션을 지향해야 하는지 의문이 들거나 햇갈리는 것들만 정리해봤다.
if, else, for, do 및 while문
또는 body가 비어 있거나 단 하나의 문이 포함 된 경우에도 괄호가 쓰인다.else
또는 ,
가 오면 줄바꿈 x// Ex)
return () -> {
while (condition()) {
method();
}
};
return new MyClass() {
@Override public void method() {
if (condition()) {
try {
something();
} catch (ProblemException e) {
recover();
}
} else if (otherCondition()) {
somethingElse();
} else {
lastThing();
}
}
};
// 허용
void doNothing() {}
// 허용
void doNothingElse() {
}
// 허용되지 않음 : 멀티 블럭 구문에서는 간결한 빈 블럭을 사용할 수 없다.
try {
doSomething();
} catch (Exception e) {}
Java 코드의 열 제하는 100 자 이다.
예외
줄바꿈 예시
// before
int result = someLongVariable + anotherLongVariable - yetAnotherVariable * calculateSomeValue();
// after
int result = someLongVariable +
anotherLongVariable -
yetAnotherVariable * calculateSomeValue();
// before
object.method().property1.property2.property3;
// after
object.method()
.property1
.property2
.property3;
// before
public <T extends SomeReallyLongTypeName & AnotherLongTypeName> void someMethod() {
// method implementation
}
// after : 제네릭이 길 때 줄 바꿈
public <T extends SomeReallyLongTypeName & AnotherLongTypeName>
void someMethod() {
// method implementation
}
// good : 쉼표 뒤에 줄 바꿈
int[] numbers = {
1,
2,
3,
4
};
// bad
package com.example.myApp
package com.example.my_app
// good
package com.example.myapp
_
로 각 단어를 구분하는 방법