Java coding rule

2019-02-15 14:46

Java coding rules

(a) The class name should be the same as file name

Example: public class Point => Point.java

(b) The location of file

The root directory of the project is decided, and it puts it in the directory tree according to the “.” in the package name.

Example: com.yellowbook.myProject => /com/yellowbook/myProject

(c) Package name

The package name should be all lowercase Example: com.yellowbook.common.dao

(d) Class name

The first letter of each word in the class name should be capitalized. The length of the class name should be within 20 characters. The name should be a noun. Example: GoogleSearchEngine

(e) Exceptional Class name

The word “Exception” should be put at the end of the Class name.

Apart from the word “Exception”, the name should be limited to 15 characters. Example: ClassNameException

(f) Interface name

The letter “I” should be in the front of the class name. Example: IClassName

(g) Implement class name

“Impl” should be at the end of the class name. Example: ClassNameImpl

(h) Abstract class name

“Abstract” should be in the front of the class name. Example: AbstractClassName

(i) Constant name

Static variable (constant) declared names should all be capital letters. When the name consists of two or more words, delimit it with “_\ Example: ER_CASE_WITH_UNDERSCORES (j) Variable name

The first letter should be lower case, and the head of following words should be capitalized. The words in the variable name should be meaningful.

Neither an array nor a primitive variable should be written in one line. Example(Bad): int a, b[]; × Example(Good): int firstArray[]; Int secondArray[]; ○

Static variables should not be used if possible unless they are static final variables. Example: targetList, anniversaryDate etc.

(k) Method name

The first letter should be lowercase, and the first letter of the following words should be capitalized.

Example: setSecurtyFlag, setMailSecurtyLevel etc.

(l) Factory method name

Example: newXXX(), createXXX()

(m) Converter method name

Example: toXXX()

(n) Boolean returned method name

Is + Adjective, can + Verb, has + Past participle

Example: isEmpty(), canGet(), hasChenged()

(o) Loop counter

I, j, and k are sequentially used for loop counter

(p) Symmetry of name

When the class name and the method name are named, the following English symmetry should be noted. add/remove insert/delete get/set start/stop begin/end send/receive first/last

apply/release put/get up/down show/hide

source/target open/close

source/destination increment/decrement lock/unlock old/new

next/previous

(q) No meaningful name

The following nouns should not be used to name variables. Example: Info, Data, Temp, Str, Buf ×

(r) Source Comment

Block comment:

/*

* block comment * block comment */

One line comment if (xxx) { }

(s) Method comment

/**

* The functionality of the method should be explained. *

* @param the explanation of parameter

* @return the explanation of returned value

* @exception the explanation of expected exception of the method * @see Class name * @since version */

(t) Class comment

/* one line comment */ xxxMethod(xxx);

/**

* The functionality of the class *

* @see class name * @since version */

(u) Statement

Two or more variable definitions should not be in one line.

\

Example(Bad): i++; j++; ×

if (xxx) i++; × Example(Good):

if (xxx) {

i++;

}

Switch block should be:

If “break” is not used、the comment“/* no break*/”should be added.

“default” should not be omitted. “break” must be specified in the “default” block. switch (a) {

case ABC: ???? /* no break */ case DEF: ???? break; default:

???? break;

}

(v) Collection

It is recommended that List(ArrayList), Map(HashMap), and Iterator should be used instead of Vector, Hashtable, and Enumeration, respectively.

(w) import

import xxx.xxx.* is not recommended. (x) Final

If a parameter of a method does not have its value changed within the method, it is strongly recommended that the word “final” be added in front of the parameter. Example:

Private String searchRouteName(final String strSource, final String strDestination) { …. }


Java coding rule.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:八年级下册物理导学案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: