代碼格式Code format
為了提高代碼的可讀性,代碼格式的規則非常的重要,請按下列方式去實現:
格式規則:
1.import中不能有通配符
// 不允许
import java.util.*;
// Good!
import java.util.List;
2.即使代碼塊中沒有內容,或者只有一行代碼,也必須使用大括號
// 不允许
if(Math.abs(x, y) > 0)
return;
// Good!
if(Math.abs(x, y) > 0) {
return;
}
3.使用IneterFace(接口)禁止使用public修飾
// 不允许
public void deleteProduct(String ids);
// Good!
void deleteProduct(String ids);
4.泛型遵循鑽石寫法
// 不允许
Map<String, Object> map = new HashMap<String, Object>();
// Good!
Map<String, Object> map = new HashMap<>();
5.switch語句中,必須包含default語句
switch(level) {
case 1:
// Do something here
break;
default:
// Do nothing here
}
6.用log替代System.out.print()
Log.d(TAG, "onBefore s " + request.getUrl().toString());
7.不要對boolean值做true/false判斷
// 不允许
if(record.isDelete() == true) {
// Do something here
}
// Good!
if (record.isDelete()) {
// Do something here
}
8.基本的空行與縮排,使用Android自定的規則,使用重新整理代碼(Alt+Ctrl+L)的原則。
9.提交版本前,刪除無引用的import,快捷鍵(Alt+Ctrl+O),以及重新整理代碼(Alt+Ctrl+L),始終保持優雅的代碼。