JAVA規則開發篇

 本文介紹的JAVA規則的說明分爲3個主要級別,本篇拋棄了平時開發中很少遇到的情況,那些用得比較少的以後再高級篇裏面出現。並有六個有用的國際軟件開發重要注意的有關String的問題,遵守了這些規則可以提高程序的效率、使代碼又更好的可讀性等。
(1) 如果有JDBC連接沒有關掉的話,需要在"finally"方法中關掉
如果數據庫連接失敗或者是沒有釋放連接,看上去無關緊要。但是其他的用戶就需要用更長的時間等待連接,這樣數據庫利用效率就會下降。確保你的代碼在任何情況下,包括出錯或者程序異常終止的情況下都釋放數據庫連接。在"finally"方法中關掉連接,就可以確保這一點。
錯誤示例:
try {
Statement stmt = con.createStatement();
} catch(SQLException e) {
e.printStackTrace();
}
正確示例:
try {
Statement stmt = con.createStatement();
} finally {
if (con != null && !con.isClosed()) {
con.close();
}
}

(2) 儘量避免使用'Thread.resume ()', 'Thread.stop ()', 'Thread.suspend ()'和 'Runtime.runFinalizersOnExit ()' 方法。
這些方法在平時的開發或者是教科書裏面也有用到過,但是這些方法會導致四鎖的傾向。一下有充足的資料來說明爲什麼不建議用上述方法。
參考:1."java.lang.Thread" in the JDK API documentation
2. http://java.sun.com/j2se/1.3/docs/guide/misc/threadPrimitiveDeprecation.html
3.Paul Hyde: "Java Thread Programming"
Sams, ISBN: 0-672-31585-8 pp. 270

(3) 在表示長整常量的時候,用L來代替l.
因爲l很容易和1混一起。
錯誤示例:
long temp = 23434l;
正確示例:
long temp = 23434L;
參考:Ken Arnold, James Gosling: "The Java Programming Language Second Edition"Addison Wesley, 1997, pp.108

(4) 最好在jsp開頭寫一條註釋
在 jsp文件頭上面寫一條註釋,這樣可以幫助別人來理解你的代碼。這條規則不僅適用於jsp,更是用於任何開發的文檔。
正確示例:<%-- JSP comment --%>

(5)明確的初始化一個構造類裏面的所有的字段
因爲沒有初始化的字段會是一個潛在的bug,所以最好初始化類裏面的所有的字段。特別是靜態的字段,最好在一開始就分配一個初始值
錯誤示例:
public class CSI {
public CSI () {
this (12);
k = 0;
}

public CSI (int val) {
j = val;
}

private int i = 5;
private int j;
private int k;
}

正確示例:
public class CSIFixed {
public CSIFixed () {
this (12);
}

public CSIFixed (int val) {
j = val;
k = 0;
}

private int i = 5;
private int j;
private int k;
}
參考:http://www.ambysoft.com/javaCodingStandards.pdf

(6) 國際化開發建議:邏輯操作符不要再一個單個的字符的前面或者後面
一個單個字符的前後不要用邏輯操作符,如果代碼要在一個國家環境中運行的話。我們可以使用字符比較方法,這些方法使用統一字符比較標準來定義字符的屬性的。
錯誤示例:public class CLO {
public boolean isLetter (char ch) {
boolean _isLetter = ( ch >= 'a' && ch <= 'z')   //錯誤
|| (ch >= 'A' && ch <= 'Z');
return _isLetter;
}
}

正確示例:
public class CLOFixed {
public boolean isLetter (char ch) {
boolean _isLetter = Character.isLetter(ch);
return _isLetter;
}
}
參考: http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
更多的字符比較方法請參考:http://java.sun.com/docs/books/tutorial/i18n/text/charintro.html

(7) 國際化開發建議:不要對日期對象使用'Date.toString ()'
不要使用'Date.toString ()'方法,日期格式對於地區和語言不同的國家來說是不一樣的,務必不要使用。
錯誤示例:'DateFormat'類提供了一個預定義的格式類型來指定本地的格式。
public void printToday () {
Date today = new Date ();
String todayStr = today.toString ();
System.out.println (todayStr);
}
正確示例:
public void printToday () {
Locale currentLocale = Locale.getDefault ();
DateFormat dateFormatter = DateFormat.getDateInstance (
DateFormat.DEFAULT, currentLocale);
Date today = new Date ();
String todayStr = dateFormatter.format (today);
System.out.println (todayStr);
}
參考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
http://java.sun.com/docs/books/tutorial/i18n/format/dateFormat.html

(8) 國際化開發建議:不要對數字變量使用'toString ()'方法
在全球化的開發中,不要對數字變量使用'toString ()'方法,對於java.lang.Number的任何子類都適用。包括:BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.對於這樣的情況,java裏也與定義了"NumberFormat"方法來格式化。
錯誤示例:
public class NTS {
public void method (Double amount) {
String amountStr = amount.toString ();
System.out.println (amountStr);
}
}
正確示例:
public class NTSFixed {
public void method (Double amount) {
Locale currentLocale = Locale.getDefault ();
NumberFormat numberFormatter =
NumberFormat.getNumberInstance (currentLocale);
String amountStr = numberFormatter.format (amount); //
System.out.println (amountStr + ' ' + currentLocale.toString ());
}
}
參考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
http://java.sun.com/docs/books/tutorial/i18n/format/numberFormat.html

(9) 國際化開發建議:不要使用'String.equals ()'方法
建議不要使用'String.equals ()'方法,因爲在統一字符比較標準中不一定按照相關的順序來比較。'Collator'提供的預定義整理規則來排序string,Collator類調用'getInstance ()'方法,一般來說,可以爲默認的本地創建一個Collator。例如:Collator myCollator = Collator.getInstance ();創建Collator的時候你也可以指定一個特殊的locale。例如:Collator myFrenchCollator = Collator.getInstance (Locale.FRENCH);然後就可以調用'Collator.compare ()'來執行一個本地的字符比較myCollator.compare (s1,s2);從這裏可以瞭解更多的有關Collator類的信息:http://java.sun.com/docs/books/tutorial/i18n/text/collationintro.html

錯誤示例:
public class SE {
public boolean compstr (String s1, String s2) {
boolean b = (s1.equals (s2));
return b;
}
}
正確示例:
public class SEFixed {
public boolean compstr (String s1, String s2) {
Collator myCollator = Collator.getInstance ();
boolean b = (myCollator.compare(s1,s2) == 0);
return b;
}
}

參考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
http://java.sun.com/docs/books/tutorial/i18n/text/locale.html

(10) 國際化開發建議:不要使用'StringTokenizer()'方法
錯誤示例:StringTokenizer st = new StringTokenizer(str);
可以從這裏得到更多的信息:‘
參考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html

(11) 國際化開發建議:不要使用'Time.toString ()'方法
因爲時間的格式各個國家也不一樣。如果你使用日期格式類,你的應用就能夠在世界上各個地方正確的顯示時間和日期了。首先,用'getTimeInstance ()'方法創建一個formatter。然後,調用'format ()'方法。
錯誤示例:
public class TTS {
public void printTime (Time t1) {
String timeStr = t1.toString ();
System.out.println (timeStr);
}
}
正確示例:
import java.sql.Time;
import java.text.DateFormat;
import java.util.Locale;

public class TTSFixed {
public void printTime (Time t1) {
DateFormat timeFormatter = DateFormat.getTimeInstance(
DateFormat.DEFAULT, Locale.getDefault ());
String timeStr = timeFormatter.format(t1);
System.out.println (timeStr);
}
}

 

 

發佈了208 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章