posts - 61,  comments - 2033,  trackbacks - 0

1.统一工作目录

2.Interface oriented programming

在定义参数类型,或者方法返回类型,使用Map或者List,不用Hashmap or ArrayList。只有在构造时才允许出现Hashmap或者ArrayList
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();//只有构造时出现的实现类

3.变量命名不允许出现下划线,除了常量命名时用下划线区分单词

String user_name= null;//错误!!! 即使数据库中这种风格
String userName = null;//正确写法
int CET_SIX=6;//常量命名时用下划线区分单词,字符全部大写

4.代码中不能出现magic number

//错误!!不能出现如此写法,需要定义为常量
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
}

5.不在循环中定义变量

//sample code snippet
for(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();
}

6.NOT TAB,采用4 spaces。大家请设置ide的TAB为4 space

7.使用StringBuffer来替代String + String

不正确写法:
//sample code snippet
String 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)”);

8.单语句在IF,While中的写法. 使用Brackets 来做程序块区分

不正确写法:
if ( condition) //single statement, code here

while ( condition ) //single statement, code here

正确写法:
//IF
if ( condition) {
  //code here
}

//WHILE
while ( condition ) {
  // code here
}

9.Brackets 应当直接在语句后

if ( foo ) {
    // code here
}

try {
    // code here
} catch (Exception bar) {
    // code here
} finally {
    // code here
}

while ( true ) {
    // code here
}

10.用log4j来做日志记录,不在代码中使用System.out

错误写法:
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来做输出
logger.info(""Can't find column to build index. ColName=" + columnName");

11.异常处理中日志问题

错误写法1:
try{
   //handle something
} catch (Exception ex) {
   //do nothing. 请确定该exception是否可以被忽略!!!!
}

错误写法2:
try{
   //handle something
} catch (Exception ex) {
  ex.printStackTrace ();//不在程序中出现如此写法!!
}

错误写法3:
try{
   //handle something
} catch (Exception ex) {
  log.error(ex);//这样将仅仅是输出ex.getMessage(),不能输出stacktrace
}

正确写法:
try{
   //handle something
} catch (Exception ex) {
   log.error("错误描述",ex);//使用log4j做异常记录
}

12.Release Connection ,ResultSet and Statement

//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
}

13.Replace $variableName.equals(’string’) with ‘string’.equals($variableName)

减少由于匹配的字符为null出现的nullpointexception
//sample code snippet
String username = …
…
if(“mark”.equals(userName)){
	…
}

14.always import classes

//recommend coding convention
import 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

15.注释

程序header,加入如下片断。这样将可以记录当前文件的版本以及最后的修改人员

java,jsp中加入
/**
 * $Id: $
 *          
 */

xml中加入
<?xml version="1.0" encoding="UTF-8"?>
<!--
$Id:$ 
-->

16.在有issue出,加入//TODO 来记录,以便在task中可以方便记录未完成部分

//sample code snippet
//TODO issue: the data format, should be fixed

17.domain model或者VO使用注意事项

检查数据库中允许为null的栏位

对从数据库中取出的domain model或者VO,如果数据库中允许为null,使用有必要检查是否为null
CODE SNIPPET
//user table中的该字段允许为null,使用时就需要去check,否则可能出现nullpoint exception
if(user.getDescription()!=null) {
  //do something
}

需要完成VO中的equals,hashCode,toString 三个方法

18.JSP中约定

为每个input设定好size同maxlength

<input type="text" maxlength = "10" size="20"/>
posted on 2005-12-20 17:07 鱼上游 阅读(1404) 评论(7)  编辑  收藏 所属分类: 爪哇世界探险


FeedBack:
# re: Rule Of Development
2005-12-20 17:29 | 胡子鱼
# re: Rule Of Development
2005-12-20 17:46 | Flyingis
市面上可以买到一个小册子《Java编程规范》,英文版的,讲的也非常详细,但仅限于Java编码部分,不包括JSP等。  回复  更多评论
  
# re: Rule Of Development
2005-12-20 17:50 | 胡子鱼
谢谢介绍!  回复  更多评论
  
# re: Rule Of Development
2005-12-20 18:06 | ronghao
不错,仔细看了一遍,发现有几个地方自己未在意过,流汗:)  回复  更多评论
  
# re: Rule Of Development
2005-12-20 18:26 | TrampEagle
确实汗,以前也看过不少规范文档,但是都没有好好遵守,以为自己已经够规范的了,但是现在看来还有不少细节没有好好重视。一定改!一定要做一个合格的编程人员。  回复  更多评论
  
# re: Rule Of Development
2005-12-22 15:46 | 老妖
汗,看来还要加强规范  回复  更多评论
  
# re: Rule Of Development[未登录]
2007-03-21 23:45 | 阿蜜果
呵呵,蛮好,有些东西平时还真没有注意!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2005年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(82)

随笔分类(59)

文章分类(21)

相册

收藏夹(40)

GoodSites

搜索

  •  

积分与排名

  • 积分 - 1263743
  • 排名 - 22

最新评论

阅读排行榜