应用组Java编码规范_第1页
应用组Java编码规范_第2页
应用组Java编码规范_第3页
应用组Java编码规范_第4页
应用组Java编码规范_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、无锡中科方德应用组Java编码规范(版本 1.0)无锡中科方德软件有限公司2007-8-31版本变更说明版本完稿时间撰写人说明1.02007-8-31吴德有、郗嘉初稿2007-9-3吴德有修改2007-10-18靳健补充实例目 录17 / 17文档可自由编辑打印1概述41.1编写目的41.2预期读者41.3适用范围42命名规范52.1包命名规则52.1.1与业务系统相关的包命名52.1.2与业务系统无关的、可公用的包52.2类名、接口命名规则52.3方法命名规则62.4变量命名规则62.5常量命名规则73代码书写规范83.1返回值83.2异常83.3表达式93.4体前代码93.5注释104代码

2、规范举例111 概述1.1 编写目的本文描述了应用组基于JAVA开发中的有关包、类、接口、方法、实例变量、变量和常量的命名规则,用于规范各个项目组的有关JAVA编程过程中的命名和代码书写规范。1.2 预期读者应用组全体成员。1.3 适用范围适用于应用组所有基于JAVA开发的项目。2 命名规范2.1 包命名规则应用组将基于JAVA开发中产生的包分为两类,一是与各业务系统相关的包,一是与业务系统无关的、可公用的包。它们的命名规则除要遵守“包名应全部是小写字母,包名中不能出现下划线,并且第一个字母不能是数字”的规则。2.1.1 与业务系统相关的包命名与业务系统相关的包命名格式为:com.nfschi

3、na.<projectabbr>.<modulename>。其中: <projectabbr>为项目英文简称或缩写;<modulename>为模块英文名称或简称。2.1.2 与业务系统无关的、可公用的包成熟框架方面的包命名格式为:com.nfschina.framework.<Classname>工具方面的包命名格式为:com. nfschina.util.<Classname >2.2 类名、接口命名规则类和接口的名称应是一个名词,采用大小写混和的方式,所有单词都应紧靠在一起,其中每个单词的首字母应大写。例如: clas

4、s Person;class ImageSprite;每个类定义要前必须加类的说明。2.3 方法命名规则方法名应是一个动词,采用大小写混和的方式,其中第一个单词的首字母用小写,其后单词的首字母大写。例如:Xxxxx.getBackgorund();每个方法前必须加说明包括:参数说明、返回值说明、异常说明。如果方法名实在是太长可以对变量名缩写,但是必须添加相应的说明。2.4 变量命名规则变量命名一般采用大小写混和的方式,第一个单词的首字母小写,其后单词的首字母大写,变量名一般不要用下划线或美元符号开头。变量名应简短且有意义,即,能够指出其用途。除非是一次性的临时变量,应尽量避免单个字符的变量名。

5、(1)类的实例对象定义如下:Person person;(2)同一个类的多个对象可以采用一下定义方式:Person person_1;Person person_2;(3)集合类的实例命名使用集合包含元素的英文名称的复数表示,例如:Vector persons;(4)如果变量名实在是太长可以对变量名缩写,但是必须在类说明或方法说明部分(视缩写的范围而定)进行说明。(5)数组的声明要用"int packets"的形式,而不要用"int packets"。2.5 常量命名规则类常量和ANSI常量的命名应全部用大写,单词间用下划线隔开。例如:final sta

6、tic int MIN_WIDTH = 4;final static int MAX_WIDTH = 99;3 代码书写规范类的方法的代码行数不能过长,尽量控制在100行(90%),长的方法要拆分成私有函数。3.1 返回值在一般情况下,方法返回值不应返回null。而是尽量使用异常代替返回null。如果在特殊情况必须返回null, 必须在方法说明中加以特别说明,如使用“特别注意”等字样。例如:从一个集合类实例中提取一个对象,因为有些集合类实例是允许null作为键或值的,这个时候用异常取代返回null就不合适了。如果方法的返回值是集合类对象,而且返回的集合对象不包含任何元素时,则应返回0长度或0大

7、小的集合对象。不能返回null。3.2 异常整个应用系统使用自行设计的唯一异常类,该类包括message(表示错误信息)和ID号(整型,表示异常类型)两部分,该类在创建时是自动获得类名、方法名、行号等信息。在系统开发和上线之后的一段时间内,异常信息要直接发送到浏览器页面,以便于开发人员迅速定位错误。3.3 表达式1) 所有的算术、逻辑表达式的每一项运算都需要加圆括号,避免使用java语言的运算符优先级,例如:(2 *(x + y)/(1 - x);(n > 1)?(n - 1):(n = 1)result =(result && (lastO

8、perand > nextOperand));2) 二元算术运算符(除去“/”)、二元逻辑元素符、赋值运算符,既“+、-、*、%、+=、-=、*=、/=、%=、>、<、 =、 >=、<=、 =”等符号左右两边要加空格,例如:if(lastOperand >= lastOperand)3) 参数说明部分的逗号“,”和for语句循环说明部分的分号“;”之前不需要留空格,之后需要留空格。如:Calculator.add(int a, int b);for(int i = 0; i < 100; i +);3.4 体前代码体前代码包括:a)

9、 方法的参数说明和异常说明;b) 条件语句,如if语句、switch语句;c) 循环语句,如while语句、for语句。这些语句的参数说明、条件说明和循环控制都放在圆括号内。如果不是特别长,应尽量放在同一行内。同时注意,参数说明、条件说明和循环控制的结束圆括号“)”与体开始花括号“”之间留一个空格。3.5 注释注释是软件可读性的具体体现。程序注释量一般占程序编码量的20%,软件工程要求不少于20%。以下是四种必要的注释:(1) 类说明注释注释一般位于 package/import 语句之前,class 描述之前。要求至少写出内容说明、创建者、创建时间和特别注意事项

10、等内容。例如:/* Script Name : <b>CmsPersistencer</b>* Description : Content Management System Data Access Layer* Type : Java* Last Modified : <b>2007-10-17 16:58:00</b>* since 2007/09/12* author jinjian*/(2) 方法说明注释对几乎每个方法都应有适当的说明,位于方法声明之前,包括:说明,参数说明、异常说明、返回值说明和特别说明等。例如:/* Insert a

11、Menuitem entity* param connection* the specific database connection* param menuItem* the menuitem to be inserted* return the inserted menuItem id* throws NfschinaException* when it occurs a SQLException*/(3) 体内代码的注释体(方法体、代码块体、静态代码块体等)内的代码按照功能分成多个虚拟的功能块,每个块以块注释“/* xxx */”注释开始,以空行结束;例如:/* articleTitle

12、 */if (nameLike != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "articleTitle LIKE '" + nameLike + "'"/* creator */if (creatorLike != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "creator LIKE '

13、;" + creatorLike + "'"(4) 行注释行注释“/”仅用于调试注释,在程序稳定之后,行注释必须被删除,以免影响程序的可读性。4 代码规范举例package com.nfschina.cms.controller;/* * Script Name : <b>CmsPersistencer</b> * Description : Content Management System Data Access Layer * Type : Java * Last Modified : <b>2007-9-15 1

14、6:58:00</b> * * since 2007/09/12 * author jinjian */import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Date;import java.util.Vector;

15、import com.nfschina.cms.model.Article;import com.nfschina.cms.model.ArticleCategory;import com.nfschina.cms.model.ArticleSchema;import com.nfschina.cms.model.ArticleStyle;import com.nfschina.cms.model.Document;import com.nfschina.cms.model.Image;import com.nfschina.cms.model.Menuitem;import com.nfsc

16、hina.utils.NfschinaException;import com.nfschina.utils.Tools;public class CmsPersistencer ./* = */* * Parts of Article */* * Get an article entry by its id * * param connection * the specific database connection * param articleId * the article wanted to be got * return the article * * throws Nfschin

17、aException * when it occurs a SQLException */public Article getArticle(Connection connection, String articleId)throws NfschinaException Article article = null;PreparedStatement preparedStatement = null;ResultSet resultSet = null;try preparedStatement = connection.prepareStatement("SELECT articl

18、eSchemaId, articleStyleId, "+ "articleTitle, content, creator, "+ "createTimestamp, contentType, openCount "+ "FROM Article WHERE articleId = '"+ articleId+ "'");resultSet = preparedStatement.executeQuery();while (resultSet.next() article = new Ar

19、ticle();article.setArticleId(articleId);article.setArticleSchema(getArticleSchema(connection, resultSet.getString("articleSchemaId");article.setArticleStyle(getArticleStyle(connection, resultSet.getString("articleStyleId");article.setArticleTitle(resultSet.getString("article

20、Title");article.setContent(resultSet.getBinaryStream("content");article.setCreator(resultSet.getString("creator");article.setType(resultSet.getInt("contentType");article.setOpenCount(resultSet.getLong("openCount");article.setArticleCategoryId(getArticleAr

21、ticleCategoriesByArticleID(connection, articleId);if (article = null)throw new NfschinaException(NfschinaException.DATA_NOTFOUND,"文章" + articleId + "找不到");return article; catch (SQLException e) throw new NfschinaException(e.getMessage(); finally try resultSet.close();if (prepared

22、Statement != null) preparedStatement.close(); catch (SQLException e) throw new NfschinaException(e.getMessage();/*-*/* * Get article Array selected by some condition * * param connection * the specific database connection * param articleSchemaId * the schemaId * param articleStyleId * the articleSty

23、leId * param nameLike * the name contains in article title * param creatorLike * the name contains in creator * param type * the article type * param minOpenCount * the minimum times opened * param maxOpenCount * the max times opened * param afterCreateTimestamp * the time after the create time * pa

24、ram beforeCreateTimestamp * the time before the create time * return the article Array * * throws NfschinaException * when it occurs a SQLException */public Article selectArticles(Connection connection,String articleSchemaId, String articleStyleId, String nameLike,String creatorLike, Integer type, I

25、nteger minOpenCount,Integer maxOpenCount, Date afterCreateTimestamp,Date beforeCreateTimestamp) throws NfschinaException PreparedStatement preparedStatement = null;ResultSet resultSet = null;try String sql = "SELECT articleId, articleSchemaId, articleStyleId, "+ "articleTitle, content

26、, creator, "+ "createTimestamp, contentType, openCount "+ "FROM Article"String where = ""/* articleSchemaId */if (articleSchemaId != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "articleSchemaId = " + articleSchemaId;/*

27、articleStyleId */if (articleStyleId != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "articleStyleId = " + articleStyleId;/* articleTitle */if (nameLike != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "articleTitle L

28、IKE '" + nameLike + "'"/* creator */if (creatorLike != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "creator LIKE '" + creatorLike + "'"/* contentType */if (type != null) where += (where.length() = 0) ? " WHER

29、E " : " AND ")+ "contentType = " + type;/* openCount */if (minOpenCount != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "openCount > " + minOpenCount;if (maxOpenCount != null) where += (where.length() = 0) ? " WHERE "

30、 : " AND ")+ "openCount < " + maxOpenCount;/* createTimestamp */Vector<Timestamp> vector = new Vector<Timestamp>();if (afterCreateTimestamp != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "createTimestamp > ?"vector.a

31、dd(Tools.dateToTimestamp(afterCreateTimestamp);if (beforeCreateTimestamp != null) where += (where.length() = 0) ? " WHERE " : " AND ")+ "createTimestamp < ?"vector.add(Tools.dateToTimestamp(beforeCreateTimestamp);sql += " ORDER BY createTimestamp desc"prepa

32、redStatement = connection.prepareStatement(sql + where);for (int k = 0; k < vector.size(); k+) preparedStatement.setTimestamp(k + 1, (Timestamp) vector.get(k);resultSet = preparedStatement.executeQuery();ArrayList<Article> list = new ArrayList<Article>(128);while (resultSet.next() Article article = new Article();article.setArticleId(resultSet.getString("

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论