Java Code Style
- Don’t Ignore Exception
- 不要忽略Exception的處理
- 當程式架構越大時,若沒注意Exception的問題,容易發生許多錯誤
- 在catch Exception時,不要取出最上層的Excpetion
- Throw the exception up to the caller of your method.
void setServerPort(String value) throws NumberFormatException {
serverPort = Integer.parseInt(value);
}
- Throw a new exception that's appropriate to your level of abstraction.
void setServerPort(String value) throws ConfigurationException {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new ConfigurationException("Port " + value + " is not valid.");
}
}
- Catch the Exception and throw a new RuntimeException. This is dangerous: only do it if you are positive that if this error occurs, the appropriate thing to do is crash.
void setServerPort(String value) {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new RuntimeException("port " + value " is invalid, ", e);
}
}
- Don’t Use Finalizers
- 在Java中,當沒在使用的Object會經由Garbage Collection機制回收,回收的同時會執行Object的finalize()。
- 由於回收的時間不固定,因此若要在Object被回收後做某些處理,不適合採用finalize()
- 可以在適當時機,自行撰寫、呼叫function
- Fully Qualify Imports
- Java有兩種方法進行import
- Import foo.*
- import的行數,可以把foo下面所有的class都import進來
- Import foo.Bar
- class,程式可讀性較高,不會將不需要的也import進來
Java Style Rules
- Use Javadoc Standard Comments
- 每個class或其重要的公開函式,最少有一行來解釋該函式的作用
- 一些命名較含糊不清的,也需要注解其意義與功能
- Write Short Methods
- 撰寫的function應該要保持精簡,若超過40行,則要思考是否要拆成2~*個函式來處理
- Limit Variable Scope
- 在程式的一些區域變數中,其存在的範圍要盡量最小化,藉此提升程式可讀性與可維護性,也可避免一些錯誤
- 區域變數應等需要時,才進行宣告,並且做初始化的動作
- 只有一個情況例外,當變數的初始化是藉由其它function return回來的話,需要try catch去驗證,此時該區域變數就需要放在try catch之外。
Set s = null;
try {
s = (Set) cl.newInstance();
} catch(IllegalAccessException e) {
throw new IllegalArgumentException(cl + " not accessible");
} catch(InstantiationException e) {
throw new IllegalArgumentException(cl + " not instantiable");
}
- for迴圈所使用的變數,也應直接宣告在for的
for (int i = 0; i < n; i++) {
doSomething(i);
}
- Use space for Indentation
若程式碼過長,會需要換行來進行排版,此時跟以往縮4格不同,若為同一行程式應縮成8格。
Instrument i =
someLongExpression(that, wouldNotFit, on, one, line);
- Follow Field Naming Conventions
- 不是public、static的變數,以m為開頭
- Static的變數,以s開頭
- 其它變數,第一個字姆小寫即可
- Public static變數,則全部大寫
public class MyClass {
public static final int SOME_CONSTANT = 42;
public int publicField;
private static MyClass sSingleton;
int mPackagePrivate;
private int mPrivate;
protected int mProtected;
}
- Use Standard Brace Style
- 在括號的使用上,範例如下
class MyClass {
int func() {
if (something) {
// ...
} else if (somethingElse) {
// ...
} else {
// ...
}
}
}
- 若括號中的程式碼只有一行,可以不加括號,但該行程式不應直接放在下一行
if (condition) body();
if (condition)
body(); // bad!
- Limit Line Length
- 每一行程式碼,其長度最好別超過100個字元
- Use Standard Java Annotations
- @Deprecated (過時的)
- API存在此函式,但是後來不建議使用,就會加上此注解,當使用者之後想針對該函式覆寫或呼叫時,會發出警示
- @Override (覆寫)
- @SuppressWarnings (仰制警告)
- @Deprecated,當執行時會發出警告,但程式想要強行執行的話,可以加上 @SuppressWarnings(deprecated) ,如此便可忽略Deprecated的警告(其它警告亦同)
- @Deprecated (過時的)
- Use TODO Comments
- 使用這個來下注解,代表這裡的程式可以運行,但不是最好的解法
// TODO: Remove this code after the UrlTable2 has been checked in.
