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 =>
(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) { …. }
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java coding rule在线全文阅读。
相关推荐: