public List getAllProduct(); //正确。定义返回类型public ArrayList getAllProduct(); //错误!!定义返回类型public List queryByCritical(Map criticals); //定义参数类型public List queryByCritical(HashMap criticals); //错误!!定义参数类型
List result = null;//定义类型的时候未List result = new ArrayList();//只有构造时出现的实现类 Map result = null;//定义类型的时候未Map result = new HashMap();//只有构造时出现的实现类
String user_name= null;//错误!!! 即使数据库中这种风格String userName = null;//正确写法int CET_SIX=6;//常量命名时用下划线区分单词,字符全部大写
//错误!!不能出现如此写法,需要定义为常量if(user.getNumber() == 1001 ) { //do something } //正确写法static final int ADMINISTRATOR_ROLE_NUMBER = 1001; static final int MIN_WIDTH = 4; static final int MAX_WIDTH = 999; static final int GET_THE_CPU = 1 if(user.getNumber() == ADMINISTRATOR_ROLE_NUMBER ) { //do something }
//sample code snippetfor(int i=0;i<10;i++){ ValueObject vo = new ValueObject(); }
//recommend this style ValueObject vo = null; for(int i=0;i<10;i++){ vo = new ValueObject(); }
//sample code snippetString sql =”INSERT INTO test (”; Sql+=”column1,column2,values(” Sql+=”1,2)”
StringBuffer sql = new StringBuffer(); sql.append(”INSERT INTO test (”); sql.appdend(”column1,column2,values(”); sql.append(”1,2)”);
if ( condition) //single statement, code herewhile ( condition ) //single statement, code here
//IFif ( condition) { //code here } //WHILEwhile ( condition ) { // code here }
if ( foo ) { // code here } try { // code here } catch (Exception bar) { // code here } finally { // code here } while ( true ) { // code here }
System.out.println(" debug 信息");
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; private static Log logger = LogFactory.getLog(SQLTable.class); logger.debug("debug 信息"); //注意这里没有涉及字符串操作 //涉及字符串操作或者方法调用的,需要用 logger.isDebugEnable() 来做判断 if(logger.isDebugEnable()){ logger.debug(String1 + string 2 + string3 + object.callMethod()); }
logger.info(""Can't find column to build index. ColName=" + columnName");
try{ //handle something } catch (Exception ex) { //do nothing. 请确定该exception是否可以被忽略!!!! }
try{ //handle something } catch (Exception ex) { ex.printStackTrace ();//不在程序中出现如此写法!! }
try{ //handle something } catch (Exception ex) { log.error(ex);//这样将仅仅是输出ex.getMessage(),不能输出stacktrace }
try{ //handle something } catch (Exception ex) { log.error("错误描述",ex);//使用log4j做异常记录 }
//sample code snippet Connection con = null; Statement st = null; ResultSet rs = null; try { con = JNDIUtils.getConnection(); st = … rs = … } finally { JDBCUtil.safeClose(rs);//close resultset ,it is optional JDBCUtil.safeClose(st);//close statement after close resultset, it is optional JDBCUtil.safeClose(con);//make sure release it right now }
//sample code snippetString username = … … if(“mark”.equals(userName)){ … }
//recommend coding conventionimport java.util.HashMap; import java.util.Map; import java.util.List; //import java.util.*; NOT!!//We can use eclipse,right click, choose Source -> Organize Imports//hotkey:Ctrl+Shift+O
/** * $Id: $ * */
<?xml version="1.0" encoding="UTF-8"?><!-- $Id:$ -->
//sample code snippet//TODO issue: the data format, should be fixed
//user table中的该字段允许为null,使用时就需要去check,否则可能出现nullpoint exceptionif(user.getDescription()!=null) { //do something }
<input type="text" maxlength = "10" size="20"/>