JAVA规则 开发篇
2008-01-05 10:48:29 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁炬儳缍婇弻鐔兼⒒鐎靛壊妲紒鐐劤缂嶅﹪寮婚悢鍏尖拻閻庨潧澹婂Σ顔剧磼閹冣挃闁硅櫕鎹囬垾鏃堝礃椤忎礁浜鹃柨婵嗙凹缁ㄧ粯銇勯幒瀣仾闁靛洤瀚伴獮鍥敍濮f寧鎹囬弻鐔哥瑹閸喖顬堝銈庡亝缁挸鐣烽崡鐐嶆棃鍩€椤掑嫮宓佸┑鐘插绾句粙鏌涚仦鎹愬闁逞屽墰閹虫捇锝炲┑瀣╅柍杞拌兌閻ゅ懐绱撴担鍓插剱妞ゆ垶鐟╁畷銉р偓锝庡枟閻撴洘銇勯幇闈涗簼缂佽埖姘ㄧ槐鎾诲礃閳哄倻顦板┑顔硷工椤嘲鐣烽幒鎴旀瀻闁规惌鍘借ⅵ濠电姷鏁告慨顓㈠磻閹剧粯鈷戞い鎺嗗亾缂佸鏁婚獮鍡涙倷閸濆嫮顔愬┑鐑囩秵閸撴瑦淇婇懖鈺冪<闁归偊鍙庡▓婊堟煛鐏炵硶鍋撻幇浣告倯闁硅偐琛ラ埀顒冨皺閺佹牕鈹戦悙鏉戠仸闁圭ǹ鎽滅划鏃堟偨缁嬭锕傛煕閺囥劌鐏犻柛鎰ㄥ亾婵$偑鍊栭崝锕€顭块埀顒佺箾瀹€濠侀偗婵﹨娅g槐鎺懳熺拠鑼舵暱闂備胶枪濞寸兘寮拠宸殨濠电姵纰嶉弲鎻掝熆鐠虹尨宸ョ€规挸妫濆铏圭磼濡搫顫嶇紓浣风劍閹稿啿鐣烽幋锕€绠婚悹鍥у级瀹撳秴顪冮妶鍡樺鞍缂佸鍨剁粋宥夋倷椤掍礁寮垮┑鈽嗗灣閸樠勭妤e啯鍊垫慨妯煎亾鐎氾拷

核心提示:本文介绍的java规则的说明分为3个主要级别,本篇抛弃了平时开发中很少碰到的情况,JAVA规则 开发篇,那些用得比较少的以后再高级篇里面出现,并有六个有用的国际软件开发重要注重的有关String的问题,用'getTimeInstance ()'方法创建一个formatter,然后,遵守了这些规则可以提高
本文介绍的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
(5) 国际化开发建议:逻辑操作符不要再一个单个的字符的前面或者后面
一个单个字符的前后不要用逻辑操作符,假如代码要在一个国家环境中运行的话。我们可以使用字符比较方法,这些方法使用统一字符比较标准来定义字符的属性的。
错误示例: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
(6) 国际化开发建议:不要对日期对象使用'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
(7) 国际化开发建议:不要对数字变量使用'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
(8) 国际化开发建议:不要使用'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
(9) 国际化开发建议:不要使用'StringTokenizer()'方法
错误示例:StringTokenizer st = new StringTokenizer(str);
可以从这里得到更多的信息:‘
参考:http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
(10) 国际化开发建议:不要使用'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);
更多精彩
赞助商链接