`
bibiye
  • 浏览: 172209 次
社区版块
存档分类
最新评论

Rule Of Development

阅读更多

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"/>
分享到:
评论

相关推荐

    Fiori_Development_Guideline_Portal_V1.1 - Final Version.pdf

    A typical use case - for example - for this guideline is that you have to change the margin of some UI elements in you app to get a pixel perfect design but you heard that there is the golden rule ...

    Game Physics Engine Development的源代码

    常见的积分器有简单欧拉方法(Euler Integration)和辛普森法则(Simpson's Rule),Cyclone可能采用了更高级的数值积分技术,如半隐式欧拉方法或Verlet积分,以提高模拟精度。 4. **约束(Constraints)**:约束...

    Fuzzy Sets Engineering

    1.2.2 The general architecture of fuzzy models and the methodology of their development 1.3 References Chapter 2—Development of input interfaces 2.1 The frame of cognition 2.1.1 ...

    C安全编码标准(实现C安全编程的权威指南)__...

    Although this is an incomplete work, we would greatlyappreciate your comments and feedback at this time to further the development and refinement of thematerial. Please provide comments that are ...

    C安全编码标准-CERT C Programming Language Secure Coding Standard

    appreciate your comments and feedback at this time to further the development and refinement of the material. Please provide comments that are commensurate with the existing detail in the document. ...

    httpd2.2.12.tar

    One suggested rule of thumb is that if it requires too much effort to port a module from 2.2 to 2.4, then the stable version should be labeled 3.0. In order to ease the burden of creating ...

    Information and Computer Technology, Modeling and Control

    Chapter 32 A Conceptual Approach to the Regional Development of Georgia* Chapter 33 Some Problems of the Regional Development in Georgia* Chapter 34 Code Injection Techniques into a Remote Process and...

    Fuzzy Sets in Human-Centric Systems

    Fuzzy models and granular models: design methodology, development algorithms, interpretability accuracy tradeoffs, validation and verification. Rulebased architectures Fuzzy clustering and ...

    skopos theory翻译功能目的论.ppt

    Vermeer, having been influenced by Katharina Reiss, sought to create a new translation theory, which eventually led to the development of the Skopos theory. Their collaborative work, "A Framework for...

    Rules-of-development:前端开发人员的开发准则

    描述 用户界面开发规则将使新的前端开发人员(HTML编码器)熟悉公司内采用的代码... 作者 Google文档中的原始创意,创建和放置 转换为Markdown,在GitHub上添加,托管和维护 公开法规: 上次更新时间:2015年6月17日

    Forecasting S&P 500 stock index futures with a hybrid AI system

    By highlighting the advantages and overcoming the limitations of both the neural networks technique and rule-based systems technique, the hybrid approach can facilitate the development of more ...

    Spring 实践(Spring in Practice).pdf版本

    As a general rule, it does so by isolating infrastructural concerns (such as persistence management and transaction management) from domain concerns. The framework handles the former so app ...

    企业管理制度律师职业独立理论及制度研究.pdf

    These issues are attributed to the initial lack of emphasis on freedom and independence in the design of China's lawyer system, which has hindered the profession's development. Lastly, the paper ...

    Xamarin.Cross.Platform.Development.Cookbook.178

    Table of Contents Chapter 1. One Ring to Rule Them All Chapter 2. Declare Once, Visualize Everywhere Chapter 3. Native Platform-Specific Views and Behavior Chapter 4. Different Cars, Same Engine ...

    Adaptive Model Rules From High-Speed Data Streams

    The paper "Adaptive Model Rules from High-Speed Data Streams" discusses the development and implementation of an adaptive model specifically designed to handle regression problems in real-time ...

Global site tag (gtag.js) - Google Analytics